<template>
  <dialog-edit
    :visible.sync="dialogVisible"
    :title="$t('chargeback.pay_code_view')"
    editModel="V"
    :form="form"
    :appendToBody="true"
    width="1480px"
    :api="api"
  >
    <dym-form
      :menuCode="menuId"
      :formValue.sync="assetCodeForm"
      :componentList="searchList"
    >
    </dym-form>
    <data-table
        :api="api"
        headTitle="$t('chargeback.paymentViewTitle')"
        operateBtnLayout
        :sourceData.sync="sourceData"
        :columns="columns"
        :loadCount.sync="loadCount"
        :showPage="false"
      >
      </data-table>
      <el-row>
        <span>发票金额合计：{{invoiceAmountTotal}}</span>
        <span style="margin-left: 24px">抵扣金额合计：{{usedAmountTotal}}</span>
      </el-row>
  </dialog-edit>
</template>

<script>
import BaseEditPage from '@/pages/baseEditPage'
import { DialogEdit, dymForm, DataTable } from 'newepoch-components'
import { setTimeout } from 'timers'
import factoringBusinessInfoApi from '@/api/factoringBusinessInfoApi'
import * as format from '@/filters/index'
import chargebackApi from '@/api/chargeback'
const cols = [24, 12, 12, 8]
export default {
  extends: new BaseEditPage('paymentConfirmation'),
  components: {
    DialogEdit,
    dymForm,
    DataTable
  },
  props: {
    form: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  computed: {
    // 发票金额合计
    invoiceAmountTotal () {
      let total = 0
      this.sourceData.forEach(item => {
        if (item.invoiceAmount !== '') {
          total += item.invoiceAmount
        }
      })
      return format.thousandFilter(total, 2)
    },
    // 抵扣金额合计
    usedAmountTotal () {
      let total = 0
      this.sourceData.forEach(item => {
        if (item.usedAmount !== '') {
          total += item.usedAmount
        }
      })
      return format.thousandFilter(total, 2)
    },
    api () {
      return chargebackApi.initAPI
    }
  },
  data () {
    return {
      assetCodeForm: {},
      sourceData: [],
      searchList: [
        {
          // 付款确认书编号
          label: this.$i18n.messages[this.$i18n.locale].chargeback.pay_code,
          value: 'assetCode',
          disabled: true,
          placeholder: '请输入',
          componentName: 'el-input',
          cols,
          labelWidth: '110px'
        },
        {
          // 合同编号
          label: this.$i18n.messages[this.$i18n.locale].chargeback.contract_no,
          value: 'basicContractCode',
          disabled: true,
          placeholder: '请输入',
          componentName: 'el-input',
          cols,
          labelWidth: '130px'
        },
        {
          // 账款到期日
          label: this.$i18n.messages[this.$i18n.locale].chargeback
            .account_maturity_date,
          value: 'dueDate',
          disabled: true,
          placeholder: '请输入',
          componentName: 'el-date-picker',
          cols,
          labelWidth: '160px'
        },
        {
          // 收款人名称
          label: this.$i18n.messages[this.$i18n.locale].chargeback.payee_name,
          value: 'receiverName',
          disabled: true,
          placeholder: '请输入',
          componentName: 'el-input',
          cols,
          labelWidth: '110px'
        },
        {
          // 项目公司名称
          label: this.$i18n.messages[this.$i18n.locale].chargeback
            .project_company,
          value: 'debtor',
          disabled: true,
          placeholder: '请输入',
          componentName: 'el-input',
          cols,
          labelWidth: '130px'
        },
        {
          // 应收账款金额
          label: this.$i18n.messages[this.$i18n.locale].chargeback
            .account_receivable,
          value: 'factoringMoney',
          disabled: true,
          placeholder: '请输入',
          componentName: 'el-input',
          cols,
          labelWidth: '160px'
        },
        {
          // 供应商名称
          label: this.$i18n.messages[this.$i18n.locale].chargeback.creditor,
          value: 'creditor',
          disabled: true,
          placeholder: '请输入',
          componentName: 'el-input',
          cols,
          labelWidth: '110px'
        },
        {
          // 原始权益人
          label: this.$i18n.messages[this.$i18n.locale].chargeback
            .initial_equity_owner,
          value: 'initialOwnersName',
          disabled: true,
          placeholder: '请输入',
          componentName: 'el-input',
          cols,
          labelWidth: '130px'
        },
        {
          // 供应商开户行
          label: this.$i18n.messages[this.$i18n.locale].chargeback
            .suppliers_bank,
          value: 'receiverBank',
          disabled: true,
          placeholder: '请输入',
          componentName: 'el-input',
          cols,
          labelWidth: '160px'
        },
        {
          // 核心企业
          label: this.$i18n.messages[this.$i18n.locale].chargeback
            .core_enterprise,
          value: 'coreEnterpriseName',
          disabled: true,
          placeholder: '请输入',
          componentName: 'el-input',
          cols,
          labelWidth: '110px'
        },
        {
          // 供应商账号
          label: this.$i18n.messages[this.$i18n.locale].chargeback
            .suppliers_bank_num,
          value: 'receiverName',
          disabled: true,
          placeholder: '请输入',
          componentName: 'el-input',
          cols,
          labelWidth: '130px'
        },
        {
          // 计划管理人/再保理银行
          label: this.$i18n.messages[this.$i18n.locale].chargeback
            .planning_manager,
          value: 'planOrBankName',
          disabled: true,
          placeholder: '请输入',
          componentName: 'el-input',
          cols,
          labelWidth: '160px'
        },
        {
          // 合同名称
          label: this.$i18n.messages[this.$i18n.locale].chargeback
            .contract_name,
          value: 'basicContractName',
          disabled: true,
          placeholder: '请输入',
          componentName: 'el-input',
          cols,
          labelWidth: '110px'
        },
        {
          // 商务合同编号
          label: this.$i18n.messages[this.$i18n.locale].chargeback.BusinessContractNumber,
          value: 'bussContractCode',
          disabled: true,
          placeholder: '请输入',
          componentName: 'el-input',
          cols,
          labelWidth: '130px'
        },
        {
          // 商务合同名称
          label: this.$i18n.messages[this.$i18n.locale].chargeback.BusinessContractName,
          value: 'bussContractName',
          disabled: true,
          placeholder: '请输入',
          componentName: 'el-input',
          cols,
          labelWidth: '160px'
        },
        {
          // 区域编号
          label: this.$i18n.messages[this.$i18n.locale].chargeback.area_number,
          value: 'regionCode',
          disabled: true,
          placeholder: '请输入',
          componentName: 'el-input',
          cols,
          labelWidth: '110px'
        },
        {
          // 保理专员
          label: this.$i18n.messages[this.$i18n.locale].chargeback.factoring_commissioner,
          value: 'customerAccountName',
          disabled: true,
          placeholder: '请输入',
          componentName: 'el-input',
          cols,
          labelWidth: '130px'
        },
        {
          // 区域专员
          label: this.$i18n.messages[this.$i18n.locale].chargeback.regional_commissioner,
          value: 'coreAreaAttacheName',
          disabled: true,
          placeholder: '请输入',
          componentName: 'el-input',
          cols,
          labelWidth: '160px'
        },
        {
          // 业务人员
          label: this.$i18n.messages[this.$i18n.locale].chargeback.operational_personne,
          value: 'coreAreaBussUserName',
          disabled: true,
          placeholder: '请输入',
          componentName: 'el-input',
          cols,
          labelWidth: '110px'
        },
        {
          // 内引
          label: this.$i18n.messages[this.$i18n.locale].chargeback.inside_lead,
          value: 'innerLead',
          disabled: true,
          placeholder: '请输入',
          type: 'textarea',
          componentName: 'el-input',
          cols: [24, 24, 24, 24],
          labelWidth: '110px'
        }
      ],
      columns: [
        {
          // 发票号码
          prop: 'invoiceNo',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].chargeback
            .invoiceNumber
        }, {
          // 票据名称
          prop: 'invoiceName',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].chargeback
            .billName
        }, {
          // 开票日期
          prop: 'openDate',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].chargeback
            .invoiceDate,
          format: row => format.formatDateNull(row.openDate)
        }, {
          // 发票金额
          prop: 'invoiceAmount',
          align: 'right',
          label: this.$i18n.messages[this.$i18n.locale].chargeback
            .invoiceAmount,
          format: row => format.thousandFilter(row.invoiceAmount, 2)
        }, {
          // 抵扣金额
          prop: 'usedAmount',
          align: 'right',
          label: this.$i18n.messages[this.$i18n.locale].chargeback
            .deduction_amount,
          format: row => format.thousandFilter(row.usedAmount, 2)
        }, {
          // 发票备注
          prop: 'invoiceRemarks',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].chargeback
            .invoiceRemarks
        }, {
          // 检验结果
          prop: 'verificationCode',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].chargeback
            .testResult
        }
      ],
      loadCount: 0
    }
  },
  watch: {
    visible (val) {
      if (val) {
        factoringBusinessInfoApi.assetApi.get(this.form.assetInfoId).then(res => {
          if (res && res.data) {
            this.sourceData = []
            this.assetCodeForm = {
              // 资产信息
              assetPaymentInfoId: res.data.assetPaymentInfoDTO.keyId,
              basicContractName: res.data.assetPaymentInfoDTO.basicContractName,
              bussContractName: res.data.assetPaymentInfoDTO.bussContractName,
              basicContractCode: res.data.assetPaymentInfoDTO.basicContractCode,
              bussContractCode: res.data.assetPaymentInfoDTO.bussContractCode,
              assetCode: res.data.assetPaymentInfoDTO.assetCode,
              coreEnterpriseId: res.data.assetPaymentInfoDTO.coreEnterpriseId,
              coreEnterpriseName: res.data.assetPaymentInfoDTO.coreEnterpriseName,
              creditor: res.data.assetPaymentInfoDTO.creditor,
              creditorId: res.data.assetPaymentInfoDTO.creditorId,
              debtor: res.data.assetPaymentInfoDTO.debtor,
              debtorId: res.data.assetPaymentInfoDTO.debtorId,
              factoringMoney: res.data.assetPaymentInfoDTO.factoringMoney,
              dueDate: res.data.assetPaymentInfoDTO.dueDate,
              regionCode: res.data.assetPaymentInfoDTO.regionCode,
              innerLead: res.data.assetPaymentInfoDTO.innerLead,
              // abs专项计划资产池信息
              assetPaymentPlanOrBankId: res.data.assetPaymentPlanOrBankDTO.keyId,
              initialOwnersId: res.data.assetPaymentPlanOrBankDTO.initialOwnersId,
              initialOwnersName: res.data.assetPaymentPlanOrBankDTO.initialOwnersName,
              planOrBankId: res.data.assetPaymentPlanOrBankDTO.planOrBankId,
              planOrBankName: res.data.assetPaymentPlanOrBankDTO.planOrBankName,
              specialPlanPeriods: res.data.assetPaymentPlanOrBankDTO.specialPlanPeriods,
              factoringRate: res.data.assetPaymentPlanOrBankDTO.factoringRate,
              // 资产池信息
              assetPaymentPoolId: res.data.assetPaymentPoolDTO.keyId,
              projectName: res.data.assetPaymentPoolDTO.projectName,
              productType: res.data.assetPaymentPoolDTO.productType,
              projectId: res.data.assetPaymentPoolDTO.projectId,
              // 资产池操作信息系
              assetPaymentPoolOperId: res.data.assetPaymentPoolOperDTO.keyId,
              coreArea: res.data.assetPaymentPoolOperDTO.coreArea,
              coreAreaAttacheName: res.data.assetPaymentPoolOperDTO.coreAreaAttacheName,
              customerAccountName: res.data.assetPaymentPoolOperDTO.customerAccountName,
              factoringApplyTime: res.data.assetPaymentPoolOperDTO.factoringApplyTime,
              receiverBank: res.data.assetPaymentPoolOperDTO.receiverBank,
              receiverBankAccount: res.data.assetPaymentPoolOperDTO.receiverBankAccount,
              receiverName: res.data.assetPaymentPoolOperDTO.receiverName,
              modifyFlag: res.data.assetPaymentPoolOperDTO.modifyFlag,
              coreAreaBussUserName: res.data.assetPaymentPoolOperDTO.coreAreaBussUserName
            }
            this.assetCodeForm.factoringMoney = format.thousandFilter(this.assetCodeForm.factoringMoney, 2)
            this.assetCodeForm.dueDate = format.formatDateNull(this.assetCodeForm.dueDate)
            this.assetInvoiceInfoDtoList = res.data.assetInvoiceInfoDtos.sort(function (a, b) {
              return a.invoiceNo + '' - b.invoiceNo + ''
            })
            this.initFlag = true
          }
          res.data.assetInvoiceInfoDtos.forEach(item => {
            this.sourceData.push(
              {
                invoiceNo: item.invoiceNo,
                invoiceName: item.assetInvoiceRelDTO.invoiceName,
                openDate: item.openDate,
                invoiceAmount: item.invoiceAmount,
                usedAmount: item.assetInvoiceRelDTO.usedAmount,
                invoiceRemarks: '—',
                verificationCode: item.verificationCode === '' ? '' : item.verificationCode
              }
            )
          })
        })
      } else {
        setTimeout(() => {
          this.assetCodeForm = {}
        }, 300)
      }
    }
  }
}
</script>
