<template>
  <dialog-edit :visible.sync="dialogVisible"
               ref="detailDialog"
               :title="$t('factoringBusinessInfo.factoring_info_detail')"
               :editModel="'E'"
               :form="assetCodeForm"
               :appendToBody="true"
               :api="api"
               :footerVisible="true"
               :width="width"
               :saveLabel="$t('common.confirms')"
               :formRules="formRules"
               :autoSave="autoSave"
               :loading="loading"
               @onSaved="confirm">
    <!-- 付款确认书信息 -->
    <span class="typefaceSize">{{$t('factoringBusinessInfo.confirmation_payment_info')}}</span>
    <div class="divider"></div>
    <paymentConfirmation :assetCodeForm.sync="assetCodeForm" :assetInfoId="this.form.assetInfoId" :initFlag="initFlag" :roleAuthority="roleAuthority" @changeRules="changeRules"></paymentConfirmation>

    <!-- 发票信息列表 -->
    <span v-if='this.isInfo' class="typefaceSize">{{$t('factoringBusinessInfo.invoice_info')}}</span>
    <div v-if='this.isInfo' class="divider"></div>
    <invoiceInfo v-if='this.isInfo'
                 :sourceData.sync="assetInvoiceInfoDtoList"
                 :dataObj="dataObj"
                 :payConfirmObj="assetCodeForm"
                 :assetInfoId="this.form.assetInfoId"
                 :initFlag="initFlag"
                 :isDisabled='isDisabled'
                 style="margin-top: 10px"
                 ref="invoiceInfos"></invoiceInfo>

    <!-- 供应商上传文件 -->
    <span class="typefaceSize">{{$t('factoringBusinessInfo.creditor_upload_file')}}</span>
    <div class="divider"></div>
    <creditorUploadFile :query.sync="query" :initFlag="initFlag" :api="supFileApi"></creditorUploadFile>
  </dialog-edit>
</template>
<script>
import BaseEdit from '@/pages/baseEditPage'
import { DialogEdit, authButton, validate } from 'newepoch-components'
import paymentConfirmation from './components/paymentConfirmation'
import invoiceInfo from './components/invoiceInfo'
import creditorUploadFile from './components/creditorUploadFile'
import factoringBusinessInfoApi from '@/api/factoringBusinessInfoApi'
export default {
  name: 'factoringInfoDetail',
  extends: new BaseEdit(),
  components: {
    DialogEdit,
    authButton,
    paymentConfirmation,
    invoiceInfo,
    creditorUploadFile
  },
  computed: {
    supFileApi () {
      return factoringBusinessInfoApi.supFileApi
    },
    api () {
      return factoringBusinessInfoApi.searchApi
    }
  },
  data () {
    const { common, factoringBusinessInfo } = this.$i18n.messages[this.$i18n.locale]
    return {
      width: '1480px',
      autoSave: false,
      assetCodeForm: {
        basicContractName: '',
        bussContractName: '',
        basicContractCode: '',
        bussContractCode: '',
        assetCode: '',
        coreEnterpriseId: '',
        creditor: '',
        debtor: '',
        factoringMoney: '',
        dueDate: '',
        initialOwnersId: '',
        planOrBankId: '',
        specialPlanId: '',
        specialPlanPeriods: '',
        projectName: '',
        coreArea: '',
        coreAreaAttacheName: '',
        customerAccountName: '',
        factoringApplyTime: '',
        receiverBank: '',
        receiverBankAccount: '',
        receiverName: '',
        assetServiceName: '',
        assetServiceId: ''
      },
      assetInvoiceInfoDtoList: [],
      dataObj: {},
      initFlag: false,
      query: {},
      formRules: {
        projectId: [{ required: true, trigger: 'change', message: common.input + factoringBusinessInfo.product_name }],
        coreEnterpriseId: [{ required: true, trigger: 'change', message: common.point + factoringBusinessInfo.core_enterprise }],
        planOrBankId: [{ required: true, trigger: 'change', message: common.point + factoringBusinessInfo.planning_manager }],
        specialPlanId: [{ required: true, trigger: 'change', message: common.input + factoringBusinessInfo.periods }],
        initialOwnersId: [{ required: true, trigger: 'change', message: common.point + factoringBusinessInfo.initial_equity_owner }],
        assetServiceId: [{ required: true, trigger: 'change', message: common.point + factoringBusinessInfo.assest_service }], // 资产服务机构
        assetCode: [{ required: true, trigger: 'blur', message: common.input + factoringBusinessInfo.pay_code }],
        creditor: [{ required: true, trigger: 'blur', message: common.input + factoringBusinessInfo.creditor }],
        debtor: [{ required: true, trigger: 'blur', message: common.input + factoringBusinessInfo.project_company }],
        receiverName: [{ required: true, trigger: 'blur', message: common.input + factoringBusinessInfo.payee_name }],
        receiverBank: [{ required: true, trigger: 'blur', message: common.input + factoringBusinessInfo.payee_bank }],
        receiverBankAccount: [{ required: true, trigger: 'blur', message: common.input + factoringBusinessInfo.payee_account }],
        dueDate: [{ required: true, trigger: 'change', message: common.point + factoringBusinessInfo.amount_due_date }],
        factoringMoney: [{ required: true, message: common.input + factoringBusinessInfo.account_receivable }, { validator: validate.yuanValidate, trigger: 'blur' }],
        coreAreaName: [{ required: true, trigger: 'blur', message: common.input + factoringBusinessInfo.area }],
        coreArea: [{ required: true, trigger: 'blur', message: common.input + factoringBusinessInfo.area_number }],
        factoringApplyTime: [{ required: true, trigger: 'change', message: common.point + factoringBusinessInfo.factoring_application_date }]
      },
      roleAuthority: [],
      isInfo: false,
      isDisabled: false,
      loading: false
    }
  },
  methods: {
    changeRules (prop, rules) {
      this.formRules[prop] = rules
      this.$refs['detailDialog'].validateField('specialPlanId')
    },
    // 保存
    confirm () {
      let flag = true
      this.$refs.invoiceInfos.$refs.invoiceForm.validate((valid) => {
        flag = valid
      })
      if (flag && this.$refs.invoiceInfos.validateInvoice()) {
        this.loading = true
        let newObj = {
          assetInvoiceInfoDtos: this.assetInvoiceInfoDtoList,
          // 资产信息
          assetPaymentInfoDTO: {
            assetCode: this.assetCodeForm.assetCode,
            basicContractCode: this.assetCodeForm.basicContractCode,
            basicContractName: this.assetCodeForm.basicContractName,
            bussContractCode: this.assetCodeForm.bussContractCode,
            bussContractName: this.assetCodeForm.bussContractName,
            coreEnterpriseId: this.assetCodeForm.coreEnterpriseId,
            coreEnterpriseName: this.assetCodeForm.coreEnterpriseName, // 核心企业名称todo
            creditor: this.assetCodeForm.creditor,
            creditorId: this.assetCodeForm.creditorId,
            debtor: this.assetCodeForm.debtor,
            debtorId: this.assetCodeForm.debtorId,
            dueDate: this.assetCodeForm.dueDate,
            factoringMoney: this.assetCodeForm.factoringMoney,
            keyId: this.assetCodeForm.assetPaymentInfoId,
            regionCode: this.assetCodeForm.regionCode
          },
          // abs专项计划资产池信息
          assetPaymentPlanOrBankDTO: {
            assetServiceId: this.assetCodeForm.assetServiceId,
            assetServiceName: this.assetCodeForm.assetServiceName,
            factoringRate: this.assetCodeForm.factoringRate,
            initialOwnersId: this.assetCodeForm.initialOwnersId,
            initialOwnersName: this.assetCodeForm.initialOwnersName, // 原始权益人名称todo
            keyId: this.assetCodeForm.assetPaymentPlanOrBankId,
            planOrBankId: this.assetCodeForm.planOrBankId,
            planOrBankName: this.assetCodeForm.planOrBankName, // 计划管理人名称todo
            specialPlanId: this.assetCodeForm.specialPlanId,
            specialPlanPeriods: this.assetCodeForm.specialPlanPeriods,
            coreEnterpriseId: this.assetCodeForm.coreEnterpriseId,
            coreEnterpriseName: this.assetCodeForm.coreEnterpriseName
          },
          // 资产池信息
          assetPaymentPoolDTO: {
            keyId: this.assetCodeForm.assetPaymentPoolId,
            productType: this.assetCodeForm.productType,
            projectId: this.assetCodeForm.projectId,
            projectName: this.assetCodeForm.projectName
          },
          // 资产池操作信息系
          assetPaymentPoolOperDTO: {
            coreArea: this.assetCodeForm.coreArea,
            coreAreaName: this.assetCodeForm.coreAreaName,
            factoringApplyTime: this.assetCodeForm.factoringApplyTime,
            keyId: this.assetCodeForm.assetPaymentPoolOperId,
            modifyFlag: this.assetCodeForm.modifyFlag,
            receiverBank: this.assetCodeForm.receiverBank,
            receiverBankAccount: this.assetCodeForm.receiverBankAccount,
            receiverName: this.assetCodeForm.receiverName,
            customerAccountName: this.assetCodeForm.customerAccountName,
            coreAreaAttacheName: this.assetCodeForm.coreAreaAttacheName
          }
        }
        factoringBusinessInfoApi.assetApi.saveAsset(newObj).then(res => {
          if (res && res.success) {
            this.success('资产信息修改成功！')
            this.dialogVisible = false
            this.$emit('handleFilter')
          } else {
            this.error('资产信息修改失败！')
          }
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      }
    }
  },
  watch: {
    dialogVisible (val) {
      if (val) {
        // 查询资产信息
        factoringBusinessInfoApi.assetApi.get(this.form.assetInfoId).then(res => {
          if (res && res.data) {
            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,
              // abs专项计划资产池信息
              assetServiceId: res.data.assetPaymentPlanOrBankDTO.assetServiceId,
              assetServiceName: res.data.assetPaymentPlanOrBankDTO.assetServiceName,
              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,
              specialPlanId: res.data.assetPaymentPlanOrBankDTO.specialPlanId,
              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,
              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
            }
            this.assetInvoiceInfoDtoList = res.data.assetInvoiceInfoDtos.sort(function (a, b) {
              return a.invoiceNo + '' - b.invoiceNo + ''
            })
            this.assetInvoiceInfoDtoList.forEach(it => {
              this.$set(it, 'maxAvailableAmount', it.availableAmount)
            })
            this.dataObj = Object.assign({}, res.data)
            this.roleAuthority = res.data.assetModifyConfigDtos
            this.$nextTick(() => {
              if (this.roleAuthority.length === 0 || this.roleAuthority === undefined) {
                this.isInfo = false
              } else {
                for (let i = 0; i < this.roleAuthority.length; i++) {
                  if (this.roleAuthority[i].columnCode === 'invoice') {
                    if (this.roleAuthority[i].roleAuthority === 0) {
                      this.isInfo = false
                      this.isDisabled = false
                      return
                    } else if (this.roleAuthority[i].roleAuthority === 1) {
                      this.isInfo = true
                      this.isDisabled = true
                      return
                    } else if (this.roleAuthority[i].roleAuthority === 2) {
                      this.isInfo = true
                      this.isDisabled = false
                      return
                    }
                  }
                }
              }
            })
            this.initFlag = true
          }
        })
        this.query = Object.assign({}, { assetInfoId: this.form.assetInfoId })
      } else {
        this.initFlag = false
      }
    }
  }
}
</script>
