merchantGrounding.vue 45 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
<!-- 商户上架 -->
<template>
  <d2-container>
    <div class="merchant-grounding-form" v-loading="showLoading">
      <el-form ref="merchantGroundingForm" :model="formData" label-width="220px" :rules="rules">
        <div class="title">流量方信息</div>
        <div v-for="(item, index) in trafficsideForm" :key="index">
          <el-form-item :label="item.label + ':'" :prop="item.name">
            <template v-if="item.type == 'methodsSelect'">
              <!-- 流量方 -->
              <template v-if="item.name == 'merchantId'">
                <el-select v-model="formData[item.name]"
                          :disabled="addOrEdit == 'edit'"
                          :placeholder="item.placeholder"
                          @change="handel4SStoreChange"
                          style="width: 432px;" remote reserve-keyword @visible-change="(event) => selectVisibleChange(event, item)"
                          :remote-method="(event) => remoteMethod(event, item.searchKey, 'store')" v-el-select-loadmore="interestsLoadmoreMerchant" filterable>
                  <el-option v-for="subItem in item.options" :key="subItem[item.optionsId]"
                              :label="subItem[item.optionsLabel]"
                              :value="subItem[item.optionsId]">
                  </el-option>
                </el-select>
              </template>
              <!-- 一级流量方 -->
              <template v-if="item.name == 'firstFlowMerchantId'">
                <el-select v-model="formData[item.name]"
                          @change="handleFirstFlowChange"
                          :placeholder="item.placeholder"
                          style="width: 432px;" remote reserve-keyword @visible-change="(event) => selectVisibleChange(event, item)"
                            :remote-method="(event) => remoteMethod(event, item.searchKey, 'first')" v-el-select-loadmore-first="interestsLoadmoreFirstMerchant" filterable>
                  <el-option v-for="subItem in item.options" :key="subItem[item.optionsId]"
                              :label="subItem[item.optionsLabel]"
                              :value="subItem[item.optionsId]">
                  </el-option>
                </el-select>
              </template>
              <!-- 流量通道方 -->
              <template v-if="item.name == 'flowChannelMerchantId'">
                <el-select v-model="formData[item.name]"
                          @change="handleFlowChannelChange"
                          :placeholder="item.placeholder"
                          style="width: 432px;" remote reserve-keyword @visible-change="(event) => selectVisibleChange(event, item)"
                            :remote-method="(event) => remoteMethod(event, item.searchKey, 'flow')" v-el-select-loadmore-flow="interestsLoadmoreFlowMerchant" filterable>
                  <el-option v-for="subItem in item.options" :key="subItem[item.optionsId]"
                              :label="subItem[item.optionsLabel]"
                              :value="subItem[item.optionsId]">
                  </el-option>
                </el-select>
              </template>
            </template>
            <template v-if="item.type == 'input'">
              <el-input v-model.trim="formData[item.name]" 
                        style="width: 432px;" :maxlength="item.maxLength" 
                        @input="(event) => handleInput(event, '', item.name)" 
                        :placeholder="item.placeholder">
              </el-input>
            </template>
            <template v-if="item.type == 'select'">
              <el-select v-model="formData[item.name]" filterable :disabled="item.disabled" :no-data-text="item.empty || '无数据'" @visible-change="(event) => selectVisibleChange(event, item)" style="width: 432px;" :placeholder="item.placeholder">
                <el-option
                  v-for="(subItem, subIndex) in item.options"
                  :key="subIndex"
                  :label="subItem[item.optionsLabel]"
                  :value="subItem[item.optionsId]"
                  :disabled="subItem.disabled">
                </el-option>
              </el-select>
            </template>
          </el-form-item>
        </div>
        <div class="title">服务商信息</div>
        <el-form-item label="一级服务商:">
          <el-select v-model="formData['firstServiceMerchantId']" filterable style="width: 432px;" placeholder="请选择一级服务商">
            <el-option
              v-for="(subItem, subIndex) in baseData.serviceList"
              :key="subIndex"
              :label="subItem['merchantName']"
              :value="subItem['merchantId']">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级服务商:">
          <el-select v-model="formData['secondServiceMerchantId']" filterable style="width: 432px;" placeholder="请选择二级服务商">
            <el-option
              v-for="(subItem, subIndex) in baseData.serviceList"
              :key="subIndex"
              :label="subItem['merchantName']"
              :value="subItem['merchantId']">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="三级服务商(营销):" prop="marketServiceList" class="edit-table">
          <el-table :data="formData.marketServiceList" :header-cell-style="{ 'text-align': 'center' }" border style="width: 100%">
            <el-table-column label="序号" width="60px" align="center">
              <template slot-scope="scope">
                {{ scope.$index + 1 }}
              </template>
            </el-table-column>
            <el-table-column v-for="(item, index) in filterTableColumn" :key="index" :label="item['label']" :render-header="addRedStar" align="center">
              <template slot-scope="scope" v-if="!item.isHide">
                <el-form-item v-if="item.type == 'input'" :prop="`marketServiceList.${scope.$index}.${item.name}`">
                  <el-input v-model.trim="scope.row[item.name]" 
                            :placeholder="item.placeholder" 
                            :maxlength="item.maxLength" 
                            @input="(event) => handleInput(event, 'marketServiceList', item.name, scope.$index)">
                  </el-input>
                </el-form-item>
                <el-form-item v-if="item.type == 'select'" :prop="`marketServiceList.${scope.$index}.${item.name}`" :rules="item.rules">
                  <el-select v-model="scope.row[item.name]" filterable :placeholder="item.placeholder">
                    <el-option v-for="(subItem, subIndex) in item.options" 
                               :key="subIndex" 
                               :label="subItem[item.optionsLabel]" 
                               :value="subItem[item.optionsId]"
                               :disabled="marketingServiceDisabledList.includes(subItem[item.optionsId])">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item v-if="item.type == 'checkbox'" :prop="`marketServiceList.${scope.$index}.${item.name}`" :rules="item.rules">
                  <el-checkbox-group v-model="scope.row[item.name]">
                    <el-checkbox v-for="(subItem, subIndex) in item.options" :key="subIndex" :label="subItem[item.optionsId]">{{ subItem[item.optionsLabel]}}</el-checkbox>
                  </el-checkbox-group>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="60px" align="center">
              <template slot-scope="scope">
                <el-button type="text" @click="deleteTableRow('marketServiceList', scope.$index)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-button type="primary" plain v-if="formData.marketServiceList.length < 10" @click="addTableRow('marketServiceList')">添加</el-button>
        </el-form-item>
        <el-form-item label="三级服务商(安装):" class="edit-table">
          <el-table :data="formData.installServiceList" :header-cell-style="{ 'text-align': 'center' }" border style="width: 100%">
            <el-table-column label="序号" width="60px" align="center">
              <template slot-scope="scope">
                {{ scope.$index + 1 }}
              </template>
            </el-table-column>
            <el-table-column v-for="(item, index) in filterTableColumn" :key="index" :label="item['label']" :render-header="addRedStar" align="center">
              <template slot-scope="scope">
                <el-form-item v-if="item.type == 'input'" :prop="`installServiceList.${scope.$index}.${item.name}`">
                  <el-input v-model.trim="scope.row[item.name]" 
                            :placeholder="item.placeholder" 
                            :maxlength="item.maxLength" 
                            @input="(event) => handleInput(event, 'installServiceList', item.name, scope.$index)">
                  </el-input>
                </el-form-item>
                <el-form-item v-if="item.type == 'select'" :prop="`installServiceList.${scope.$index}.${item.name}`" :rules="item.rules">
                  <el-select v-model="scope.row[item.name]" filterable :placeholder="item.placeholder">
                    <el-option v-for="(subItem, subIndex) in item.options" 
                               :key="subIndex" 
                               :label="subItem[item.optionsLabel]" 
                               :value="subItem[item.optionsId]"
                               :disabled="installServiceDisabledList.includes(subItem[item.optionsId])">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item v-if="item.type == 'checkbox'" :prop="`installServiceList.${scope.$index}.${item.name}`" :rules="item.rules">
                  <el-checkbox-group v-model="scope.row[item.name]">
                    <el-checkbox v-for="(subItem, subIndex) in item.options" :key="subIndex" :label="subItem[item.optionsId]">{{ subItem[item.optionsLabel]}}</el-checkbox>
                  </el-checkbox-group>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="60px" align="center">
              <template slot-scope="scope">
                <el-button type="text" @click="deleteTableRow('installServiceList', scope.$index)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-button type="primary" plain v-if="formData.installServiceList.length < 10" @click="addTableRow('installServiceList')">添加</el-button>
        </el-form-item>
        <div class="title">其他信息</div>
        <el-form-item label="收入所属公司:">
          <el-select v-model="formData.incomeOwnerMerchantId" style="width: 432px;" placeholder="请选择收入所属公司">
            <el-option v-for="(item, index) in baseData.contractAttributionList" :key="index" :label="item.merchantAccount || item.merchantName" :value="item.merchantId" />
          </el-select>
        </el-form-item>
        <el-form-item label="是否为一般纳税人:">
          <el-radio-group v-model="formData.generalTaxpayer">
            <el-radio :label="'Y'"> 是 </el-radio>
            <el-radio :label="'N'"> 否 </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div class="footer">
        <el-button @click="navBack">取消</el-button>
        <el-button @click="submit" type="primary">保存</el-button>
      </div>
    </div>
  </d2-container>  
</template>
<script>
import { sceneCodeList, invoiceTaxRateList, cooperationModeList, paymentMethodList, partnerClassifyList, subBusinessList } from '@/common/constants.js'
import { mapActions } from 'vuex'
import {
  getServiceList,
  getyqMerchantList,
  getContractAffiliationDataList,
  serviceGetMerchantList,
  merchantOnShelf,
  getMerchantRelationDetailApi,
  getBusinessTypeByDiversionMerchant,
  get4sAndServiceMerchantListApi
} from '@api/merchant'
import { getPlatformMerchantList } from '@api/index'
export default {
  name: 'merchantGrounding',
  data() {
    return {
      formData: {
        scene: '',
        marketServiceList: [{ subBusiness: [] }],
        installServiceList: [],
        generalTaxpayer: 'Y'
      },
      trafficsideForm: [{
        label: '流量方信息',
        name: 'merchantId',
        placeholder: '请选择流量方信息',
        type: 'methodsSelect',
        optionsId: 'merchantId',
        optionsLabel: 'merchantName',
        options: [],
        searchKey: 'merchantNameKeyword'
      }, {
        label: '业务类型',
        name: 'scene',
        placeholder: '请选择业务类型',
        type: 'select',
        empty: '请先选择流量方信息',
        options: [],
        optionsId: 'confId',
        optionsLabel: 'confValue',
        optionsName: 'sceneCodeList'
      }, {
        label: '一级流量方',
        name: 'firstFlowMerchantId',
        placeholder: '请选择一级流量方',
        type: 'methodsSelect',
        options: [],
        optionsId: 'merchantId',
        optionsLabel: 'merchantName',
        searchKey: 'firstFlowMerchantNameKeyword'
      }, {
        label: '流量通道方',
        name: 'flowChannelMerchantId',
        placeholder: '请选择流量通道方',
        type: 'methodsSelect',
        options: [],
        optionsId: 'merchantId',
        optionsLabel: 'merchantName',
        searchKey: 'flowChannelMerchantNameKeyword'
      }, {
        label: '合作模式',
        name: 'cooperationModel',
        placeholder: '请选择合作模式',
        type: 'select',
        options: [],
        optionsId: 'confId',
        optionsLabel: 'confValue',
        optionsName: 'cooperationModeList'
      }, {
        label: '收款方式',
        name: 'paymentMethod',
        placeholder: '请选择收款方式',
        type: 'select',
        options: [],
        optionsId: 'confId',
        optionsLabel: 'confValue',
        optionsName: 'paymentMethodList'
      }, {
        label: '流量方合同规定发票税率',
        name: 'contractStipulateRate',
        placeholder: '请选择税率',
        type: 'select',
        options: [],
        optionsId: 'confId',
        optionsLabel: 'confValue',
        optionsName: 'invoiceTaxRateList'
      }, {
        label: '流量方实际发票税率',
        name: 'contractActualRate',
        placeholder: '请选择税率',
        type: 'select',
        options: [],
        optionsId: 'confId',
        optionsLabel: 'confValue',
        optionsName: 'invoiceTaxRateList'
      }, {
        label: '合同归属',
        name: 'contractOwnerMerchantId',
        placeholder: '请选择合同归属',
        type: 'select',
        options: [],
        optionsId: 'merchantId',
        optionsLabel: 'merchantName',
        optionsName: 'platformMerchantList'
      }, {
        label: '合同编码',
        name: 'contractCode',
        placeholder: '请输入合同编码',
        type: 'input',
        maxLength: 50
      }, {
        label: '园区店信息',
        name: 'yqMerchantId',
        placeholder: '请选择园区店信息',
        type: 'select',
        options: [],
        optionsId: 'merchantId',
        optionsLabel: 'merchantName',
        optionsName: 'yqMerchantList'
      }],
      rules: {
        merchantId: [{ required: true, message: '请选择流量方', trigger: 'change' }],
        scene: [{ required: true, message: '请选择业务类型', trigger: 'change' }],
        yqMerchantId: [{ required: true, message: '请选择园区店', trigger: 'change' }],
        marketServiceList: [ { required: true, message: '请添加三级服务商(营销)', trigger: ['blur', 'change']}]
      },
      tableColumn: [{
        label: '服务商名称',
        name: 'merchantId',
        type: 'select',
        placeholder: '请选择服务商',
        options: [],
        optionsId: 'merchantId',
        optionsLabel: 'merchantName',
        optionsName: 'serviceList',
        rules: [{ required: true, message: '请选择服务商', trigger: 'change'}]
      }, {
        label: '服务商分类',
        name: 'serviceProviderType',
        type: 'select',
        placeholder: '请选择分类',
        options: [],
        optionsId: 'confId',
        optionsLabel: 'confValue',
        optionsName: 'partnerClassifyList',
      }, {
        label: '子业务',
        name: 'subBusiness',
        type: 'checkbox',
        placeholder: '请选择子业务',
        options: [],
        optionsId: 'confId',
        optionsLabel: 'confValue',
        optionsName: 'subBusinessList',
        isHide: false,
        rules: [{ required: true, message: '请选择子业务', trigger: 'change'}]
      }, {
        label: '合同规定发票税率',
        name: 'contractStipulateRate',
        type: 'select',
        placeholder: '请选择税率',
        options: [],
        optionsId: 'confId',
        optionsLabel: 'confValue',
        optionsName: 'invoiceTaxRateList'
      }, {
        label: '实际发票税率',
        name: 'contractActualRate',
        type: 'select',
        placeholder: '请选择税率',
        options: [],
        optionsId: 'confId',
        optionsLabel: 'confValue',
        optionsName: 'invoiceTaxRateList'
      }, {
        label: '合同归属',
        name: 'contractOwnerMerchantId',
        placeholder: '请选择合同归属',
        type: 'select',
        options: [],
        optionsId: 'merchantId',
        optionsLabel: 'merchantName',
        optionsName: 'platformMerchantList'
      }, {
        label: '合同编码',
        name: 'contractCode',
        placeholder: '请输入合同编码',
        type: 'input',
        maxLength: 50
      }],
      routeParams: {}, // 路由参数
      baseData: {},
      addOrEdit: '',
      storeCurrentPage: 1, // 4s店数据分页
      firstCurrentPage: 1, // 一级流量方数据分页
      flowCurrentPage: 1, // 流量通道数据分页
      pageSize: 50,
      merchantNameKeyword: '', // 流量方下拉框搜索
      firstFlowMerchantNameKeyword: '', // 一级流量方下拉框搜索
      flowChannelMerchantNameKeyword: '', // 流量通道方下拉框搜索
      showLoading: false, // 是否显示loading
      selected4SOptionObj: {}, // 已选的4S店
      selectedFirstOptionsObj: {}, // 已选的一级流量方
      selectedFlowOptionsObj: {} // 已选的流量通道
    }
  },
  computed: {
    // 三级服务商(营销) 不可重复选择的项
    marketingServiceDisabledList() {
      let list = []
      this.formData?.marketServiceList?.forEach(val => {
        if (val.merchantId) list.push(val.merchantId)
      })
      return list
    },
    // 三级服务商(安装) 不可重复选择的项
    installServiceDisabledList() {
      let list = []
      this.formData?.installServiceList?.forEach(val => {
        if (val.merchantId) list.push(val.merchantId)
      })
      return list
    },
    filterTableColumn() {
      return this.tableColumn.filter(v => !v.isHide)
    }
  },
  watch: {
    'formData.scene': {
      handler(val) { // 监听业务类型变化 会员权益/升级 有子业务
        if (val == 0) {
          this.tableColumn.find(v => v.name == 'subBusiness').isHide = false
        } else {
          this.tableColumn.find(v => v.name == 'subBusiness').isHide = true
        }
        this.$nextTick(() => { // 清除校验
          this.$refs['merchantGroundingForm'].clearValidate()
        })
      },
      deep: true
    }
  },
  beforeRouteEnter(to, from, next) {
    if (to.query.addOrEdit == 'edit') {
      to.meta.title = '编辑页面'
    } else {
      to.meta.title = '商户上架'
    }
    next()
  },
  created() {
    
  },
  mounted() {
    this.addOrEdit = this.$route.query.addOrEdit
    this.routeParams = this.$route.query?.params ? JSON.parse(this.$route.query?.params) : {}
    this.getBaseData()
    let requestEventArr = [
      this.getServiceList(), // 获取服务商列表
      this.getyqMerchantList(), // 获取园区店列表
      this.get4sAndServiceMerchantList(), // 获取4s店和服务商列表
      this.getContractAffiliationDataList(), // 获取收入所属公司数据列表
      this.getPlatformMerchantList() // 获取平台方数据(账户中心下省代市代商户列表)
    ]
    if (this.addOrEdit === 'edit') { // 编辑时流量方信息字段不可操作 不需拉接口
      requestEventArr.push(this.getMerchantDetail())
    } else {
      requestEventArr.unshift(this.get4SStoreList())
    }
    this.showLoading = true
    Promise.all(requestEventArr).then(() => {
      this.showLoading = false
    })
  },
  directives: {
    'el-select-loadmore': {
      bind(el, binding) {
        const SELECTWRAP_DOM = el.querySelector(
          '.el-select-dropdown .el-select-dropdown__wrap'
        )
        SELECTWRAP_DOM.addEventListener('scroll', function () {
          const condition =
            this.scrollHeight - this.scrollTop <= this.clientHeight
          if (condition) {
            binding.value()
          }
        })
      }
    },
    'el-select-loadmore-first': {
      bind(el, binding) {
        const SELECTWRAP_DOM = el.querySelector(
          '.el-select-dropdown .el-select-dropdown__wrap'
        )
        SELECTWRAP_DOM.addEventListener('scroll', function () {
          const condition =
            this.scrollHeight - this.scrollTop <= this.clientHeight
          if (condition) {
            binding.value()
          }
        })
      }
    },
    'el-select-loadmore-flow': {
      bind(el, binding) {
        const SELECTWRAP_DOM = el.querySelector(
          '.el-select-dropdown .el-select-dropdown__wrap'
        )
        SELECTWRAP_DOM.addEventListener('scroll', function () {
          const condition =
            this.scrollHeight - this.scrollTop <= this.clientHeight
          if (condition) {
            binding.value()
          }
        })
      }
    }
  },
  methods: {
    ...mapActions({ close: 'd2admin/page/close' }),
    getServiceList() {
      return new Promise((resolve, reject) => {
        let params = {
          pageSize: 100000,
          currentPage: 1
        }
        getServiceList(params).then(res => {
          res?.list?.forEach(val => {
            val.merchantId = val.merchantNo - 0
          })
          this.baseData.serviceList = res?.list || []
          this.getOptionsList()
          resolve()
        })
      })
    },
    getContractAffiliationDataList() {
      return new Promise((resolve, reject) => {
        getContractAffiliationDataList({}).then(res => {
          this.baseData.contractAttributionList = res || []
          this.getOptionsList()
          resolve()
        })
      })
    },
    getyqMerchantList() {
      return new Promise((resolve, reject) => {
        getyqMerchantList({currentPage: 1, pageSize: 100000}).then(res => {
          res?.list?.forEach(val => {
            val.merchantId = val.merchantNo - 0
          })
          this.baseData.yqMerchantList = res?.list || []
          this.getOptionsList()
          resolve()
        })
      })
    },
    getMerchantDetail() {
      return new Promise((resolve, reject) => {
        let params = this.routeParams
        getMerchantRelationDetailApi({ merchantId: params.merchantId, scene: params.scene }).then(res => {
          // 处理数据
          res.marketServiceList?.forEach(val => { // 三级服务商(营销)
            val.merchantId = val.serviceMerchantId
            val.merchantName = val.serviceMerchantName
            val.subBusiness = val.subBusiness?.split(',') || []
          })
          res.installServiceList?.forEach(val => { // 三级服务商(安装)
            val.merchantId = val.serviceMerchantId
            val.merchantName = val.serviceMerchantName
            val.subBusiness = val.subBusiness?.split(',') || []
          })
          res.firstServiceMerchantId = res?.firstServiceMerchant?.serviceMerchantId
          res.secondServiceMerchantId = res?.secondServiceMerchant?.serviceMerchantId
          res.scene = res.scene || params.scene
          res.marketServiceList = res?.marketServiceList || []
          res.installServiceList = res?.installServiceList || []
          res.generalTaxpayer = res?.generalTaxpayer == 0 ? 'N' : 'Y'
          // 根据流量方信息 获取流量方下拉列表
          if (res.merchantId) {
            serviceGetMerchantList({currentPage: 1, pageSize: 50, merchantIdName: res.merchantId}).then(res => {
              res?.list?.forEach(val => {
                val.merchantId = val.merchantNo - 0
              })
              let index = this.trafficsideForm?.findIndex(v => v.name == 'merchantId')
              let arr = this.unique(this.trafficsideForm[index]?.options?.concat(res?.list || []))
              this.$set(this.trafficsideForm[index], 'options', arr)
            })
          }
          // 有已选中的一级流量方/流量通道方 将选中项添加到请求到的数据中用于回显
          if (res.firstFlowMerchantId) {
            get4sAndServiceMerchantListApi({currentPage: 1, pageSize: 50, merchantIdName: res.firstFlowMerchantId}).then(res => {
              res?.list?.forEach(val => {
                val.merchantId = val.merchantNo - 0
              })
              let index = this.trafficsideForm?.findIndex(v => v.name == 'firstFlowMerchantId')
              let arr = this.unique(this.trafficsideForm[index]?.options?.concat(res?.list || []))
              this.$set(this.trafficsideForm[index], 'options', arr)
            })
          }
          if (res.flowChannelMerchantId) {
            get4sAndServiceMerchantListApi({currentPage: 1, pageSize: 50, merchantIdName: res.flowChannelMerchantId}).then(res => {
              res?.list?.forEach(val => {
                val.merchantId = val.merchantNo - 0
              })
              let index = this.trafficsideForm?.findIndex(v => v.name == 'flowChannelMerchantId')
              let arr = this.unique(this.trafficsideForm[index]?.options?.concat(res?.list || []))
              this.$set(this.trafficsideForm[index], 'options', arr)
            })
          }
          this.formData = res || {}
          this.$nextTick(() => {
            this.$refs.merchantGroundingForm.clearValidate()
          })
          resolve()
        })
      })
    },
    getPlatformMerchantList() {
      return new Promise((resolve, reject) => {
        getPlatformMerchantList({}).then(res => {
          res.unshift({
            merchantId: -1,
            merchantName: '嘀加智慧云'
          })
          this.baseData.platformMerchantList = res || []
          this.getOptionsList()
          resolve()
        })
      })
    },
    handleFlowChannelChange(val) {
      let index = this.trafficsideForm?.findIndex(v => v.name == 'flowChannelMerchantId')
      let obj = this.trafficsideForm[index]?.options?.find(v => v.merchantId == val)
      this.selectedFlowOptionsObj = obj
      let arr = this.unique(this.trafficsideForm[index]?.options?.concat([obj] || []))
      this.$set(this.trafficsideForm[index], 'options', arr)
    },
    handleFirstFlowChange(val) {
      let index = this.trafficsideForm?.findIndex(v => v.name == 'firstFlowMerchantId')
      let obj = this.trafficsideForm[index]?.options?.find(v => v.merchantId == val)
      this.selectedFirstOptionsObj = obj
      let arr = this.unique(this.trafficsideForm[index]?.options?.concat([obj] || []))
      this.$set(this.trafficsideForm[index], 'options', arr)
    },
    // 获取上架数据
    remoteMethod(query, searchKey, index) {
      this[searchKey] = query
      this[index + 'CurrentPage'] = 1
      if (index == 'store') {
        let index_ = this.trafficsideForm?.findIndex(v => v.name == 'merchantId')
        this.$set(this.trafficsideForm[index_], 'options', [])
        this.get4SStoreList() // 请求接口
      } else {
        if (index == 'first') {
          let index_ = this.trafficsideForm?.findIndex(v => v.name == 'firstFlowMerchantId')
          this.$set(this.trafficsideForm[index_], 'options', [])
        } else {
          let index_ = this.trafficsideForm?.findIndex(v => v.name == 'flowChannelMerchantId')
          this.$set(this.trafficsideForm[index_], 'options', [])
        }
        this.get4sAndServiceMerchantList(index) // 请求接口
      }
    },
    // 获取更多4s店数据
    interestsLoadmoreMerchant() {
      setTimeout(() => {
        this.storeCurrentPage = this.storeCurrentPage + 1
        if (this.storeAllPages < this.storeCurrentPage) {
          this.storeCurrentPage = this.storeAllPages
          return
        }
        this.get4SStoreList()
      }, 10)
    },
    // 获取更多一级流量方数据
    interestsLoadmoreFirstMerchant() {
      setTimeout(() => {
        this.firstCurrentPage = this.firstCurrentPage + 1
        if (this.firstAllPages < this.firstCurrentPage) {
          this.firstCurrentPage = this.firstAllPages
          return
        }
        this.get4sAndServiceMerchantList('first')
      }, 10)
    },
    // 获取更多流量通道方数据
    interestsLoadmoreFlowMerchant() {
      setTimeout(() => {
        this.flowCurrentPage = this.flowCurrentPage + 1
        if (this.flowAllPages < this.flowCurrentPage) {
          this.flowCurrentPage = this.flowAllPages
          return
        }
        this.get4sAndServiceMerchantList('flow')
      }, 10)
    },
    // 切换流量方信息 清空业务类型字段
    handel4SStoreChange(val) {
      let index = this.trafficsideForm?.findIndex(v => v.name == 'merchantId')
      let obj = this.trafficsideForm[index]?.options?.find(v => v.merchantId == val)
      this.selected4SOptionObj = obj
      let arr = this.unique(this.trafficsideForm[index]?.options?.concat([obj] || []))
      this.$set(this.trafficsideForm[index], 'options', arr)
      this.formData.scene = ''
      this.$nextTick(() => {
        this.$refs['merchantGroundingForm'].clearValidate('scene')
      })
    },
    get4SStoreList() {
      return new Promise((resolve) => {
        serviceGetMerchantList({
          merchantIdName: this.merchantNameKeyword,
          currentPage: this.storeCurrentPage,
          pageSize: this.pageSize
        }).then(res => {
          res?.list?.forEach(val => {
            val.merchantId = val.merchantNo - 0
          })
          this.storeAllPages = res.pages
          let index = this.trafficsideForm?.findIndex(v => v.name == 'merchantId')
          let arr = this.unique(this.trafficsideForm[index]?.options?.concat(res?.list || []))
          if (Object.keys(this.selected4SOptionObj)?.length) {
            arr = this.unique(arr.concat([this.selected4SOptionObj] || []))
          }
          this.$set(this.trafficsideForm[index], 'options', arr)
          resolve()
        }).catch(() => {
          resolve()
        })
      })
      
    },
    get4sAndServiceMerchantList(searchIndex) {
      return new Promise((resolve) => {
        get4sAndServiceMerchantListApi({
          currentPage: this[searchIndex + 'CurrentPage'] || 1,
          pageSize: 50,
          merchantIdName: searchIndex == 'first' ? this.firstFlowMerchantNameKeyword : searchIndex == 'flow' ? this.flowChannelMerchantNameKeyword : ''
        }).then(res => {
          res?.list?.forEach(val => {
            val.merchantId = val.merchantNo - 0
          })
          let index = this.trafficsideForm?.findIndex(v => v.name == 'firstFlowMerchantId')
          let arr = this.unique(this.trafficsideForm[index]?.options?.concat(res?.list || []))
          let index_ = this.trafficsideForm?.findIndex(v => v.name == 'flowChannelMerchantId')
          let arr_ = this.unique(this.trafficsideForm[index_]?.options?.concat(res?.list || []))
          if (searchIndex == 'first') {
            if (Object.keys(this.selectedFirstOptionsObj)?.length) {
              arr = this.unique(arr.concat([this.selectedFirstOptionsObj] || []))
            }
            this.firstAllPages = res.pages
            this.$set(this.trafficsideForm[index], 'options', arr)
          } else if (searchIndex == 'flow') {
            if (Object.keys(this.selectedFlowOptionsObj).length) {
              arr_ = this.unique(arr_.concat([this.selectedFlowOptionsObj] || []))
            }
            this.flowAllPages = res.pages
            this.$set(this.trafficsideForm[index_], 'options', arr_)
          } else {
            this.firstAllPages = res.pages
            this.flowAllPages = res.pages
            this.$set(this.trafficsideForm[index], 'options', arr)
            this.$set(this.trafficsideForm[index_], 'options', arr_)
          }
          resolve()
        })
      })
    },
    unique(arr) {
      const res = new Map()
      return arr?.filter((a) => !res.has(a.merchantId) && res.set(a.merchantId, 1))
    },

    // 下拉框出现隐藏时触发
    selectVisibleChange(event, item) {
      if (event && item.name == 'scene') {
        if (!this.formData.merchantId) {
          this.baseData.sceneCodeList = []
          this.getOptionsList()
        } else {
          let arr = []
          // 拉接口获取当前流量方已开通的业务类型 将业务类型选项置为disabled
          getBusinessTypeByDiversionMerchant({ merchantId: this.formData.merchantId }).then(res => {
            JSON.parse(JSON.stringify(sceneCodeList)).forEach(val => {
              if (res?.includes(val.confId)) {
                val.disabled = true
              } else {
                val.disabled = false
              }
              arr.push(val)
            })
            this.baseData.sceneCodeList = arr
            this.getOptionsList()
          })
        }
      }
      if (event && item.name == 'merchantId') {
        if (this.merchantNameKeyword) {
          this.merchantNameKeyword = ''
          this.storeCurrentPage = 1
          let index = this.trafficsideForm?.findIndex(v => v.name == 'merchantId')
          this.$set(this.trafficsideForm[index], 'options', [])
          this.get4SStoreList()
        }
      }
      if (event && item.name == 'firstFlowMerchantId') {
        if (this.firstFlowMerchantNameKeyword) {
          this.firstFlowMerchantNameKeyword = ''
          this.firstCurrentPage = 1
          let index = this.trafficsideForm?.findIndex(v => v.name == 'firstFlowMerchantId')
          this.$set(this.trafficsideForm[index], 'options', [])
          this.get4sAndServiceMerchantList('first')
        }
      }
      if (event && item.name == 'flowChannelMerchantId') {
        if (this.flowChannelMerchantNameKeyword) {
          this.flowChannelMerchantNameKeyword = ''
          this.flowCurrentPage = 1
          let index = this.trafficsideForm?.findIndex(v => v.name == 'flowChannelMerchantId')
          this.$set(this.trafficsideForm[index], 'options', [])
          this.get4sAndServiceMerchantList('flow')
        }
        }
    },
    getBaseData() { 
      this.baseData = {
        sceneCodeList: JSON.parse(JSON.stringify(sceneCodeList)),
        invoiceTaxRateList: invoiceTaxRateList,
        cooperationModeList: cooperationModeList,
        paymentMethodList: paymentMethodList,
        partnerClassifyList: partnerClassifyList,
        subBusinessList: subBusinessList
      }
      this.getOptionsList()
    },
    getOptionsList() {
      this.trafficsideForm.forEach((item, index) => {
        if (item.type === 'select') {
          // 编辑 流量方和业务类型不可操作
          if (this.addOrEdit == 'edit') {
            if (item.name == 'scene') {
              this.$set(this.trafficsideForm[index], 'disabled', true)
            } else {
              this.$set(this.trafficsideForm[index], 'disabled', false)
            }
          }
          this.$set(this.trafficsideForm[index], 'options', this.baseData[item.optionsName])
        }
      })
      this.tableColumn.forEach((item, index) => {
        if (item.type === 'select' || item.type === 'checkbox') {
          this.$set(this.tableColumn[index], 'options', this.baseData[item.optionsName])
        }
      })
      this.$forceUpdate()
    },
    // 表头添加必填标识
    addRedStar(h, { column }) {
      if (column.label == '服务商名称' || column.label == '子业务') {
        // 需要加红*的表头
        return [
          h('span', { style: 'color: #F56C6C' }, '*'),
          h('span', ' ' + column.label)
        ]
      } else {
        // 不需要加红*的表头
        return [
          h('span', ' ' + column.label)
        ]
      }
    },
    // 添加三级服务商
    addTableRow(item) {
      this.formData[item].push({
        merchantId: '',
        serviceProviderType: '',
        subBusiness: [],
        contractStipulateRate: '',
        contractActualRate: '',
        contractOwnerMerchantId: '',
        contractCode: ''
      })
      this.$nextTick(() => {
        this.$refs['merchantGroundingForm'].clearValidate('marketServiceList')
      })
    },
    // 删除三级服务商
    deleteTableRow(item, index) {
      this.formData[item].splice(index, 1)
    },
    // 合同编码输入限制 只可输入数字 字母 特殊符号
    handleInput(e, formDataItem, item, index) {
      if (formDataItem) {
        this.$set(this.formData[formDataItem][index], item, e.replace(/[^a-zA-Z0-9!@#$%^&*()_+-={},./<>\[.*\]?'"!……&*()——【】‘;:”“’。,、?|\\\/]/g, ''))
      } else {
        this.$set(this.formData, item, e.replace(/[^a-zA-Z0-9!@#$%^&*()_+-={},./<>\[.*\]?'"!……&*()——【】‘;:”“’。,、?|\\\/]/g, ''))
      }
    },
    // 保存
    submit() {
      this.$refs['merchantGroundingForm'].validate((valid) => {
        let _this = this
        if (valid) {
          let _formData = _this.formData
          let serviceList = []
          // 一级服务商
          let firstServiceMerchantIndex = this.baseData.serviceList.findIndex(item => item.merchantId == _formData.firstServiceMerchantId)
          let firstServiceMerchantInfo = firstServiceMerchantIndex > -1 ? this.baseData.serviceList[firstServiceMerchantIndex] : {}
          if (_formData?.firstServiceMerchantId) serviceList.push({
            ...firstServiceMerchantInfo,
            contractInfo: {
              serviceMerchantId: _formData.firstServiceMerchantId,
              serviceMerchantName: firstServiceMerchantInfo?.merchantName,
              serviceProviderLevel: 1
            }
          })
          // 二级服务商
          let secondServiceMerchantIndex = this.baseData.serviceList.findIndex(item => item.merchantId == _formData.secondServiceMerchantId)
          let secondServiceMerchantInfo = secondServiceMerchantIndex > -1 ? this.baseData.serviceList[secondServiceMerchantIndex] : {}
          if (_formData?.secondServiceMerchantId) serviceList.push({
            ...secondServiceMerchantInfo,
            contractInfo: {
              serviceMerchantId: _formData.secondServiceMerchantId,
              serviceMerchantName: secondServiceMerchantInfo?.merchantName,
              serviceProviderLevel: 2
            }
          })
          // 三级服务商(营销)
          _formData.marketServiceList.forEach(item => {
            _this.getSubParamsItem(serviceList, item, 3)
          })
          // 三级服务商(安装)
          _formData.installServiceList.forEach(item => {
            _this.getSubParamsItem(serviceList, item, 4)
          })
          // 流量方下拉框选中对象数据
          let shopInfoIndex = this.trafficsideForm.findIndex(item => item.name == 'merchantId')
          let shopInfo = this.trafficsideForm[shopInfoIndex]?.options?.find(item => item['merchantId'] == _formData?.merchantId)
          // 一级流量方下拉框选中对象数据
          let firstFlowMerchantInfoIndex = this.trafficsideForm.findIndex(item => item.name == 'firstFlowMerchantId')
          let firstFlowMerchantInfo = this.trafficsideForm[firstFlowMerchantInfoIndex]?.options?.find(item => item['merchantId'] == _formData?.firstFlowMerchantId)
          // 流量通道方下拉框选中对象数据
          let flowChannelMerchantInfoIndex = this.trafficsideForm.findIndex(item => item.name == 'flowChannelMerchantId')
          let flowChannelMerchantInfo = this.trafficsideForm[flowChannelMerchantInfoIndex]?.options?.find(item => item['merchantId'] == _formData?.flowChannelMerchantId)
          // 园区店下拉框选中对象数据
          let yqMerchantIndex = this.trafficsideForm.findIndex(item => item.name == 'yqMerchantId')
          let yqMerchantInfo = this.trafficsideForm[yqMerchantIndex]?.options?.find(item => item['merchantId'] == _formData?.yqMerchantId)
          // 合同归属下拉框选中对象
          let contractOwnerMerchantIndex = this.trafficsideForm.findIndex(item => item.name == 'contractOwnerMerchantId')
          let contractOwnerMerchantInfo = this.trafficsideForm[contractOwnerMerchantIndex]?.options?.find(item => item['merchantId'] == _formData?.contractOwnerMerchantId)
          // 收入所属公司下拉框选中对象
          let incomeOwnerMerchantInfo = this.baseData?.contractAttributionList?.find(item => item['merchantId'] == _formData?.incomeOwnerMerchantId)
          let params = {
            flag: this.addOrEdit == 'edit' ? 0 : this.addOrEdit == 'add' ? 1 : '', // 新增修改标识(0:修改,1:新增)
            scene: _formData?.scene, // 业务类型
            shopInfo: shopInfo,
            shopContractInfo: {
              merchantId: _formData?.merchantId, // 流量方商户号
              merchantName: shopInfo?.merchantName, // 流量方商户名称
              scene: _formData?.scene,
              firstFlowMerchantInfo, // 一级流量方
              flowChannelMerchantInfo, // 流量通道方,
              cooperationModel: _formData?.cooperationModel, // 合作模式
              paymentMethod: _formData?.paymentMethod, // 支付方式
              contractStipulateRate: _formData?.contractStipulateRate, // 合同发票费率
              contractActualRate: _formData?.contractActualRate, // 实际发票费率
              contractOwnerMerchantId: _formData?.contractOwnerMerchantId, // 合同归属商户号
              contractOwnerMerchantName: contractOwnerMerchantInfo?.merchantName, // 合同归属商户名称
              contractCode: _formData?.contractCode, // 合同编码
              incomeOwnerMerchantId: _formData?.incomeOwnerMerchantId, // 收入所属公司 商户号
              incomeOwnerMerchantName: incomeOwnerMerchantInfo?.merchantName, // 收入所属公司 商户名称
              incomeOwnerMerchantAccount: incomeOwnerMerchantInfo?.merchantAccount, // 收入所属公司 商户名称+账号后4位
              generalTaxpayer: _formData?.generalTaxpayer == 'Y' ? 1 : 0, // 是否为一般纳税人
            },
            yqMerchant: {
              ...yqMerchantInfo,
              yqMerchantId: yqMerchantInfo?.merchantId,
              yqMerchantName: yqMerchantInfo?.merchantName
            },
            serviceList
          }
          if (this.showLoading) return
          this.showLoading = true
          merchantOnShelf({json: JSON.stringify(params)}).then(() => {
            this.showLoading = false
            _this.$message.success('保存成功')
            _this.$router.replace({name: 'merchantManage'})
          }).catch(() => {
            this.showLoading = false
          })
        }
      })
    },
    getSubParamsItem(serviceList, item, level) {
      let selectMerchant = this.tableColumn[0]?.options.find(v => v[this.tableColumn[0].optionsId] == item.merchantId)
      // 合同归属下拉框选中对象
      let contractOwnerMerchantIndex = this.tableColumn.findIndex(v => v.name == 'contractOwnerMerchantId')
      let contractOwnerMerchantInfo = this.tableColumn[contractOwnerMerchantIndex]?.options?.find(v => v['merchantId'] == item?.contractOwnerMerchantId)
      return serviceList.push({
        ...selectMerchant,
        contractInfo: {
          serviceMerchantId: item.merchantId,
          serviceMerchantName: selectMerchant?.merchantName,
          serviceProviderType: item.serviceProviderType,
          serviceProviderLevel: level,
          subBusiness: this.formData.scene == 0 ?item.subBusiness.join(',') : [], // 非会员权益/升级场景 无子业务
          contractStipulateRate: item.contractStipulateRate,
          contractActualRate: item.contractActualRate,
          contractOwnerMerchantId: item.contractOwnerMerchantId,
          contractOwnerMerchantName: contractOwnerMerchantInfo?.merchantName,
          contractCode: item.contractCode
        },
      })
    },
    // 取消
    navBack() {
      this.$router.replace({name: 'merchantManage'})
    }
  }
}
</script>
<style lang="scss" scoped>
.merchant-grounding-form{
  background: #ffffff;
  padding-bottom: 20px;
  .title {
    height: 50px;
    line-height: 50px;
    padding: 0 20px;
    border-bottom: 1px solid #E5E5E5;
    font-size: 16px;
    font-weight: 600;
    color: #333333;
    margin-bottom: 20px;
  }
  .edit-table{
    width: 98%;
  }
  .footer {
    text-align: center;
  }
  ::v-deep .el-table .cell{
    padding: 0 0;
  }
  ::v-deep .edit-table .el-table__body-wrapper .el-form-item__content{
    padding: 0 8px;
  }
}
::v-deep .el-form-item__error {
  padding-top: 2px;
  z-index: 9999999;
  position: static;
  text-align: left;
}
::v-deep .el-select__caret:first-child::before {
  content: "\e6e1";
}
::v-deep .el-select .is-focus {
  .el-select__caret:first-child {
    transform: rotateZ(0deg);
  }
}
</style>