<!-- 供应商信息管理--新增联系人 -->
<template>
  <div>
    <dialog-edit ref="allEditClearValid" :visible.sync="dialogVisible" :title="$t('enterpriseInfoManage.supplierInfoManage.contactMaintain')"
                 @onSaved="generateAccount"
                 :form="formValue" :editModel="editModel" :api="api" labelWidth="110px" :formRules="formRules" :autoSave="autoSave" :appendToBody="true"
                 :menuCode="menuId" savePerms="enterSave" :loading="formValue.loading">
      <div class="contactTitle">
        <span class="title-text">{{$t('enterpriseInfoManage.supplierInfoManage.enterpriseInfo')}}</span>
      </div>
      <div class="contact">
        <el-divider></el-divider>
      </div>
      <el-row :gutter="10">
        <el-col :xs="it.cols[0]" :sm="it.cols[1]" :md="it.cols[2]" :lg="it.cols[3]" v-for="(it, ind) in supplierList" :key="ind">
          <el-form-item v-if="it.showFlag" :label="it.label" :label-width="it.labelWidth"  :prop="it.prop" :show-message="it.showMessage"
                        class="dymfromItem">
            <component :is="it.componentName"
                       :store="it.store"
                       :maxlength="it.maxlength"
                       :dictTypeCode="it.dictTypeCode"
                       :disabled="it.disabled"
                       :placeholder="(editModel === 'V' || it.disabled) ? '' : it.placeholder"
                       :multiple="it.multiple"
                       :name="it.name"
                       :reload="it.reload"
                       :tagCode.sync="it.tagCode"
                       :value="it.value"
                       v-bind="it.attrs"
                       v-model.trim="formValue[it.value]"
                       @blur="blurDymFormSelect(it.value, formValue[it.value])"
                       @selectChanged="dymFormSelectChanged">
            </component>
          </el-form-item>
        </el-col>
      </el-row>
      <div class="contactTitle">
        <span class="title-text">{{$t('enterpriseInfoManage.supplierInfoManage.contactInfos')}}</span>
        <auth-button size="medium" type="primary" :label="$t('common.create')" icon="iconfont iconxinzeng" @click="addData"
                     :menuCode="menuId" perms="enterCreate" :openPerms="true"></auth-button>
      </div>

      <div class="contact">
        <el-divider></el-divider>
      </div>
      <el-card v-for="(item, index) in formValue.contactArray" :key="index" class="contactContent">
        <el-row :gutter="10">
          <el-col :xs="24" :sm="24" :md="24" :lg="8">
            <el-form-item :label="$t('enterpriseInfoManage.supplierInfoManage.contact')" :prop="'contactArray.' + index + '.employeeName'"
                          :rules="formRules.employeeName">
              <el-input type="text" v-model.trim="item.employeeName" maxlength="100" :placeholder="$t('common.pleaseInput')" class="dymForm"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="8">
            <el-form-item :label="$t('enterpriseInfoManage.supplierInfoManage.contactType')"
                          :prop="'contactArray.' + index + '.employeeType'"
                          :rules="formRules.employeeType">
<!--              <el-input type="text" :disabled="true" v-model="item.contactType" class="dymForm"></el-input>-->
              <select-dictionary v-model="item.employeeType" dictTypeCode="ZD0140"
                                 :store="$store"
                                 :multiple="false"
                                 name="employeeType"
                                 @selectChanged="employeeTypeSelectChanged"
                                 :bufferParams="{ index: index }"
                                 :exclude="contactTypeexclueds"
                                 :reload="contactTypereload"></select-dictionary>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="8">
            <el-form-item :label="$t('enterpriseInfoManage.supplierInfoManage.sex')" :prop="'contactArray.' + index + '.gender'"
                          :rules="formRules.sex">
              <select-dictionary v-model="item.gender" dictTypeCode="ZD0031" :store="$store"></select-dictionary>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :xs="24" :sm="24" :md="24" :lg="8">
            <el-form-item :label="$t('enterpriseInfoManage.supplierInfoManage.mobilePhone')" :prop="'contactArray.' + index + '.telephone'"
                          :rules="[{ required: true, message: $t('regMsg.enterpriseInfoManage.mobilePhone'), trigger: 'blur' },
                                  { validator: mobilePhoneCheck, trigger: 'blur' }]">
              <el-input type="text" v-model.trim="item.telephone" maxlength="11" :placeholder="$t('common.pleaseInput')" class="dymForm"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="8">
            <el-form-item :label="$t('enterpriseInfoManage.supplierInfoManage.weChat')" :prop="'contactArray.' + index + '.weixin'"
                          :rules="[{ required: item.qq === undefined || item.qq === '', message: $t('regMsg.enterpriseInfoManage.weChat'), trigger: 'change' },
                                  {validator: weChatCheck, trigger: 'blur', obj: item}]">
              <el-input type="text" v-model.trim="item.weixin" maxlength="50" :placeholder="$t('common.pleaseInput')" class="dymForm"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="8">
            <el-form-item :label="$t('enterpriseInfoManage.supplierInfoManage.QQ')" :prop="'contactArray.' + index + '.qq'"
                          :rules="[{ required: item.weixin === undefined || item.weixin === '', message: $t('regMsg.enterpriseInfoManage.QQ'), trigger: 'change' },
                                  {validator: QQCheck, trigger: 'blur', obj: item}]">
              <el-input type="text" v-model.trim="item.qq" maxlength="20" :placeholder="$t('common.pleaseInput')" class="dymForm"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <!--<dym-form :componentList="contactList" :formValue="item"></dym-form>-->
        <el-row :gutter="10">
          <el-col :xs="24" :sm="24" :md="24" :lg="8">
            <el-form-item :label="$t('enterpriseInfoManage.supplierInfoManage.department')">
              <select-department v-model="item.deptId" :placeholder="$t('common.pleaseSelect')" :parasObj="{ index: index }"
                                 :queryParas="departmentQueryParas" name="deptInfo" :reload="reloaddeptId"
                                 :allowCreate="true" @selectChanged="contactListSelectChanged"></select-department>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="8">
            <el-form-item :label="$t('enterpriseInfoManage.supplierInfoManage.position')">
              <el-input type="text" v-model.trim="item.position" maxlength="100" :placeholder="$t('common.pleaseInput')" class="dymForm"></el-input>
            </el-form-item>
          </el-col>
          <!--保理负责事项-->
          <el-col :xs="24" :sm="24" :md="24" :lg="8">
            <el-form-item :label="$t('enterpriseInfoManage.supplierInfoManage.responsibleMatter')">
<!--              <select-dictionary  v-model="item.factoringResponsibility" dictTypeCode="factor_responsibility"></select-dictionary>-->
              <select-dict-dis :disabled="item.forbiddenStatus" v-model="item.factoringResponsibility" :disabledOptions="item.disabledOptions" :reload="item.reload"
                               :dictTypeCode="'factor_responsibility'" name="forbiddenStatusDict"
                               :multiple="true" :placeholder="$t('common.pleaseSelect')" @selectChanged="forbiddenStatusChange(arguments, item, index)">
              </select-dict-dis>
            </el-form-item>
          </el-col>
        </el-row>
        <span class="title-text1">{{$t('enterpriseInfoManage.supplierInfoManage.accountInfos')}}</span>
<!--        <div class="contact">-->
<!--          <el-divider></el-divider>-->
<!--        </div>-->
        <!--是否生成账号-->
        <el-row :gutter="10">
          <el-col :xs="24" :sm="24" :md="24" :lg="8">
            <el-form-item :label="$t('enterpriseInfoManage.supplierInfoManage.isGenerateAccount')" :prop="'contactArray.' + index + '.cenerateType'"
                          :rules="formRules.isGenerateAccount">
              <select-dictionary v-model="item.cenerateType" dictTypeCode="ZD0003"
                                 name="cenerateType" :bufferParams="item" :store="$store" @selectChanged="isGenerateAccountChange"></select-dictionary>
            </el-form-item>
          </el-col>
          <!--账号类型-->
          <el-col :xs="24" :sm="24" :md="24" :lg="8">
            <el-form-item :label="$t('enterpriseInfoManage.supplierInfoManage.accountType')" :prop="'contactArray.' + index + '.employeeAccountType'"
                          :rules="[{ required: item.cenerateType !== '0', message: $t('regMsg.enterpriseInfoManage.accountType'), trigger: 'change'}]">
              <select-dictionary v-model="item.employeeAccountType" dictTypeCode="create_account_type" name="employeeAccountType"
                                 :placeholder="(item.cenerateType === '0') ? '' : $t('common.pleaseSelect')"
                                 :bufferParams="item" :store="$store" :disabled="item.cenerateType === '0'" @selectChanged="accountTypeChange"></select-dictionary>
            </el-form-item>
          </el-col>
          <!--关联主账号-->
          <el-col :xs="24" :sm="24" :md="24" :lg="8">
            <el-form-item :label="$t('enterpriseInfoManage.supplierInfoManage.relevanceAccount')" :prop="'contactArray.' + index + '.mainAccountId'"
                          :rules="[{ required: (item.employeeAccountType === '2'), message: $t('regMsg.enterpriseInfoManage.relevanceAccount'), trigger: 'change'}]">
              <select-account v-model="item.mainAccountId"
                              :queryParas="accountQuery1(item)"
                              :reload="relevanceAccountReload"
                              :placeholder="(item.cenerateType === '0' || item.employeeAccountType === '1' ||  item.employeeAccountType === '3') ? '' : $t('common.pleaseSelect')"
                              :disabled="item.cenerateType === '0' || item.employeeAccountType === '1' ||  item.employeeAccountType === '3'">
              </select-account>
            </el-form-item>
          </el-col>
        </el-row>
        <!--关联已有账号-->
        <el-row :gutter="10">
          <el-col :xs="24" :sm="24" :md="24" :lg="8">
            <el-form-item :label="$t('enterpriseInfoManage.supplierInfoManage.relevanceExistingAccount')" :prop="'contactArray.' + index + '.exitAccountId'">
              <select-account v-model="item.exitAccountId"
                              :disabled="item.cenerateType !== '0'"
                              :placeholder="(item.cenerateType !== '0') ? '' : $t('common.pleaseSelect')"
                              :reload="relevanceAccountReload"
                              :queryParas="accountQuery2(item)">
              </select-account>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="16">
            <auth-button size="medium" type="danger" icon="iconfont iconshanchu" :label="$t('common.del')" @click="delData(index)" class="delBtn" v-if="formValue.contactArray.length > 1"
                         :menuCode="menuId" perms="enterDelete" :openPerms="true"></auth-button>
          </el-col>
        </el-row>
      </el-card>
    </dialog-edit>
    <generate-account-edit :visible.sync="showFlag" :editModel="showModel" :form="accountForm" :accountItem="accountItem" :regMsg="regMsg"></generate-account-edit>
  </div>
</template>

<script>
import { DialogEdit, dymForm, SelectDictionary, authButton, validate, regExp } from 'newepoch-components'
import BaseEdit from '@/pages/baseEditPage'
import supplierInfoManageApi from '@/api/enterpriseInfoManage/supplierInfoManageApi.js'
import GenerateAccountEdit from '@/pages/enterpriseInfoManage/supplierInfoManage/generateAccountEdit.vue'
// import SelectArea from '@/components/element/enterpriseInfoManage/SelectArea.vue'
import SelectDepartment from '@/components/element/enterpriseInfoManage/SelectDepartment.vue'
import SelectAccount from '@/components/element/enterpriseInfoManage/SelectAccount.vue'
import enterpriseName from '@/components/element/enterpriseInfoManage/enterpriseName.vue'
import selectDictDis from '@/components/element/enterpriseInfoManage/selectDictDis.vue'
import coreEnterpriseInfoManageApi from '@/api/enterpriseInfoManage/coreEnterpriseInfoManageApi'
import SelectWorkTemplateProPlatModify from '@/components/element/enterpriseInfoManage/platElement/SelectWorkTemplateProPlatModify'
import SelectWorkTemplatePlatModify from '@/components/element/enterpriseInfoManage/platElement/SelectWorkTemplatePlatModify'
export default {
  extends: new BaseEdit(),
  name: 'contactMaintainEdit',
  components: {
    DialogEdit,
    dymForm,
    SelectDictionary,
    SelectWorkTemplatePlatModify,
    SelectAccount,
    SelectDepartment,
    GenerateAccountEdit,
    authButton,
    enterpriseName,
    selectDictDis,
    SelectWorkTemplateProPlatModify
  },
  data () {
    return {
      showFlag: false,
      autoSave: false,
      showModel: '',
      accountForm: {},
      accountItem: [],
      // 联系人类型
      employeeTypeList: [],
      // 重新加载部门
      reloaddeptId: false,
      // 关联账号重新加载
      relevanceAccountReload: false,
      // 保存按钮禁用
      showDisabled: true,
      formRulesFlag: false,
      // 保存加载标识
      loading: false,
      contactTypeexclueds: [],
      contactTypereload: false,
      // 企业标签
      enterpriseTagDTOList: [],
      // 区域选择
      infoRegionList: [],
      // 项目公司-合同
      accountProjectPlateContractRelDTOList: [],
      // accountTerritoryDtoList: [],
      // 联系人、账号信息
      contactItem: {
        employeeAccountType: ''
      },
      supplierList: [
        // 企业名称
        {
          value: 'enterpriseName',
          disabled: false,
          showFlag: true,
          componentName: enterpriseName,
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.enterpriseName,
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          cols: [24, 24, 24, 8],
          name: 'enterpriseName',
          labelWidth: '135px',
          prop: 'enterpriseName'
        },
        // 统一社会信用代码
        {
          value: 'creditCode',
          disabled: false,
          showFlag: true,
          componentName: 'el-input',
          maxlength: 18,
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.unifiedSocialCreditCode,
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          cols: [24, 24, 24, 8],
          labelWidth: '135px',
          prop: 'creditCode'
        },
        // 企业标签
        {
          value: 'enterpriseTag',
          disabled: true,
          showFlag: true,
          componentName: 'SelectDictionary',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.enterpriseTag,
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          cols: [24, 24, 24, 8],
          dictTypeCode: 'ZD0040',
          name: 'enterpriseTag',
          multiple: true,
          labelWidth: '135px',
          prop: 'enterpriseTag'
        },
        // 核心企业简称
        {
          value: 'shortName',
          disabled: false,
          showFlag: true,
          componentName: 'el-input',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.ShortName,
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          cols: [24, 24, 24, 8],
          labelWidth: '135px',
          prop: 'shortName'
        },
        // 区域
        {
          value: 'area',
          disabled: false,
          showFlag: false,
          filterable: true,
          componentName: SelectWorkTemplatePlatModify,
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.returnAddressEdit.area,
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseSelect,
          cols: [24, 24, 24, 8],
          labelWidth: '135px',
          name: 'area',
          reload: false,
          prop: 'area',
          attrs: {
            attrsQueryParas: { enterpriseId: '' },
            isShowThree: true,
            isShowAll: true,
            isload: true
          }
        },
        // 核心企业简码
        {
          value: 'enterpriseCode',
          disabled: false,
          showFlag: true,
          name: 'enterpriseCode',
          componentName: 'el-input',
          maxlength: 20,
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.Code,
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          cols: [24, 24, 24, 8],
          labelWidth: '135px',
          prop: 'enterpriseCode'
        }
      ],
      contactList: [
        // 部门(下拉兼输入框组件)
        {
          value: 'deptId',
          disabled: false,
          componentName: SelectDepartment,
          allowCreate: true,
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.department,
          width: '50%',
          queryName: ['keyId'],
          queryValue: ['enterpriseId'],
          reload: this.reloaddeptId,
          name: 'deptId',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseSelect,
          cols: [24, 24, 24, 8],
          prop: 'deptId'
        },
        // 职位
        {
          value: 'position',
          disabled: false,
          componentName: 'el-input',
          maxlength: 100,
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.position,
          width: '50%',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          cols: [24, 24, 24, 8],
          prop: 'position'
        },
        // 负责事项
        {
          value: 'factoringResponsibility',
          disabled: false,
          componentName: 'SelectDictionary',
          store: this.$store,
          dictTypeCode: 'factor_responsibility',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.responsibleMatter,
          width: '50%',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          cols: [24, 24, 24, 8],
          prop: 'factoringResponsibility'
        }
      ],
      formRules: {
        // 企业名称
        enterpriseName: [{
          required: true,
          message: this.regMsg.enterpriseInfoManage.enterpriseName,
          trigger: 'change'
        }, { validator: validate.enterpriseNameValidate, trigger: 'blur' }],
        // 统一社会信用代码
        creditCode: [{
          required: true,
          message: this.regMsg.enterpriseInfoManage.creditCode,
          trigger: 'blur'
        }, { validator: validate.creditCodeValidate, trigger: 'blur' }],
        // 企业标签
        enterpriseTag: [{
          required: true,
          message: this.regMsg.enterpriseInfoManage.selectEnterpriseTag,
          trigger: 'blur'
        }],
        // 核心企业简称
        shortName: [
          {
            required: false,
            message: this.regMsg.enterpriseInfoManage.coreEnterpriseShortName,
            trigger: 'blur'
          },
          {
            validator: this.shortNameLengthCheck, trigger: 'blur'
          }
        ],
        // 核心企业简码
        enterpriseCode: [{
          required: false,
          message: this.regMsg.enterpriseInfoManage.coreEnterpriseCode,
          trigger: 'blur'
        }, { validator: validate.enterpriseCodeValidate, trigger: 'blur' }],
        // 联系人
        employeeName: [{
          required: true,
          message: this.regMsg.enterpriseInfoManage.contact
        }, {
          validator: validate.nameValidate,
          trigger: 'blur'
        }],
        // 联系人类型
        employeeType: [{
          required: true,
          message: this.regMsg.enterpriseInfoManage.contactType,
          trigger: 'blur'
        }],
        // 性别
        sex: [{ required: true, message: this.regMsg.enterpriseInfoManage.sex, trigger: 'change' }],
        // 手机号码
        mobilePhone: [{
          required: true,
          message: this.regMsg.enterpriseInfoManage.mobilePhone
        }, {
          validator: validate.mobilePhoneValidate,
          trigger: 'blur'
        }],
        // 是否生成账号
        isGenerateAccount: [{
          required: true,
          message: this.regMsg.enterpriseInfoManage.isGenerateAccount,
          trigger: 'change'
        }],
        email: [{
          required: true,
          message: this.regMsg.enterpriseInfoManage.mobilePhone
        }, {
          validator: validate.emailValidate,
          trigger: 'blur'
        }]
      }
    }
  },
  computed: {
    accountQuery1 () {
      return (item) => {
        return { enterpriseId: this.formValue.enterpriseId, employeeAccountType: '1', enterpriseType: this.formValue.tagCode === '60' ? (!item.employeeType || item.employeeType.length === 0) ? this.formValue.tagCode : item.employeeType : this.formValue.tagCode }
      }
    },
    accountQuery2 () {
      return (item) => {
        return { enterpriseId: this.formValue.enterpriseId, enterpriseType: this.formValue.tagCode === '60' ? (!item.employeeType || item.employeeType.length === 0) ? this.formValue.tagCode : item.employeeType : this.formValue.tagCode }
      }
    },
    formValue: {
      get: function () {
        return this.form
      },
      set: function (value) {
        this.$emit('update:form', value)
      }
    },
    api () {
      return supplierInfoManageApi.supplierInfoApi
    },
    departmentQueryParas () {
      return { keyId: this.formValue.enterpriseId }
    }
  },
  methods: {
    forbiddenStatusChange (a, b, index) {
      if (a[0]) {
        if (a[0].some(it => it === '1')) {
          b.factoringResponsibility = ['1']
          this.$set(b, 'disabledOptions', [{ dictId: '1' }])
        } else {
          this.$set(b, 'disabledOptions', [])
        }
        this.$nextTick(() => {
          this.$set(b, 'reload', !b.reload)
        })
      }
    },
    // 简码长度验证
    shortNameLengthCheck (rule, value, callback) {
      if (value) {
        if (value.length > 20) {
          callback(new Error(this.regMsg.enterpriseInfoManage.shortNamecheckLabel))
        } else {
          callback()
        }
      } else {
        callback()
      }
    },
    // 联系人类型
    employeeTypeSelectChanged (key, name, selectItemts, bufferParams) {
      // this.employeeTypeList = key
      this.form.contactArray[bufferParams.index].employeeTypeList = [key]
      this.$set(this.form.contactArray[bufferParams.index], 'mainAccountId', '')
      this.$set(this.form.contactArray[bufferParams.index], 'exitAccountId', '')
      this.$nextTick(() => {
        this.$refs.allEditClearValid.$refs.editForm.clearValidate()
      })
      // if (key) {
      //   if (key === '150' || key === '60') {
      //     this.$set(this.formValue.contactArray[bufferParams.index], 'employeeAccountType', '3')
      //   }
      // }
      // if (key.length === 2) {
      //   let flag = true
      //   key.forEach(it => {
      //     if (it !== '150' && it !== '60') {
      //       flag = false
      //     }
      //   })
      //   if (flag) {
      //     this.$set(this.formValue.contactArray[bufferParams.index], 'employeeAccountType', '3')
      //   } else {
      //     this.formValue.contactArray[bufferParams.index].employeeAccountType = ''
      //     // this.$set(this.formValue.contactArray[bufferParams.index], 'employeeAccountType', '')
      //   }
      // }
    },
    contactListSelectChanged (val, name, data, obj) {
      if (name === 'deptInfo') {
        if (data) {
          this.$set(this.formValue.contactArray[obj.index], 'deptName', data.deptName)
          this.$set(this.formValue.contactArray[obj.index], 'deptId1', data.keyId)
        } else if (val && !data) {
          this.$set(this.formValue.contactArray[obj.index], 'deptId1', '')
          this.$set(this.formValue.contactArray[obj.index], 'deptName', val)
        }
      }
    },
    // 企业标签
    enterpriseTag (select) {
      let that = this
      if (!that.formValue.contactArray) {
        return
      }
      // that.enterpriseTagDTOList = []
      that.$store.dispatch('GetDictionary', { dictTypeCode: 'ZD0140' }).then(res => {
        that.formValue.contactArray.forEach(it => { it.contactType = '' })
        let contactTypeexclueds = res.filter(it => {
          let bl = !select.some(it1 => {
            if (typeof it1 === 'string') {
              return it1 === it.dictId
            } else {
              return it1.dictId === it.dictId
            }
          })
          // if (!bl) {
          //   that.enterpriseTagDTOList.push({ tagName: it.dictName, tagCode: it.dictId })
          // }
          return bl
        })
        that.$set(that, 'contactTypeexclueds', contactTypeexclueds)
        that.contactTypereload = !that.contactTypereload
      })
    },
    // 信用代码处理
    blurDymFormSelect (name, value) {
      let that = this
      if (name === 'creditCode') {
        if (value && value.length === 18) {
          supplierInfoManageApi.enterpriseInfo.getEnterpriseInfo({ creditCode: that.formValue.creditCode }).then(res => {
            if (res.data.length > 0) {
              if (res.data.length > 0 || res.data[0]) {
                that.$set(this.formValue, 'enterpriseName', res.data[0].enterpriseName)
                that.$set(this.formValue, 'enterpriseId', res.data[0].keyId)
                that.$set(this.formValue, 'creditCode', res.data[0].creditCode)
                that.$set(this.formValue, 'shortName', res.data[0].shortName)
                that.$set(this.formValue, 'enterpriseCode', res.data[0].enterpriseCode)
              }
            }
          })
        }
      }
    },
    // 组件数据处理
    dymFormSelectChanged (index, name, row) {
      let that = this
      if (name === 'enterpriseTag') {
        // let select = row.filter(it => !it.statusFlag || it.statusFlag !== '-1')
        that.enterpriseTagDTOList = []
        let contactType = []
        if (row.length > 0) {
          that.enterpriseTagDTOList.push({ tagName: row[0].dictName, tagCode: row[0].dictId })
          if (row[0].dictId === '140') {
            contactType.push('140')
          }
          if (row[0].dictId === '150') {
            contactType.push('150')
          }
          if (row[0].dictId === '60') {
            contactType = ['60', '61']
          }
        }
        that.enterpriseTag(contactType)
      } else if (name === 'area') {
        that.accountProjectPlateContractRelDTOList = []
        // that.accountTerritoryDtoList = []
        that.infoRegionList = []
        if (this.form.tagCode === '140') {
          that.infoRegionList = this.workInfoFun(row)
        } else {
          row.forEach(it => {
            if (this.form.tagCode === '150') {
              that.accountProjectPlateContractRelDTOList.push({
                contractId: it.value,
                contractName: it.label
              })
            } else if (this.form.tagCode === '140') {
              // that.infoRegionList.push({ areaId: it.areaId, areaName: it.areaName, coreEnterpriseId: it.coreEnterpriseId, coreEnterpriseName: it.coreEnterpriseName })
              // var a = {
              //   coreEnterpriseId: it.value,
              //   coreEnterpriseName: it.label,
              //   children: []
              // }
              // if (it.children) {
              //   it.children.forEach(it1 => {
              //     var b = {
              //       areaId: it1.value,
              //       areaName: it1.label,
              //       children: []
              //     }
              //     if (it1.children) {
              //       it.children.forEach(it2 => {
              //         b.children.push({
              //           contractId: it2.value,
              //           contractName: it2.label
              //         })
              //       })
              //     }
              //     a.children.push(b)
              //   })
              // }
              // that.accountTerritoryDtoList.push(a)
              // that.accountProjectPlateContractRelDTOList
            }
          })
        }
      } else if (name === 'enterpriseName') {
        if (row.length !== 0) {
          let { enterpriseName, keyId, creditCode, shortName, enterpriseCode } = row[0]
          this.$set(this.formValue, 'enterpriseName', enterpriseName)
          this.$set(this.formValue, 'enterpriseId', keyId)
          this.$set(this.formValue, 'creditCode', creditCode)
          this.$set(this.formValue, 'shortName', shortName)
          this.$set(this.formValue, 'enterpriseCode', enterpriseCode)
        } else {
          this.formValue.contactArray.forEach(it => {
            this.$set(it, 'enterpriseId', '')
          })
        }
      }
    },
    // 供应商
    workInfoFun (data1) {
      let list = []
      data1.some(item => {
        let data = {}
        if (item.value === '-1') {
          data.coreEnterpriseId = item.value
          data.coreEnterpriseName = item.label
          list.push(data)
          return true
        } else {
          data.coreEnterpriseId = item.value
          data.coreEnterpriseName = item.label
          if (item.children && item.children.length > 0) {
            item.children.some(it => {
              if (it.value === '-1') {
                data.areaId = it.value
                data.areaName = it.label
                list.push(data)
                return true
              } else {
                data.areaId = it.value
                data.areaName = it.label
                if (it.children && it.children.length > 0) {
                  it.children.some(it2 => {
                    data.contractId = it2.value
                    data.contractName = it2.label
                    list.push(data)
                  })
                } else {
                  list.push(data)
                }
              }
            })
          } else {
            list.push(data)
          }
        }
      })
      return list
    },
    isGenerateAccountChange (val, name, item, bufferParams) {
      this.$set(bufferParams, 'employeeAccountType', '')
      this.$set(bufferParams, 'mainAccountId', '')
      this.$set(bufferParams, 'exitAccountId', '')
      if (val === '0') {
      } else {
        let temp = this.checkTag()
        if (temp) {
          this.$set(bufferParams, 'employeeAccountType', '3')
        }
      }
      this.$nextTick(() => {
        if (this.$refs.allEditClearValid && this.$refs.allEditClearValid.$refs.editForm) {
          this.$refs.allEditClearValid.$refs.editForm.clearValidate()
        }
      })
    },
    accountTypeChange (val, name, item, bufferParams) {
      if (val === '1') {
        this.$set(bufferParams, 'mainAccountId', '')
        this.$set(bufferParams, 'exitAccountId', '')
      }
      if (val === '2') {
        // this.$set(bufferParams, 'mainAccountId', '')
        this.$set(bufferParams, 'exitAccountId', '')
      }
      if (val === '3') {
        this.$set(bufferParams, 'mainAccountId', '')
      }
    },
    // 新增按钮点击事件
    addData () {
      if (this.form.contactArray.length <= 5) {
        let temp = Object.assign({}, this.contactItem)
        if (this.checkTag()) {
          this.$set(temp, 'cenerateType', '1')
          this.$set(temp, 'employeeAccountType', '3')
        }
        this.formValue.contactArray.push(Object.assign({ enterpriseId: this.formValue.enterpriseId }, temp))
      } else {
      }
    },
    // 删除按钮
    delData (index) {
      if (this.form.contactArray.length > 1) {
        this.formValue.contactArray.splice(index, 1)
      }
    },
    // 手机号码校验
    mobilePhoneCheck: function (rule, value, callback) {
      if (!this.UtilJs.isEmpty(value)) {
        if (regExp.mobilePhone.test(value)) {
          callback()
        } else {
          callback(new Error(this.regMsg.enterpriseInfoManage.phone))
        }
      } else {
        callback()
      }
    },
    // 微信校验
    weChatCheck: function (rule, value, callback) {
      // let obj = rule.obj
      if (!this.UtilJs.isEmpty(value)) {
        if (regExp.weChat.test(value)) {
          callback()
        } else {
          callback(new Error(this.regMsg.enterpriseInfoManage.weixin))
        }
      } else {
        callback()
      }
      this.clearValidateField('qq', rule)
    },
    // 清空qq微信
    clearValidateField (checkType, rule) {
      var field = rule.field
      field = field.split('.')
      field[2] = checkType
      field = field.join('.')
      if (this.$refs.allEditClearValid && this.$refs.allEditClearValid.$refs.editForm) {
        this.$refs.allEditClearValid.$refs.editForm.clearValidate(field)
      }
    },
    // QQ校验
    QQCheck: function (rule, value, callback) {
      // let obj = rule.obj
      if (!this.UtilJs.isEmpty(value)) {
        if (regExp.QQ.test(value)) {
          callback()
        } else {
          callback(new Error(this.regMsg.enterpriseInfoManage.qq))
        }
      } else {
        callback()
      }
      this.clearValidateField('weixin', rule)
    },
    // 联系人校验
    // contactCheck: function (rule, value, callback) {
    //   if (!this.UtilJs.isEmpty(value)) {
    //     if (regExp.name.test(value)) {
    //       callback()
    //     } else {
    //       callback(new Error(this.regMsg.enterpriseInfoManage.name))
    //     }
    //   } else {
    //     callback(new Error(this.regMsg.enterpriseInfoManage.contact))
    //   }
    // },
    // enterpriseCodeCheck: function (rule, value, callback) {
    //   if (!this.UtilJs.isEmpty(value)) {
    //     if (regExp.letter.test(value)) {
    //       callback()
    //     } else {
    //       callback(new Error(this.regMsg.enterpriseInfoManage.coreEnterpriseCodeCheck))
    //     }
    //   } else {
    //     callback(new Error(this.regMsg.enterpriseInfoManage.coreEnterpriseCode))
    //   }
    // },
    // 生成账号
    generateAccount () {
      let $this = this
      // $this.loading = true
      this.$set($this.form, 'loading', true)
      let obj = Object.create({})
      // 联系人
      let employeeInputDtos = []
      // 企业
      let enterpriseDTO = Object.create({})
      $this.form.contactArray.forEach(it => {
        let item = {}
        item.employeeName = it.employeeName // 联系人
        item.employeeTypeList = it.employeeTypeList // it.employeeType // 联系人类型
        item.gender = it.gender // 性别
        item.telephone = it.telephone // 手机号码
        item.weixin = it.weixin // 微信号
        item.qq = it.qq // QQ号
        item.deptDTO = { deptId: it.deptId1, deptName: it.deptName } // 部门
        item.position = it.position // 职位
        item.factoringResponsibility = it.factoringResponsibility // 保理负责事项
        // item.factoringResponsibility = []
        // if (it.factoringResponsibility) {
        //   it.factoringResponsibility.forEach(s => {
        //     let attribute = {}
        //     attribute.attributeType = '3'
        //     attribute.attributeValue = s
        //     item.factoringResponsibility.push(attribute)
        //   })
        // }
        item.accountInputDTO = {
          cenerateType: it.cenerateType,
          employeeAccountType: it.employeeAccountType,
          exitAccountId: it.exitAccountId,
          mainAccountId: it.mainAccountId
        }
        employeeInputDtos.push(item)
      })
      // enterpriseDTO
      enterpriseDTO = {
        shortName: $this.formValue.shortName,
        enterpriseTagDTOList: $this.enterpriseTagDTOList,
        creditCode: $this.formValue.creditCode,
        enterpriseCode: $this.formValue.enterpriseCode,
        enterpriseName: $this.formValue.enterpriseName,
        infoRegionList: $this.infoRegionList // 区域
      }
      if (this.form.tagCode === '150') {
        enterpriseDTO = Object.assign(enterpriseDTO, { accountProjectPlateContractRelDTOList: $this.accountProjectPlateContractRelDTOList })
      } else if (this.form.tagCode === '140') {
        // enterpriseDTO = Object.assign(enterpriseDTO, { accountTerritoryDtoList: $this.accountTerritoryDtoList })
      }
      obj = { employeeInputDtos: employeeInputDtos, enterpriseDTO, userName: $this.$store.state.user.user.userName }
      coreEnterpriseInfoManageApi.contactMaintain.addEntemployee(obj).then(res => {
        if (res.success) {
          this.$emit('createSuccess')
          $this.warning(this.regMsg.enterpriseInfoManage.addEnterprise)
          $this.accountItem = []
          let jump = true
          res.data.forEach(it => {
            if (Object.keys(it).length !== 0) {
              let item1 = {}
              item1.employeeName = it.employeeName
              item1.accountName = it.accountName
              item1.password = it.password
              $this.accountItem.push(item1)
              jump = false
            }
          })
          if (jump) {
            $this.dialogVisible = false
          } else {
            $this.form.website = process.env.VUE_APP_LOGIN_URL
            $this.loading = false
            this.$set($this.form, 'loading', false)
            $this.showFlag = true
            $this.dialogVisible = false
          }
        }
      }).catch(() => {
        this.$set($this.form, 'loading', false)
      })
    },
    initVeiew () {
      this.$nextTick(() => {
        if (this.formValue.enterpriseId) {
          supplierInfoManageApi.enterpriseApi.basic({ enterpriseId: this.form.enterpriseId }).then(res => {
            if (res && res.data) {
              let enterpriseTag = []
              if (res.data.enterpriseTagDTOList) {
                res.data.enterpriseTagDTOList.forEach(it => {
                  enterpriseTag.push(it.tagCode.toString())
                })
              }
              let areas = []
              if (this.form.tagCode === '150') {
                this.infoRegionList = res.data.accountProjectPlateContractRelDTOList
                if (res.data.accountProjectPlateContractRelDTOList) {
                  res.data.accountProjectPlateContractRelDTOList.forEach(it => {
                    areas.push(it.contractId)
                  })
                }
              } else if (this.form.tagCode === '140') {
                this.infoRegionList = res.data.infoRegionList
                this.infoRegionList.forEach(it => {
                  areas.push([it.coreEnterpriseId, it.areaId])
                })
              }
              this.$set(this.formValue, 'area', areas)
              this.$set(this.formValue, 'enterpriseName', res.data.enterpriseName)
              this.$set(this.formValue, 'enterpriseCode', res.data.enterpriseCode)
              this.$set(this.formValue, 'creditCode', res.data.creditCode)
              this.$set(this.formValue, 'shortName', res.data.shortName)
              this.$set(this.formValue, 'contactArray', [{}])
              // this.$set(this.formValue, 'enterpriseTag', enterpriseTag)
              let contactType = []
              if (this.form.tagCode.toString() === '140') {
                contactType.push('140')
              }
              if (this.form.tagCode.toString() === '150') {
                contactType.push('150')
                this.supplierList[4].attrs.attrsQueryParas.enterpriseId = this.form.enterpriseId
              }
              if (this.form.tagCode.toString() === '60') {
                contactType = ['60', '61']
              }
              this.enterpriseTag(contactType)
              this.supplierList.forEach(it => {
                it.disabled = true
              })
              this.supplierList[4].reload = !this.supplierList[4].reload
            } else {
              this.supplierList.forEach(it => {
                it.disabled = true
              })
            }
            this.$nextTick(() => {
              if (this.$refs.allEditClearValid && this.$refs.allEditClearValid.$refs.editForm) {
                this.$refs.allEditClearValid.$refs.editForm.clearValidate()
              }
            })
          })
        } else {
          let dataList = Object.assign({}, this.contactItem)
          this.$set(this.formValue, 'contactArray', [dataList])
          // this.formValue.contactArray.push(Object.assign({}, this.contactItem))
          this.$set(this.formValue, 'enterpriseTag', [this.form.tagCode.toString()])
          this.$set(this.supplierList[0], 'tagCode', this.form.tagCode.toString())
          let contactType = []
          if (this.form.tagCode.toString() === '140') {
            contactType.push('140')
          }
          if (this.form.tagCode.toString() === '150') {
            contactType.push('150')
          }
          if (this.form.tagCode.toString() === '60') {
            contactType = ['60', '61']
          }
          // 防止没有挂载上就reload
          setTimeout(() => {
            this.$set(this.supplierList[4], 'reload', !this.supplierList[4].reload)
          }, 100)
          this.enterpriseTag(contactType)
          this.contactTypereload = !this.contactTypereload
        }
      })
    },
    checkTag (val = this.formValue.enterpriseTag) {
      let temp = false
      if (val) {
        let accoutype1 = []
        let accoutype2 = []
        // 判断赋值
        if (val.length === 1) {
          accoutype1 = val.filter(it => it === '60' || it === '150')
        }
        if (val.length === 2) {
          accoutype2 = val.filter(it => it === '60' || it === '150')
        }
        if (accoutype1.length === 1 || accoutype2.length === 2) {
          temp = true
        }
      }
      return temp
    }
  },
  watch: {
    visible (val) {
      if (val) {
        if (this.form.tagCode === '150') {
          this.supplierList[4].componentName = SelectWorkTemplateProPlatModify
          this.supplierList[4].label = this.$t('enterpriseInfoManage.supplierInfoManage.returnAddressEdit.hetong')
        } else if (this.form.tagCode === '140') {
          this.supplierList[4].componentName = SelectWorkTemplatePlatModify
          this.supplierList[4].label = this.$t('enterpriseInfoManage.supplierInfoManage.returnAddressEdit.area')
        }
        this.$set(this.formValue, 'enterpriseTag', [this.form.tagCode.toString()])
        this.initVeiew()
      } else {
        this.form.tagCode = ''
        this.$set(this.supplierList[0], 'tagCode', '')
        this.formValue.enterpriseId = ''
        // this.contactTypeexclueds = []
        this.infoRegionList = []
        delete this.formValue
      }
    },
    // 判断核心企业表格禁用
    'formValue.enterpriseTag' (val) {
      this.$nextTick(() => {
        if (val) {
          // 账号类型默认值 为核心企业 项目公司时默认普通账号
          let temp = this.checkTag(val)
          if (temp) {
            this.formValue.contactArray.forEach(item => {
              this.$set(item, 'employeeAccountType', '3')
              this.$set(item, 'cenerateType', '1')
            })
          }
          // let accoutype1 = []
          // let accoutype2 = []
          // // 判断赋值
          // if (val.length === 1) {
          //   accoutype1 = val.filter(it => it === '60' || it === '150')
          // }
          // if (val.length === 2) {
          //   accoutype2 = val.filter(it => it === '60' || it === '150')
          // }
          // if (accoutype1.length === 1 || accoutype2.length === 2) {
          //   this.formValue.contactArray.forEach(item => {
          //     this.$set(item, 'employeeAccountType', '3')
          //     this.$set(item, 'cenerateType', '1')
          //   })
          // }
          if (this.formValue.contactArray) {
            this.formValue.contactArray.forEach(it => {
              let types = val.filter(item => it.employeeType === item)
              if (types.length === 0) {
                this.$set(it, 'employeeType', '')
              }
            })
          }
          let len = val.filter(it => it === '60')
          let len1 = val.filter(it => it === '150' || it === '140')
          if (val.length === 1 && len.length === 1) {
            this.supplierList[4].showFlag = false
          } else if (len1.length > 0) {
            this.supplierList[4].showFlag = true
          } else {
            this.supplierList[4].showFlag = false
          }
          if (len.length > 0) {
            this.formRules.shortName[0].required = true
            this.formRules.enterpriseCode[0].required = true
          } else {
            this.formRules.shortName[0].required = false
            this.formRules.enterpriseCode[0].required = false
          }
        } else {
          this.$set(this.formValue, 'formRulesFlag', false)
          if (this.formValue.contactArray) {
            this.formValue.contactArray.forEach(it => {
              this.$set(it, 'employeeType', '')
            })
          }
        }
      })
    },
    // 'formValue.creditCode' (val) {
    //   if (val) {
    //     let that = this
    //     let len = 0
    //     len = val.length
    //     if (len === 18) {
    //       // let tagCode = ['1', '2', '3', '4', '3', '2', '1']
    //       let tagCode = []
    //       supplierInfoManageApi.enterpriseTag.getList({ creditCode: that.formValue.creditCode }).then(res => {
    //         if (res.data) {
    //           // let tagCode = []
    //           if (res.data.length > 0 || res.data[0]) {
    //             res.data[0].enterpriseTagDTOList.forEach(it => {
    //               tagCode.push(it.tagCode)
    //             })
    //           }
    //           if (that.formValue.enterpriseTag && that.formValue.enterpriseTag.length > 0) {
    //             that.formValue.enterpriseTag.forEach(item => {
    //               tagCode.push(item)
    //             })
    //           }
    //           // 数组去重
    //           tagCode = [...new Set(tagCode)]
    //           that.$set(that.formValue, 'enterpriseTag', tagCode)
    //           that.enterpriseTag(that.formValue.enterpriseTag)
    //           // 保存按钮禁用放开
    //           // that.showDisabled = false
    //         }
    //       })
    //     }
    //   }
    // },
    'formValue.enterpriseId' (val) {
      if (val) {
        if (!this.formValue.contactArray) {
          this.$set(this.formValue, 'contactArray', [])
          this.formValue.contactArray.push(Object.assign({}, this.contactItem))
        }
        this.formValue.contactArray.forEach(it => {
          this.$set(it, 'enterpriseId', this.formValue.enterpriseId)
        })
      } else {
        if (this.formValue.contactArray) {
          this.formValue.contactArray.forEach(it => {
            this.$set(it, 'enterpriseId', '')
          })
        }
      }
      // this.$set(this.contactList[0], 'reload', !this.reloaddeptId)
      this.reloaddeptId = !this.reloaddeptId
      this.relevanceAccountReload = !this.relevanceAccountReload
    }
  }
}
</script>

<style scoped lang="scss">
  @import '~@/styles/enterpriseInfoManage/supplierInfoManage/contactMaintainEdit.scss';
</style>
