<!--导单管理编辑页面-->
<template>
  <div>
    <dialog-edit :visible.sync="dialogVisible" :title="$t('ladingManageList.ladingInfoEdit')" width="1200px"
                 :autoSave="false" @onSaved="onSaved" :formRules="formRules" :loading="saveLoading"
                 :api="api" :form="form" :editModel="editModel">
      <dym-form :formValue.sync="form" :componentList="componentList" @selectChanged="selectChanged"></dym-form>
    </dialog-edit>
  </div>
</template>

<script>
import { DialogEdit, dymForm, SelectSmallArea, validate } from 'newepoch-components'
import BaseEditPage from '@/pages/baseEditPage'
import guideSheetManageApi from '@/api/guideSheetManageApi'
import ladingManageApi from '@/api/ladingManageApi'
import SelectProductName from '@/components/element/shourang/SelectProductName'
import SelectCoreEnterprise from '@/components/element/shourang/SelectCoreEnterprise'
import SelectAssestService from '@/components/element/shouji/SelectAssestService'
import SelectBankAcount from '@/components/element/shouji/SelectBankAcount'
import SelectBankAcountName from '@/components/element/shouji/SelectBankAcountName'
import SelectPrimitiveRightsHolder from '@/components/element/shourang/SelectPrimitiveRightsHolder'
import SelectFinancialInstitutions from '@/components/element/shourang/SelectFinancialInstitutions'
import * as format from '@/filters/index'
import { regExp } from '@/validator/index'

export default {
  extends: new BaseEditPage('guideSheetManage'),
  name: 'guideSheetManageEdit',
  components: {
    DialogEdit,
    dymForm
  },
  data () {
    return {
      saveLoading: false,
      componentList: [
        {
          // 产品名称
          label: this.$i18n.messages[this.$i18n.locale].guideSheetManage.productName,
          disabled: true,
          value: 'projectName',
          labelWidth: '160px',
          cols: [8, 8, 8, 8],
          clearable: true,
          prop: 'projectName',
          componentName: SelectProductName
        },
        {
          // 期数
          label: this.$i18n.messages[this.$i18n.locale].guideSheetManage.specialPlanPeriods,
          value: 'specialPlanPeriods',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          disabled: true,
          cols: [8, 8, 8, 8],
          labelWidth: '160px',
          clearable: true,
          componentName: 'el-input'
        },
        {
          // 核心企业
          label: this.$i18n.messages[this.$i18n.locale].guideSheetManage.coreEnterpriseName,
          disabled: true,
          cols: [8, 8, 8, 8],
          value: 'coreEnterpriseId',
          labelWidth: '160px',
          clearable: true,
          componentName: SelectCoreEnterprise
        },
        {
          // 计划管理人/再保理银行
          label: this.$i18n.messages[this.$i18n.locale].guideSheetManage.planningManagerName,
          disabled: true,
          cols: [8, 8, 8, 8],
          value: 'planningManagerId',
          labelWidth: '160px',
          clearable: true,
          componentName: SelectFinancialInstitutions
        },
        {
          // 提单类型
          label: this.$i18n.messages[this.$i18n.locale].guideSheetManage.ladingBillType,
          disabled: true,
          cols: [8, 8, 8, 8],
          value: 'ladingType',
          labelWidth: '160px',
          clearable: true,
          componentName: 'SelectDictionary',
          dictTypeCode: 'ZD0100',
          store: this.$store
        },
        {
          // 原始权益人
          label: this.$i18n.messages[this.$i18n.locale].guideSheetManage.initialOwnersName,
          disabled: true,
          cols: [8, 8, 8, 8],
          value: 'initialOwnersId',
          labelWidth: '160px',
          clearable: true,
          componentName: SelectPrimitiveRightsHolder
        },
        {
          // 资产服务机构
          label: this.$i18n.messages[this.$i18n.locale].guideSheetManage.assetServiceOrgan,
          disabled: true,
          cols: [8, 8, 8, 8],
          value: 'assetServiceId',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseSelect,
          labelWidth: '160px',
          clearable: true,
          componentName: SelectAssestService
        },
        {
          // 应收账款金额
          label: this.$i18n.messages[this.$i18n.locale].guideSheetManage.accountsReceivable,
          value: 'factoringMoney',
          prop: 'factoringMoney',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          disabled: false,
          cols: [8, 8, 8, 8],
          labelWidth: '160px',
          clearable: true,
          componentName: 'el-input'
        },
        {
          // 账款到期日
          label: this.$i18n.messages[this.$i18n.locale].guideSheetManage.dueDate,
          disabled: false,
          cols: [8, 8, 8, 8],
          value: 'dueDate',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseSelect,
          labelWidth: '160px',
          clearable: true,
          componentName: 'el-date-picker'
        },
        {
          // 项目公司名称
          label: this.$i18n.messages[this.$i18n.locale].confirmationRightData.projectCompanyName,
          disabled: false,
          cols: [8, 8, 8, 8],
          value: 'debtor',
          labelWidth: '160px',
          clearable: true,
          componentName: 'el-input'
        },
        {
          // 开户行
          label: this.$i18n.messages[this.$i18n.locale].guideSheetManage.openingBank,
          cols: [8, 8, 8, 8],
          value: 'receiverBank',
          prop: 'receiverBank',
          name: 'receiverBank',
          labelWidth: '160px',
          clearable: true,
          componentName: 'SelectDictionary',
          dictTypeCode: 'bank_id',
          store: this.$store
        },
        {
          // 收款账号
          label: this.$i18n.messages[this.$i18n.locale].guideSheetManage.receivingAccount,
          value: 'receiverBankAccount',
          prop: 'receiverBankAccount',
          name: 'receiverBankAccount',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          disabled: false,
          clearable: true,
          cols: [8, 8, 8, 8],
          labelWidth: '160px',
          queryName: 'companyId',
          queryValue: 'creditorId',
          allowCreate: true,
          componentName: SelectBankAcount
        },
        {
          // 账号名称
          label: this.$i18n.messages[this.$i18n.locale].guideSheetManage.accountName,
          value: 'receiverAccountName',
          prop: 'receiverAccountName',
          name: 'receiverAccountName',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          disabled: false,
          cols: [8, 8, 8, 8],
          clearable: true,
          labelWidth: '160px',
          queryName: 'companyId',
          queryValue: 'creditorId',
          allowCreate: true,
          componentName: SelectBankAcountName
        },
        {
          // 合同名称
          label: this.$i18n.messages[this.$i18n.locale].guideSheetManage.contractName,
          value: 'basicContractName',
          prop: 'basicContractName',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          disabled: false,
          cols: [8, 8, 8, 8],
          labelWidth: '160px',
          clearable: true,
          componentName: 'el-input'
        },
        {
          // 合同编号
          label: this.$i18n.messages[this.$i18n.locale].guideSheetManage.contractNum,
          value: 'basicContractCode',
          prop: 'basicContractCode',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          disabled: false,
          cols: [8, 8, 8, 8],
          labelWidth: '160px',
          clearable: true,
          componentName: 'el-input'
        },
        {
          // 付款确认书编号
          label: this.$i18n.messages[this.$i18n.locale].guideSheetManage.payCode,
          value: 'assetCode',
          prop: 'assetCode',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          disabled: false,
          cols: [8, 8, 8, 8],
          labelWidth: '160px',
          clearable: true,
          componentName: 'el-input'
        },
        {
          // 发票号码
          label: this.$i18n.messages[this.$i18n.locale].guideSheetManage.invoiceNum,
          value: 'invoiceNo',
          prop: 'invoiceNo',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          disabled: false,
          cols: [8, 8, 8, 8],
          labelWidth: '160px',
          clearable: true,
          componentName: 'el-input'
        },
        {
          // 发票金额
          label: this.$i18n.messages[this.$i18n.locale].guideSheetManage.invoiceAmount,
          value: 'taxExcludingAmount',
          prop: 'taxExcludingAmount',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          disabled: false,
          cols: [8, 8, 8, 8],
          labelWidth: '160px',
          clearable: true,
          componentName: 'el-input'
        },
        {
          // 正式提单确认状态
          label: this.$i18n.messages[this.$i18n.locale].guideSheetManage.formalBillConfirmStatus,
          disabled: true,
          cols: [8, 8, 8, 8],
          value: 'officalLadingConfirmStatus',
          labelWidth: '160px',
          clearable: true,
          componentName: 'SelectDictionary',
          dictTypeCode: 'ZD0103'
        },
        {
          // 区域编号
          label: this.$i18n.messages[this.$i18n.locale].guideSheetManage.regionCode,
          disabled: false,
          cols: [8, 8, 8, 8],
          value: 'regionCode',
          prop: 'regionCode',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          componentName: 'el-input',
          clearable: true,
          labelWidth: '160px'
        },
        {
          // 区域
          label: this.$i18n.messages[this.$i18n.locale].guideSheetManage.region,
          disabled: false,
          cols: [8, 8, 8, 8],
          value: 'coreArea',
          name: 'coreArea',
          labelWidth: '160px',
          componentName: SelectSmallArea,
          queryName: 'coreCompanyId',
          queryValue: 'coreEnterpriseId',
          clearable: true,
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseSelect
        },
        {
          // 供应商名称
          label: this.$i18n.messages[this.$i18n.locale].guideSheetManage.supplierName,
          disabled: false,
          cols: [8, 8, 8, 8],
          value: 'creditor',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          componentName: 'el-input',
          clearable: true,
          labelWidth: '160px'
        },
        {
          // 收款人名称
          label: this.$i18n.messages[this.$i18n.locale].guideSheetManage.payee,
          disabled: false,
          cols: [8, 8, 8, 8],
          value: 'receiverName',
          prop: 'receiverName',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          componentName: 'el-input',
          clearable: true,
          labelWidth: '160px'
        },
        {
          // 保理申请日期
          label: this.$i18n.messages[this.$i18n.locale].guideSheetManage.factApplicationDate,
          disabled: false,
          cols: [8, 8, 8, 8],
          value: 'factoringApplyTime',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseSelect,
          labelWidth: '160px',
          clearable: true,
          componentName: 'el-date-picker'
        },
        {
          // 正式导入状态
          label: this.$i18n.messages[this.$i18n.locale].guideSheetManage.formalImportStatus,
          disabled: true,
          cols: [8, 8, 8, 8],
          value: 'officalLadingStatus',
          labelWidth: '160px',
          clearable: true,
          componentName: 'SelectDictionary',
          dictTypeCode: 'ZD0104'
        },
        {
          // 检测结果
          label: this.$i18n.messages[this.$i18n.locale].guideSheetManage.detectionResult,
          disabled: true,
          cols: [8, 8, 8, 8],
          value: 'errorConfirmStatus',
          labelWidth: '160px',
          clearable: true,
          componentName: 'SelectDictionary',
          dictTypeCode: 'SJ010'
        }
      ],
      formRules: {
        // 应收账款金额
        factoringMoney: [{ validator: this.taxExcludingAmountCheck, trigger: 'change' }],
        // 账号名称
        receiverAccountName: [{ validator: validate.nameValidate, trigger: 'change' }],
        // 合同名称
        basicContractName: [{ validator: validate.contractNameValidate, trigger: 'change' }],
        // 合同编号
        basicContractCode: [{ validator: validate.contractNumValidate, trigger: 'change' }],
        // 付款确认书编号
        assetCode: [{ validator: validate.payNumValidate, trigger: 'change' }],
        // 收款账号
        receiverBankAccount: [{ validator: validate.openingBankAccountValidate, trigger: 'change' }],
        // 发票号码
        invoiceNo: [{ validator: this.invoiceNumValidate, trigger: 'change' }],
        // 发票金额
        taxExcludingAmount: [{ validator: this.taxExcludingAmountCheck, trigger: 'change' }],
        // 开户行
        // receiverBank: [{ validator: validate.openingBankNameValidate, trigger: 'change' }],
        // 区域编号
        regionCode: [{ validator: validate.areaNumValidate, trigger: 'change' }],
        // 收款人名称
        receiverName: [{ validator: validate.nameValidate, trigger: 'change' }]
      }
    }
  },
  computed: {
    api () {
      return guideSheetManageApi.guideSheetManage
    }
  },
  methods: {
    invoiceNumValidate (rule, value, callback) {
      let data = value
      if (this.UtilJs.isEmpty(data)) {
        return callback()
      }
      let flag = false
      let arr1 = data.split(/[,;-，；-]/)
      let len = arr1.length
      for (let i = 0; i < len; i++) {
        flag = regExp.contactNum.test(arr1[i])
        if (!flag) {
          break
        }
      }
      if (!flag) {
        if (len === 1) {
          callback(new Error(this.$i18n.messages[this.$i18n.locale].ladingManageList.inputinvoiceNumCheck1))
        } else if (len > 1) {
          callback(new Error(this.$i18n.messages[this.$i18n.locale].ladingManageList.inputMuinvoiceNumCheck1))
        }
      } else {
        callback()
      }
    },
    // 应收账款金额、发票金额 校验
    taxExcludingAmountCheck (rule, value, callback) {
      if (value) {
        value = value.toString()
        value = value.replace(/,/g, '')
        validate.yuanValidate(rule, value, callback)
      } else {
        callback()
      }
    },
    // 保存
    onSaved () {
      this.saveLoading = true
      let keys = Object.keys(this.form)
      keys.forEach(it => {
        if (!this.form[it] && this.form[it] !== 0) {
          delete this.form[it]
        }
        if (it === 'taxExcludingAmount' || it === 'factoringMoney') {
          this.form[it] = this.form[it].toString().replace(/,/g, '')
        }
      })
      ladingManageApi.ladingManageList.updateAsset(this.form).then(res => {
        if (res && res.data) {
          this.success(this.$i18n.messages[this.$i18n.locale].common.saveSuccess)
          this.dialogVisible = false
          this.$emit('savefinish')
        } else {
          this.saveLoading = false
          this.error(this.$i18n.messages[this.$i18n.locale].common.saveError)
        }
      }).catch(res => {
        this.saveLoading = false
      })
    },
    selectChanged (obj) {
      if (!this.UtilJs.isEmpty(obj.suppliersInfo)) {
        this.form.suppliersName = obj.suppliersInfo.suppliersName
      } else {
        this.form.suppliersName = ''
      }
      // if (!this.UtilJs.isEmpty(obj.projectInfo)) {
      //   this.form.projectName = obj.projectInfo.projectName
      // } else {
      //   this.form.projectName = ''
      // }
      if (!this.UtilJs.isEmpty(obj.coreAreaInfo)) {
        this.form.coreAreaName = obj.coreAreaInfo.areaName
      } else {
        this.form.coreAreaName = ''
      }
      if (!this.UtilJs.isEmpty(obj.projectCompanyInfo)) {
        this.form.projectCompanyName = obj.projectCompanyInfo.projectCompanyName
      } else {
        this.form.projectCompanyName = ''
      }
      if (!this.UtilJs.isEmpty(obj.receiverAccountNameInfo)) {
        this.form.receiverAccountName = obj.receiverAccountNameInfo.payeeName
        this.form.receiverBankAccount = obj.receiverAccountNameInfo.bankNo
        this.form.receiverBank = obj.receiverAccountNameInfo.openingBank
      }
      if (!this.UtilJs.isEmpty(obj.receiverBankAccountInfo)) {
        this.form.receiverAccountName = obj.receiverBankAccountInfo.payeeName
        this.form.receiverBankAccount = obj.receiverBankAccountInfo.bankNo
        this.form.receiverBank = obj.receiverBankAccountInfo.openingBank
      }
      // if (!this.UtilJs.isEmpty(obj.receiverBankInfo)) {
      //   this.form.receiverAccountName = obj.receiverBankInfo.payeeName
      //   this.form.receiverBankAccount = obj.receiverBankInfo.bankNo
      //   this.form.receiverBank = obj.receiverBankInfo.openingBank
      // }
    }
  },
  watch: {
    dialogVisible (newVal) {
      if (newVal) {
        this.saveLoading = false
        this.$set(this.form, 'officalLadingStatus', this.form.officalLadingStatus.toString())
        this.$set(this.form, 'receiverBank', this.form.receiverBank.toString())
        this.$set(this.form, 'officalLadingConfirmStatus', this.form.officalLadingConfirmStatus.toString())
        this.$set(this.form, 'errorConfirmStatus', this.form.errorConfirmStatus.toString())
        this.$set(this.form, 'ladingType', this.form.ladingType.toString())
        this.$set(this.form, 'coreArea', this.form.coreArea.toString())
        this.form.factoringMoney = format.thousandFilter(this.form.factoringMoney)
        this.form.taxExcludingAmount = format.thousandFilter(this.form.taxExcludingAmount)
        this.form.dueDate = format.formatDateNull(this.form.dueDate) // 账款到期日
        this.form.factoringApplyTime = format.formatDateNull(this.form.factoringApplyTime) // 保理申请日期
        if (this.form.coreArea === '0') this.form.coreArea = ''
      } else {
      }
    },
    'form.invoiceNo' (n, o) {
      if (n.length > 8) {
        this.form.invoiceNo = o
      }
    }
  }
}
</script>
