<!-- 业务操作信息 -->
<template>
  <div>
    <search-form :componentList="searchList" :formValue.sync="searchForm"
                 @handleFilter="handleFilter" @clearParam="clearParam" slot="query"></search-form>
    <data-table :api="api" :headTitle="$t('enterpriseInfoManage.supplierInfoManage.contactHeadTitle')" :loadCount.sync="loadCount"
                :queryParas="searchForm" :sourceData.sync="sourceData" :operateBtnLayout="operateBtnLayout" ref="datatable" @getTableData="getTableData"
                :menuFlag="menuFlag" @selectRowChange="selectRowChange" :columns="columns" :selectTable="selectTable" loadPage="getbussList"
                :row-style="rowForbidden">
      <!-- 区域 -->
      <template slot="coreEnterpriseAreaName" slot-scope="scope">
        <expand-collapse :sourceData="sourceData" :colProp="'coreAreaList'"
                         :valueProp="'areaName'" :rowIndex="scope.data.$index"></expand-collapse>
      </template>
      <!-- 项目公司 -->
      <template slot="contractName" slot-scope="scope">
        <expand-collapse :sourceData="sourceData" :colProp="'corePlateProjectList'"
                         :valueProp="'projectName'" :rowIndex="scope.data.$index"></expand-collapse>
      </template>
      <!-- 区域 -->
<!--      <template slot="coreEnterpriseAreaName" slot-scope="scope">-->
<!--        <expand-collapse :sourceData="sourceData" colProp="plateAreaRelDtoList"-->
<!--                         :valueProp="'coreEnterpriseAreaName'" :rowIndex="scope.data.$index"></expand-collapse>-->
<!--      </template>-->
<!--      &lt;!&ndash; 合同名称 &ndash;&gt;-->
<!--      <template slot="contractName" slot-scope="scope">-->
<!--        <expand-collapse :sourceData="sourceData" colProp="plateContractRelDtoList"-->
<!--                         :valueProp="'contractName'" :rowIndex="scope.data.$index"></expand-collapse>-->
<!--      </template>-->
      <!-- 联系人 -->
      <template slot="employeeName" slot-scope="scope">
        <expand-collapse :sourceData="sourceData" colProp="employeeDtoList"
                         :valueProp="'employeeName'" :rowIndex="scope.data.$index"></expand-collapse>
      </template>
      <!-- 手机号码 -->
      <template slot="telephone" slot-scope="scope">
        <expand-collapse :sourceData="sourceData" colProp="employeeDtoList"
                         :valueProp="'telephone'" :rowIndex="scope.data.$index"></expand-collapse>
      </template>
    </data-table>
  </div>
</template>

<script>
import * as format from '@/filters/index.js'
import { upDownLayout, searchForm, dymForm, DataTable, authButton, ExpandCollapse } from 'newepoch-components'
import SelectArea from '@/components/element/enterpriseInfoManage/SelectArea.vue'
import SelectContract from '@/components/element/enterpriseInfoManage/SelectContract.vue'
import supplierInfoManageApi from '@/api/enterpriseInfoManage/supplierInfoManageApi.js'
import BasePage from '@/pages/basePage'
import storage from '@/utils/storage'
export default {
  extends: new BasePage(),
  name: 'coreEnterbusinessOperateInfo.vue',
  components: {
    upDownLayout,
    searchForm,
    dymForm,
    DataTable,
    authButton,
    SelectArea,
    SelectContract,
    ExpandCollapse
  },
  data () {
    return {
      pa: '',
      menuFlag: true,
      showSelection: true,
      showContact: false,
      showEditContact: false,
      showRecord: false,
      showDelInfo: false,
      contactModel: '',
      recordModel: '',
      // 当前企业ID
      currentId: '',
      contactItem: {},
      operateBtnLayout: '',
      searchForm: {
        pageIndex: 1,
        pageSize: 10
      },
      sourceData: [],
      searchList: [
        // 联系人
        {
          value: 'employeeName',
          disabled: false,
          clearable: true,
          componentName: 'el-input',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.contact,
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          labelWidth: '130px'
        },
        // 登录账号
        {
          value: 'account',
          disabled: false,
          clearable: true,
          componentName: 'el-input',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.loginAccount,
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          labelWidth: '130px'
        },
        // 账号类型
        {
          value: 'employeeAccountType',
          disabled: false,
          clearable: true,
          componentName: 'SelectDictionary',
          dictTypeCode: 'create_account_type',
          store: this.$store,
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.accountType,
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseSelect,
          labelWidth: '130px'
        },
        // 是否做业务
        {
          value: 'businessStatus',
          disabled: false,
          clearable: true,
          componentName: 'SelectDictionary',
          dictTypeCode: 'ZD0003',
          store: this.$store,
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.doBusiness,
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseSelect,
          labelWidth: '130px'
        }
      ],
      columns: [
        // 账号类型
        {
          prop: 'employeeAccountType',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.accountType,
          width: '50px',
          format: (row) => {
            return format.formatDict(row.employeeAccountType, 'create_account_type')
          }
        },
        // 是否做业务
        {
          prop: 'businessStatus',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.doBusiness,
          width: '40px',
          format: (row) => { return format.formatDict(row.businessStatus, 'ZD0003') }
        },
        // 区域
        {
          prop: 'coreEnterpriseAreaName',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.area,
          width: '80px',
          slot: true
        },
        // 合同名称
        {
          prop: 'contractName',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.projectName,
          width: '120px',
          slot: true
        },
        // 登录账号
        {
          prop: 'account',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.loginAccount,
          width: '40px'
        },
        // 联系人
        {
          prop: 'employeeName',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.contact,
          width: '50px',
          slot: true
        },
        // 手机号码
        {
          prop: 'telephone',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.mobilePhone,
          width: '',
          slot: true
        }
      ]
    }
  },
  computed: {
    menuId () {
      return storage.getLocal('params').menuId
    },
    api () {
      return supplierInfoManageApi.invoiceListApi// supplierInfoManageApi.busnisssApi
    }
  },
  methods: {
    getTableData (data) {
      var that = this
      data.forEach(it => {
        if (that.searchForm.tagCode === '60') {
          it.coreAreaList = []
          it.corePlateProjectList = []
          if (it.corePlateAreaRelDTOList) {
            it.corePlateAreaRelDTOList.forEach(it1 => {
              it.coreAreaList.push({ areaName: it1.bigAreaName + '-' + it1.areaName })
              it1.fincrmCorePlateProjectRelDTOList.forEach(it2 => {
                it.corePlateProjectList.push({ projectName: it2.projectEnterpriseName })
              })
            })
          }
        }
      })
      this.sourceData = data
    },
    isCheckExpand (colInfo, index) {
      // if (!this.UtilJs.isEmpty(this.sourceData) && !this.UtilJs.isEmpty(this.sourceData[index]) && !this.UtilJs.isEmpty(this.sourceData[index][colInfo]) && this.sourceData[index][colInfo].length !== 0) {
      return true
      // } else {
      //   return false
      // }
    },
    handleFilter () {
      this.$set(this.searchForm, 'enterpriseId', this.pa.enterpriseId.toString())
      this.$set(this.searchForm, 'tagCode', this.pa.tagCode.toString())
      // this.getList(this.searchForm)
      this.loadCount = this.loadCount + 1
    },
    // 保存成功查询
    createSuccess () {
      this.handleFilter()
    },
    success () {
      this.handleFilter()
    },
    // 展开收起
    changeRow (index, data) {
      // this.sourceData[index].moreFlag = !data.moreFlag
      let $this = this
      $this.$set($this.sourceData[index], 'moreFlag', !data.moreFlag)
    },
    // 查询
    getList (parmas) {
      // let keys = Object.keys(parmas)
      // keys.forEach(it => {
      //   if (parmas[it] === '' || parmas[it].length === 0) {
      //     delete parmas[it]
      //   }
      // })
      // let that = this
      // this.$refs.datatable.vLoading = true
      // supplierInfoManageApi.invoiceListApi.getbussList(parmas).then(res => {
      //   if (res.data && res.data.datas) {
      //     res.data.datas.forEach(it => {
      //       // it.businessInfo = day
      //       if (it.employeeDtoList.length > 3 || it.plateAreaRelDtoList.length > 3 || it.plateContractRelDtoList.length > 3) {
      //         this.$set(it, 'moreFlag', true)
      //         this.$set(it, 'btnFlag', true)
      //       } else {
      //         this.$set(it, 'moreFlag', false)
      //         this.$set(it, 'btnFlag', false)
      //       }
      //     })
      //     that.sourceData = res.data.datas
      //     this.$refs.datatable.buildTableData(that.sourceData, res.data.totalRow)
      //     // that.$set(that.sourceData, 'employeeDtoListmobilePhone', that.sourceData.employeeDtoList)
      //   }
      // }).finally((res) => {
      //   this.$refs.datatable.vLoading = false
      // })
    },
    // 复选框勾选数据
    selectRowChange (val) {
      this.selectedRows = val
    },
    batchDel () {
      if (this.selectedRows.length > 0) {
        let employeeIds = []
        let keyIds = []
        let accountIds = []
        // let keyIds2 = this.selectedRows.keyId
        this.selectedRows.forEach(it => {
          keyIds.push(it.keyId)
          accountIds.push(it.accountId)
          it.employeeDtoList.forEach(it1 => {
            employeeIds.push(it1.keyId)
          })
        })
        this.editModel = 'D'
        // this.item = Object.assign({ keyIds: keyIds, keyId: keyIds2, enterpriseId: this.searchForm.enterpriseId })
        this.item = Object.assign({ employeeIds: employeeIds, keyIds: keyIds, enterpriseId: this.searchForm.enterpriseId, tagCode: this.searchForm.tagCode, accountIds: accountIds })
        this.showDelInfo = true
      } else {
        this.warning('请先勾选数据')
      }
    },
    // 新增
    showCreate () {
      this.editModel = 'C'
      // 供应商名称
      this.item = { tagCode: this.searchForm.tagCode, enterpriseId: this.searchForm.enterpriseId }
      this.showDetail = true
    },
    // 编辑
    showEdit (data) {
      this.contactModel = 'E'
      // this.contactItem = Object.assign({ tagCode: this.searchForm.tagCode }, data)
      // this.showContact = true
      if (data.employeeAccountType && data.account) {
        this.contactItem = Object.assign({ tagCode: this.searchForm.tagCode }, data)
        this.showContact = true
      } else {
        this.showEditContact = true
        if (data.employeeInfoDto && data.employeeInfoDto.enterpriseDeptId === '0') {
          data.employeeInfoDto.enterpriseDeptId = ''
        }
        if (data.factoringResponsibility === '-1') {
          data.factoringResponsibility = ''
        }
        this.contactItem = Object.assign({
          employeeType: this.searchForm.tagCode,
          enterpriseId: this.searchForm.enterpriseId,
          enterpriseName: storage.getLocal('params').enterpriseName,
          deptId: data.employeeInfoDto.enterpriseDeptId,
          position: data.employeeInfoDto.position,
          factoringResponsibility: data.factoringResponsibility,
          cenerateType: '0' }, data.employeeDtoList[0])
      }
    },
    // 联系人履历
    showRecordEdit (data) {
      this.recordModel = 'V'
      this.item = Object.assign({}, data)
      this.showRecord = true
    },
    // 重置密码
    reset (rowData) {
      let that = this
      that.$confirm(this.$i18n.messages[this.$i18n.locale].regMsg.enterpriseInfoManage.restPassward, this.$i18n.messages[this.$i18n.locale].common.tip, {
        confirmButtonText: this.$i18n.messages[this.$i18n.locale].common.assign,
        cancelButtonText: this.$i18n.messages[this.$i18n.locale].common.cancel,
        type: 'warning'
      }).then(() => {
        supplierInfoManageApi.accountApi.resetPassword(rowData.keyId).then(res => {
          if (res.data) {
            that.success(this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.midifyPassWordsu)
          } else {
            that.error(this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.midifyPassWordfa)
          }
        })
      })
    },
    // 恢复账号
    restoreAccount (rowData) {
      let that = this
      that.$confirm(this.$i18n.messages[this.$i18n.locale].regMsg.enterpriseInfoManage.recoveryAccount, this.$i18n.messages[this.$i18n.locale].common.tip, {
        confirmButtonText: this.$i18n.messages[this.$i18n.locale].common.assign,
        cancelButtonText: this.$i18n.messages[this.$i18n.locale].common.cancel,
        type: 'warning'
      }).then(() => {
        supplierInfoManageApi.accountApi.setAccountStatus(rowData).then(res => {
          if (res.data) {
            that.success(this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.midifyTextSues)
            that.handleFilter()
          } else {
            that.error(this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.midifyTextfail)
          }
        })
      })
    },
    // 账号禁用
    rowForbidden ({ row, index }) {
      if (row.delStatus === '1') {
        return 'color: #cccbcb'
      } else {
        return 'color: #606266'
      }
    },
    // 勾选框禁用
    selectTable (row) {
      if (row.delStatus === '1' || row.delStatus === null) {
        return false
      } else {
        return true
      }
    },
    // 重置
    clearParam () {
      this.searchForm = {
        pageIndex: 1,
        pageSize: 10,
        // employeeName: '',
        // regionCode: '',
        // account: '',
        enterpriseId: this.searchForm.enterpriseId,
        tagCode: this.searchForm.tagCode
        // employeeAccountType: '',
        // businessStatus: '',
        // contractId: '',
        // bizMode: ''
      }
    }
  },
  watch: {
    showDelInfo (val) {
      if (!val) {
        this.handleFilter()
      }
    },
    sourceData (n, o) {
      console.log(n, o)
    }
  }
}
</script>

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