<template>
  <dialog-edit :visible.sync="dialogVisible"
               :title="$t('factoringBusinessInfo.factoring_info_detail')"
               :editModel="editModel"
               :form="form"
               width="1480px"
               :appendToBody="true"
               :api="api">
    <!-- 付款确认书信息 -->
    <span class="typefaceSize" >{{$t('factoringBusinessInfo.confirmation_payment_info')}}</span>
    <div class="divider"></div>
    <paymentConfirmation :assetCodeForm.sync="assetCodeForm" :initFlag="initFlag"></paymentConfirmation>

    <!-- 发票信息列表 -->
    <span class="typefaceSize">{{$t('factoringBusinessInfo.invoice_info')}}</span>
    <div class="divider"></div>
    <invoiceInfo :sourceData="assetInvoiceInfoDtoList" :initFlag="initFlag"></invoiceInfo>

    <!-- 供应商上传文件 -->
    <span class="typefaceSize">{{$t('factoringBusinessInfo.creditor_upload_file')}}</span>
    <div class="divider"></div>
    <creditorUploadFile :query.sync="query" :initFlag="initFlag" :api="supFileApi"></creditorUploadFile>

    <!-- 项目公司上传文件 -->
    <span class="typefaceSize">{{$t('factoringBusinessInfo.project_company_upload_file')}}</span>
    <div class="divider"></div>
    <debtorUploadFile :query.sync="query" :initFlag="initFlag" :api="proFileApi"></debtorUploadFile>

    <!-- 供应商开票信息 -->
    <span class="typefaceSize">{{$t('factoringBusinessInfo.creditor_invoice_info')}}</span>
    <div class="divider"></div>
    <creditorInvoiceInfo :form="form"></creditorInvoiceInfo>

    <!-- 供应商回寄地址信息 -->
    <span class="typefaceSize">{{$t('factoringBusinessInfo.creditor_feedback_address_info')}}</span>
    <div class="divider"></div>
    <creditorAddressInfo :formAddress="formAddress"></creditorAddressInfo>
  </dialog-edit>
</template>
<script>
import BaseEditPage from '@/pages/baseEditPage'
import { DialogEdit } from 'newepoch-components'
import paymentConfirmation from './components/paymentConfirmation'
import invoiceInfo from './components/invoiceInfo'
import creditorUploadFile from './components/creditorUploadFile'
import debtorUploadFile from './components/debtorUploadFile'
import creditorInvoiceInfo from './components/creditorInvoiceInfo'
import creditorAddressInfo from './components/creditorAddressInfo'
import factoringBusinessInfoApi from '@/api/factoringBusinessInfoApi'
import * as format from '@/filters/index'
import companyApi from '@/api/enterpriseInfoManage/companyApi'
import componentsApi from '@/api/componentsApi'
export default {
  name: 'factoringInfoDetail',
  extends: new BaseEditPage(''),
  components: {
    DialogEdit,
    paymentConfirmation,
    invoiceInfo,
    creditorUploadFile,
    debtorUploadFile,
    creditorInvoiceInfo,
    creditorAddressInfo
  },
  computed: {
    supFileApi () {
      return factoringBusinessInfoApi.supFileApi
    },
    proFileApi () {
      return factoringBusinessInfoApi.proFileApi
    },
    api () {
      return factoringBusinessInfoApi.searchApi
    }
  },
  data () {
    return {
      formAddress: {},
      addressTime: {
        addressTel: '',
        accountNum: ''
      },
      assetCodeForm: {
        basicContractName: '',
        bussContractName: '',
        basicContractCode: '',
        contractCode: '',
        assetCode: '',
        coreEnterpriseId: '',
        creditor: '',
        debtor: '',
        factoringMoney: '',
        dueDate: '',
        initialOwnersId: '',
        initialOwnersName: '',
        planOrBankId: '',
        specialPlanPeriods: '',
        projectName: '',
        coreArea: '',
        coreAreaAttacheName: '',
        customerAccountName: '',
        factoringApplyTime: '',
        receiverBank: '',
        receiverBankAccount: '',
        receiverName: '',
        factoringRate: '',
        assetCodeForm: ''
      },
      assetInvoiceInfoDtoList: [],
      initFlag: false,
      query: {},
      queryInfo: {}
    }
  },
  watch: {
    dialogVisible (val) {
      if (val) {
        console.log(val)
        // 查询资产信息
        factoringBusinessInfoApi.assetApi.get(this.form.assetInfoId).then(res => {
          if (res && res.data) {
            this.assetCodeForm = {}
            this.assetInvoiceInfoDtoList = []
            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: format.thousandFilter(res.data.assetPaymentInfoDTO.factoringMoney, 2),
              dueDate: format.formatDateNull(res.data.assetPaymentInfoDTO.dueDate),
              regionCode: res.data.assetPaymentInfoDTO.regionCode,
              // 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,
              assetServiceName: res.data.assetPaymentPlanOrBankDTO.assetServiceName,
              factoringRate: format.thousandFilter(res.data.assetPaymentPlanOrBankDTO.factoringRate * 100, 2),
              // 资产池信息
              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,
              coreAreaName: res.data.assetPaymentPoolOperDTO.coreAreaName,
              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
            }
            if (res.data.assetInvoiceInfoDtos !== undefined) {
              if (res.data.assetInvoiceInfoDtos.length > 0) {
                this.assetInvoiceInfoDtoList = res.data.assetInvoiceInfoDtos.sort(function (a, b) {
                  return a.invoiceNo + '' - b.invoiceNo + ''
                })
                this.assetInvoiceInfoDtoList.forEach(item => {
                  item.availableAmount = item.assetInvoiceRelDTO.availableAmount
                  item.invoiceName = item.assetInvoiceRelDTO.invoiceName
                  item.usedAmount = item.assetInvoiceRelDTO.usedAmount
                })
              }
            }
            this.queryInfo = Object.assign({}, { enterpriseId: this.assetCodeForm.creditorId })
            companyApi.invoiceListApi.invoiceData(this.queryInfo).then(res => {
              if (res && res.data) {
                this.addressTime = {}
                this.addressTime = {
                  addressTel: res.data.address + '-' + res.data.telephone,
                  accountNum: res.data.openingBank + '-' + res.data.bankNo
                }
                this.form = Object.assign(res.data, this.addressTime)
              }
            })
            componentsApi.assetCollectAccount.backMailAddress(this.form.reMailAddressId).then(res => {
              if (res && res.data) {
                this.formAddress = res.data
              }
            })
            this.initFlag = true
          }
        })
        this.query = Object.assign({}, { assetInfoId: this.form.assetInfoId })
      } else {
        this.initFlag = false
      }
    }
  }
}
</script>
