<!-- 供应商信息管理--企业概况 -->
<template>
  <div>
    <dialog-edit :visible.sync="dialogVisible" :editModel="editModel" :form="form" :api="api" width="1480px"
                 :title="$t('enterpriseInfoManage.supplierInfoManage.profileHead')">
      <div class="header-title">
        <el-collapse v-model="activeNames">
          <!-- 企业基本信息 -->
          <el-collapse-item :title="$t('enterpriseInfoManage.supplierInfoManage.enterpriseBasicInfo')" name="1">
            <vertical-table :tableData="enterpriseData" :columns="verticalColumns"></vertical-table>
          </el-collapse-item>
          <!-- 业务操作信息 -->
          <el-collapse-item :title="$t('enterpriseInfoManage.supplierInfoManage.businessOperateInfo')" name="2">
            <data-table :operateBtnLayout="operateBtnLayout" :queryParas="queryParas" :sourceData="businessData"
                        :toolMenuFlag="toolMenuFlag" :columns="businessColumns" :showPage="showPage">
            </data-table>
          </el-collapse-item>
          <!-- 特殊事项 -->
          <el-collapse-item :title="$t('enterpriseInfoManage.supplierInfoManage.specialIssue')" name="3">
            <data-table :operateBtnLayout="operateBtnLayout" :queryParas="queryParas" :sourceData="specialData"
                        :toolMenuFlag="toolMenuFlag" :columns="specialColumns" :showPage="showPage">
            </data-table>
          </el-collapse-item>
          <!-- 保理专员信息 -->
          <el-collapse-item :title="$t('enterpriseInfoManage.supplierInfoManage.factorCommissionerInfo')" name="4">
            <data-table :operateBtnLayout="operateBtnLayout" :queryParas="queryParas" :sourceData="factorData"
                        :toolMenuFlag="toolMenuFlag" :columns="factorColumns" :showPage="showPage">
              <!-- 区域 -->
              <template slot="area" slot-scope="scope">
                <el-row v-for="(item, index) in scope.data.row.area" :key="index">
                  <span>{{item.areaName}}</span>
                </el-row>
              </template>
            </data-table>
          </el-collapse-item>
          <!-- 合同及付款信息 -->
          <el-collapse-item :title="$t('enterpriseInfoManage.supplierInfoManage.contractAndPayInfo')" name="5">
            <data-table :operateBtnLayout="operateBtnLayout" :queryParas="queryParas" :sourceData="contractData"
                        :toolMenuFlag="toolMenuFlag" :columns="contractColumns" :showPage="showPage" :spanMethod="spanMethod">
            </data-table>
            <div class="graphic-table">
              <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <td width="18.7%" class="graphic-td"><span>{{mergeData.amount}}</span></td>
                <td width="25.9%" class="graphic-td"><span>{{mergeData.basicContractName}}</span></td>
                <td width="16.6%" class="graphic-td text-right"><span>{{mergeData.contractAmount | thousandFilter}}</span></td>
                <td width="16.6%" class="graphic-td text-right"><span>{{mergeData.payMoney | thousandFilter}}</span></td>
                <td width="7.4%" class="graphic-td"><span>{{mergeData.payCount}}</span></td>
                <td width="14.8%" class="graphic-td"><span>{{mergeData.ladingDate}}</span></td>
              </table>
            </div>
          </el-collapse-item>
          <!-- 资质证书信息 -->
          <el-collapse-item :title="$t('enterpriseInfoManage.supplierInfoManage.qualificationCertificateInfo')" name="6">
            <data-table :operateBtnLayout="operateBtnLayout" :queryParas="queryParas" :sourceData="certificateData"
                        :toolMenuFlag="toolMenuFlag" :columns="certificateColumns" :showPage="showPage">
            </data-table>
          </el-collapse-item>
        </el-collapse>
      </div>
    </dialog-edit>
  </div>
</template>

<script>
import { DialogEdit, DataTable } from 'newepoch-components'
import BaseEdit from '@/pages/baseEditPage'
import * as format from '@/filters/index.js'
import VerticalTable from '@/components/element/cyjg/verticalTable.vue'
import coreEnterpriseInfoManageApi from '@/api/enterpriseInfoManage/coreEnterpriseInfoManageApi'
import companyApi from '@/api/enterpriseInfoManage/companyApi'
export default {
  extends: new BaseEdit(),
  name: 'enterpriseProfileEdit',
  components: {
    DialogEdit,
    DataTable,
    VerticalTable
  },
  prop: {},
  data () {
    let enterpriseInfoManage = this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage
    return {
      // 企业id
      enterpriseId: '',
      mergeData: {
        amount: enterpriseInfoManage.projCompanyInfoManage.total,
        basicContractName: '',
        contractAmount: 0,
        payMoney: 0,
        payCount: 0,
        ladingDate: '—'
      },
      activeNames: ['1', '2', '3', '4', '5', '6'],
      verticalColumns: [
        // 企业名称
        {
          label: enterpriseInfoManage.supplierInfoManage.enterpriseName,
          prop: 'enterpriseName'
        },
        // 曾用名
        {
          label: enterpriseInfoManage.supplierInfoManage.usedName,
          prop: 'historyNames'
        },
        // 变更时间
        {
          label: enterpriseInfoManage.supplierInfoManage.changeTime,
          prop: 'changeTime',
          format: (it) => {
            if (!this.UtilJs.isEmpty(it)) {
              return format.formatDate(it)
            } else {
              return ''
            }
          }
        },
        // 企业性质
        {
          label: enterpriseInfoManage.supplierInfoManage.enterpriseProperty,
          prop: 'enterpriseNature',
          format: (it) => {
            return format.formatDict(it, 'enterprise_nature')
          }
        },
        // 业务模式
        {
          label: enterpriseInfoManage.supplierInfoManage.businessModel,
          prop: 'bizMode',
          format: (it) => {
            return format.formatDict(it, 'biz_mode')
          }
        },
        // 营业执照核准日期
        {
          label: enterpriseInfoManage.supplierInfoManage.yyzzApprovedDate,
          prop: 'approvedTime',
          format: (it) => {
            if (!this.UtilJs.isEmpty(it)) {
              return format.formatDate(it)
            } else {
              return ''
            }
          }
        },
        // 供应商等级
        {
          label: enterpriseInfoManage.supplierInfoManage.supplierLevel,
          prop: 'enterpriseGrade',
          format: (it) => {
            return format.formatDict(it, 'enterprise_grade')
          }
        },
        // 关联企业
        {
          label: enterpriseInfoManage.supplierInfoManage.affiliatedEnterprise,
          prop: 'relEnterpriseName'
        },
        // 操作难度
        {
          label: enterpriseInfoManage.supplierInfoManage.difficult,
          prop: 'optDifficultyLevel',
          format: (it) => {
            return format.formatDict(it, 'opt_difficulty_level')
          }
        },
        // 盖章方式
        {
          label: enterpriseInfoManage.supplierInfoManage.sealWay,
          prop: 'stampWay',
          format: (it) => {
            return format.formatDict(it, 'sealing_method')
          }
        },
        // 电子章有效期
        {
          label: enterpriseInfoManage.supplierInfoManage.electronicSealDate,
          prop: 'stampExpiryTime',
          format: (it) => {
            return this.formatStampTime(it)
          }
        },
        // 业务状态
        {
          label: enterpriseInfoManage.supplierInfoManage.businessStatus,
          prop: 'bizStatus',
          format: (it) => {
            return format.formatDict(it, 'biz_status')
          }
        }
      ],
      enterpriseData: {
        // 营业执照核准日期
        approvedTime: '',
        // 业务模式(1：总包 /2：分包)
        bizMode: '',
        // 业务状态(1：活跃 / 2：休眠 / 3：沉寂)
        bizStatus: '',
        // 变更时间
        changeTime: '',
        // 核心企业
        coreEnterpriseName: '',
        // 供应商等级
        enterpriseGrade: '',
        // 企业名称
        enterpriseName: '',
        // 企业性质
        enterpriseNature: '',
        // 曾用名
        historyNames: '',
        // 是否合资企业
        isJointVenture: '',
        // 是否合作方
        isPartners: '',
        // 操作难度等级(1:低 / 2：中 / 3：高)
        optDifficultyLevel: '',
        // 关联企业
        relEnterpriseName: '',
        // 电子章有效期
        stampExpiryTime: '',
        // 盖章方式(1:电子章 / 2：鲜章)
        stampWay: ''
      },
      showPage: false,
      toolMenuFlag: false,
      operateBtnLayout: '',
      queryParas: {},
      // 业务操作信息Data
      businessData: [],
      // 特殊事项信息Data
      specialData: [],
      // 保理专员信息Data
      factorData: [],
      // 合同及付款信息Data
      contractData: [],
      // 资质证书信息Data
      certificateData: [],
      // 业务操作信息
      businessColumns: [
        // 核心企业
        {
          prop: 'coreEnterpriseName',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.coreEnterprise,
          width: '200px'
        },
        // 业务类型
        {
          prop: 'bizType',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.businessType,
          width: '200px'
        },
        // 业务开展时间
        {
          prop: 'bizStartTime',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.busStartTime,
          width: '150px',
          format: (row) => { return format.formatDate(row.bizStartTime) }
        },
        // 提单总数(含退单)
        {
          prop: 'ladingAll',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.billTotal,
          width: '150px'
        },
        // 提单总金额(元)
        {
          prop: 'ladingTotalAmount',
          align: 'right',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.billTotalLimit,
          width: '150px',
          format: (row) => { return format.thousandFilter(row.ladingTotalAmount) }
        },
        // 均额(元)
        {
          prop: 'ladingAverageAmount',
          align: 'right',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.averageLimit,
          width: '150px',
          format: (row) => { return format.thousandFilter(row.ladingAverageAmount) }
        },
        // 提单频次(处理数据)
        {
          prop: 'ladingFrequency',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.billFrequency,
          width: '100px',
          format: (row) => { return row.ladingFrequency + this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.unit }
        },
        // 未提单天数
        {
          prop: 'noBillDay',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.noBillDay,
          width: '150px'
        },
        // 盖章方式&盖章天数(处理数据)
        {
          prop: 'sealWayAndDay',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.sealWayAndDay,
          width: '150px',
          format: (row) => {
            if (this.UtilJs.isEmpty(row.stampWay) || this.UtilJs.isEmpty(row.stampedDays)) {
              return ''
            } else {
              return format.formatDict(row.stampWay, 'sealing_method') + '&' + row.stampedDays + '天'
            }
          }
        },
        // 对接人数
        {
          prop: 'dockingPeopleCount',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.dockNumber,
          width: '100px'
        }
        // 资料模板
        // {
        //   prop: 'factoringTemplate',
        //   align: 'center',
        //   label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.dataTemplate,
        //   width: '100px'
        // }
      ],
      // 特殊事项
      specialColumns: [
        // 区域
        {
          prop: 'regionName',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.area,
          width: '150px'
        },
        // 对接人账号
        {
          prop: 'account',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.dockPeopleAccount,
          width: '100px'
        },
        // 对接人
        {
          prop: 'employeeName',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.dockPeople,
          width: '100px'
        },
        // 特殊事项内容
        {
          prop: 'specialMatter',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.specialIssueContent,
          width: '300px'
        }
      ],
      // 保理专员信息
      factorColumns: [
        // 保理专员
        {
          prop: 'attacheName',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.factorCommissioner,
          width: ''
        },
        // 在职状态(字典)
        {
          prop: 'positionStatus',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.workStatus,
          width: '',
          format: (row) => { return format.formatDict(row.positionStatus, 'ZD0030') }
        },
        // 提单区域
        {
          prop: 'regionName',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.areaCommit,
          width: '',
          slot: true
        },
        // 开始时间
        {
          prop: 'startDate',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.startTime,
          width: '',
          format: (row) => { return format.formatCheckDatetime(row.startDate) }
        },
        // 停止时间
        {
          prop: 'endDate',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.stopTime,
          width: '',
          format: (row) => { return format.formatCheckDatetime(row.endDate) }
        },
        // 提单次数
        {
          prop: 'num',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.billNumber,
          width: ''
        }
      ],
      // 合同及付款信息
      contractColumns: [
        // 核心企业
        {
          prop: 'coreEnterpriseName',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.coreEnterprise,
          width: '60px'
        },
        // 序号
        {
          prop: 'orderNumber',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.number,
          width: '40px'
        },
        // 合同名称
        {
          prop: 'basicContractName',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.contractName,
          width: '140px'
        },
        // 合同金额(元)
        {
          prop: 'contractAmount',
          align: 'right',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.contractMoney,
          width: '90px',
          format: (row) => { return format.thousandFilter(row.contractAmount) }
        },
        // 付款金额合计(元)
        {
          prop: 'payMoney',
          align: 'right',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.payMoneyTotal,
          width: '90px',
          format: (row) => { return format.thousandFilter(row.payMoney) }
        },
        // 付款次数
        {
          prop: 'payCount',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.payTime,
          width: '40px'
        },
        // 业务开始-上次提单时间(数据处理)
        {
          prop: 'ladingDate',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.lastBillTime,
          width: '',
          format: (row) => { return format.formatDate(row.ladingDate) }
        }
      ],
      // 资质证书信息
      certificateColumns: [
        // 序号
        {
          prop: 'number',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.number,
          width: '30px'
        },
        // 资质类型
        {
          prop: 'certificateType',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.qualificationType,
          width: '',
          format: (row) => { return format.formatDict(row.certificateType, '') }
        },
        // 证书编号
        {
          prop: 'certificateNumber',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.certificateNumber,
          width: '60px'
        },
        // 资质名称
        {
          prop: 'fileName',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.qualificationName,
          width: ''
        },
        // 发证日期
        {
          prop: 'issuanceTime',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.openDate,
          width: '70px',
          format: (row) => { return format.formatDate(row.issuanceTime) }
        },
        // 证书有效期
        {
          prop: 'expiryTime',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.validityPeriod,
          width: '70px',
          format: (row) => { return format.formatDate(row.expiryTime) }
        },
        // 归档状态
        {
          prop: 'archiveFlag',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.fileStatus,
          width: '50px',
          format: (row) => { return format.formatDict(row.archiveFlag, 'archive_flag') }
        }
      ]
    }
  },
  computed: {
    api () {
      return {}
    }
  },
  methods: {
    // 合并行或列方法
    spanMethod ({ row, column, rowIndex, columnIndex }) {
      let $this = this
      let spanCount = 1
      if (columnIndex === 0) {
        if (rowIndex === 0 || $this.contractData[rowIndex - 1].coreEnterpriseName !== row.coreEnterpriseName) {
          $this.contractData.reduce((last, current) => {
            if (last.coreEnterpriseName === row.coreEnterpriseName && last.coreEnterpriseName === current.coreEnterpriseName) {
              spanCount++
            }
            return current
          })
          return {
            rowspan: spanCount,
            colspan: 1
          }
        }
        return {
          rowspan: 0,
          colspan: 0
        }
      }
    },
    // 处理电子章有效期
    formatStampTime (data) {
      let $this = this
      if ($this.enterpriseData.stampWay === '2') {
        return data
      } else {
        if (!this.UtilJs.isEmpty(data)) {
          return format.formatDate(data)
        } else {
          return ''
        }
      }
    },
    // 获取企业基本信息
    getEnterpriseList () {
      let $this = this
      let enterpriseInfoManage = this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage
      coreEnterpriseInfoManageApi.enterpriseProfile.getEnterpriseList({ keyId: this.enterpriseId, tagCode: '140' }).then(res => {
        if (res && res.success && res.data) {
          $this.enterpriseData = Object.assign($this.enterpriseData, res.data)
          if ($this.enterpriseData.stampWay === '2') {
            $this.$set($this.verticalColumns[10], 'label', enterpriseInfoManage.supplierInfoManage.noApplyReason)
            $this.$set($this.verticalColumns[10], 'prop', 'noElectronicReason')
          }
        }
      })
    },
    // 获取业务操作信息
    getBusOperateList () {
      let $this = this
      coreEnterpriseInfoManageApi.busOperate.getSupplierBusOperateList({ enterpriseId: this.enterpriseId }).then(res => {
        if (res && res.success && res.data) {
          let nowDate = Date.parse(new Date())
          res.data.forEach(it => {
            let subtractVal = ((nowDate - it.lastLadingTime) / (24 * 60 * 60 * 1000)) + ''
            let day = parseInt(subtractVal)
            $this.$set(it, 'noBillDay', day)
          })
          $this.businessData = res.data
        }
      })
    },
    // 获取特殊事项列表
    getSpecialList () {
      let $this = this
      coreEnterpriseInfoManageApi.enterpriseProfile.getSpecialList({ keyId: this.enterpriseId, tagCode: '140' }).then(res => {
        if (res && res.success && res.data) {
          res.data.forEach(it => {
            let quyu = ''
            let len = it.areaRelDtoList.length
            it.areaRelDtoList.forEach((item, index) => {
              if (len > 1) {
                quyu = quyu + item.areaName + ','
              }
              if (len === index + 1) {
                quyu = quyu + item.areaName
              }
            })
            it.regionName = quyu
          })
          $this.specialData = res.data
        }
      })
    },
    // 获取保理专员列表
    getFactorList () {
      let $this = this
      coreEnterpriseInfoManageApi.enterpriseProfile.getFactorList({ enterpriseId: this.enterpriseId, tagCode: '140', attacheType: 1 }).then(res => {
        if (res && res.success && res.data) {
          $this.factorData = res.data
        }
      })
    },
    // 获取合同及付款信息
    getContractList () {
      let $this = this
      let enterpriseInfoManage = this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage
      coreEnterpriseInfoManageApi.supContract.getSupplierContractList({ enterpriseId: this.enterpriseId }).then(res => {
        if (res && res.success && res.data) {
          $this.contractData = res.data
          let contractCount = 0
          for (let i = 0; i < res.data.length; i++) {
            if (!$this.UtilJs.isEmpty(res.data[i].basicContractName)) {
              contractCount += 1
            }
            const amountValue = Number(res.data[i].contractAmount)
            if (!isNaN(amountValue)) {
              $this.mergeData.contractAmount += amountValue
            }
            const moneyValue = Number(res.data[i].payMoney)
            if (!isNaN(moneyValue)) {
              $this.mergeData.payMoney += moneyValue
            }
            const countValue = Number(res.data[i].payCount)
            if (!isNaN(countValue)) {
              $this.mergeData.payCount += countValue
            }
          }
          $this.mergeData.basicContractName = contractCount + enterpriseInfoManage.supplierInfoManage.severalContract
        }
      })
    },
    // 获取资质证书信息
    getCertificateList () {
      let $this = this
      companyApi.certificate.getList({ customerId: this.enterpriseId }).then(res => {
        if (res && res.success && res.data) {
          res.data.forEach((it, index) => {
            $this.$set(it, 'number', index + 1)
          })
          $this.certificateData = res.data
        }
      })
    }
  },
  watch: {
    visible (val) {
      let $this = this
      if (val) {
        $this.$nextTick(() => {
          $this.enterpriseId = $this.form.enterpriseId
          // 获取企业基本信息
          $this.getEnterpriseList()
          // 获取业务操作信息
          $this.getBusOperateList()
          // 获取特殊事项
          $this.getSpecialList()
          // 获取保理专员信息
          $this.getFactorList()
          // 获取合同及付款信息
          $this.getContractList()
          // 获取资质证书信息
          $this.getCertificateList()
        })
      } else {
        $this.$set($this.verticalColumns[10], 'label', this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.electronicSealDate)
        $this.$set($this.verticalColumns[10], 'prop', 'stampExpiryTime')
      }
    }
  }
}
</script>

<style scoped lang="scss" src="@/styles/enterpriseInfoManage/supplierInfoManage/enterpriseProfileEdit.scss">
</style>
