<!--提单管理编辑-->
<template>
  <div>
    <dialog-edit :visible.sync="dialogVisible" :title="$t('ladingManageList.ladingInfoEdit')" width="1200px"
                 :autoSave="false" @onSaved="onSaved" :formRules="formRules" savePerms="editSave"
                 :menuCode="menuCode" :loading="saveLoading" :openPerms="true" ref="dialogDym"
                 :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, validate, SelectArea, SelectSmallArea, SelectProjectCompanyName, SelectSupplier } from 'newepoch-components'
import BaseEditPage from '@/pages/baseEditPage'
import { regExp } from '@/validator/index'
import ladingManageApi from '@/api/ladingManageApi'
import SelectEntryName from '@/components/element/shouji/SelectEntryName'
import SelectRegionAttache from '@/components/element/shouji/SelectRegionAttache'
import SelectCompanyName from '@/components/element/shourang/SelectCompanyName'
import * as format from '@/filters/index'
export default {
  extends: new BaseEditPage(''),
  components: {
    DialogEdit,
    dymForm,
    SelectEntryName,
    SelectArea,
    SelectRegionAttache,
    SelectCompanyName
  },
  props: {
    menuCode: {
      type: String,
      default: null
    }
  },
  data () {
    return {
      saveLoading: false,
      loadRefinis: false,
      componentList: [
        // 产品名称
        {
          label: this.$i18n.messages[this.$i18n.locale].common.productName,
          labelWidth: '160px',
          value: 'projectName',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'el-input',
          prop: 'projectName',
          disabled: true
        },
        // 期数
        {
          label: this.$i18n.messages[this.$i18n.locale].common.periodsNum,
          labelWidth: '160px',
          value: 'specialPlanPeriods',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'el-input',
          prop: 'specialPlanPeriods',
          disabled: true
        },
        // 核心企业
        {
          label: this.$i18n.messages[this.$i18n.locale].common.coreEnterprises,
          labelWidth: '160px',
          value: 'coreEnterpriseName',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'el-input',
          prop: 'coreEnterpriseName',
          disabled: true
        },
        // 计划管理人/再保理银行
        {
          label: this.$i18n.messages[this.$i18n.locale].common.refactorBank,
          labelWidth: '160px',
          value: 'planningManagerName',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'el-input',
          prop: 'planningManagerName',
          disabled: true
        },
        // 提单类型
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.ladingType,
          labelWidth: '160px',
          value: 'ladingType',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'SelectDictionary',
          dictTypeCode: 'ZD0100',
          prop: 'ladingType',
          reload: this.loadRefinis,
          disabled: true
        },
        // 原始权益人
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.rightsHolder,
          labelWidth: '160px',
          value: 'initialOwnersName',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'el-input',
          prop: 'initialOwnersName',
          disabled: true
        },
        // 资产服务机构
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.assetServiceOrgan,
          labelWidth: '160px',
          value: 'assetServiceName',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'el-input',
          prop: 'assetServiceName',
          disabled: true
        },
        // 供应商名称
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.supplierName,
          labelWidth: '160px',
          value: 'creditorId',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: SelectSupplier,
          prop: 'creditorId',
          name: 'suppliers',
          attrs: {
            defaultOption: []
          }
        },
        // 收款人名称
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.payee,
          labelWidth: '160px',
          value: 'receiverName',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'el-input',
          prop: 'receiverName'
        },
        // 项目名称
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.projectName,
          labelWidth: '160px',
          value: 'projectContractName',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'el-input',
          prop: 'projectContractName'
        },
        // 区域
        {
          label: this.$i18n.messages[this.$i18n.locale].common.area,
          labelWidth: '160px',
          value: 'coreArea',
          placeholder: '',
          queryName: 'coreCompanyId',
          queryValue: 'coreEnterpriseId',
          cols: [8, 8, 8, 8],
          componentName: SelectSmallArea,
          prop: 'coreArea',
          name: 'coreArea'
        },
        // 区域编号
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.areaCode,
          labelWidth: '160px',
          value: 'regionCode',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'el-input',
          prop: 'regionCode'
        },
        // 区域专员
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.areaCommissioner,
          labelWidth: '160px',
          value: 'coreAreaAttache',
          placeholder: '',
          cols: [8, 8, 8, 8],
          queryName: 'areaId',
          queryValue: 'coreArea',
          componentName: SelectRegionAttache,
          prop: 'coreAreaAttache',
          name: 'coreAreaAttache'
        },
        // 区域专员备注
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.areaCommissionerMemo,
          labelWidth: '160px',
          value: 'coreAreaRemark',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'el-input',
          prop: 'coreAreaRemark'
        },
        // 应收账款金额
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.accountReceivableLabel,
          labelWidth: '160px',
          value: 'factoringMoney',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'el-input',
          prop: 'factoringMoney'
        },
        // 项目公司名称
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.projectCompanyName,
          labelWidth: '160px',
          value: 'debtorId',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: SelectProjectCompanyName,
          prop: 'debtorId',
          name: 'debtor'
        },
        // 开户行
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.openingBank,
          labelWidth: '160px',
          value: 'receiverBank',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'el-input',
          prop: 'receiverBank'
        },
        // 收款账号
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.payAccount,
          labelWidth: '160px',
          value: 'receiverBankAccount',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'el-input',
          prop: 'receiverBankAccount'
        },
        // 合同名称
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.contractName,
          labelWidth: '160px',
          value: 'basicContractName',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'el-input',
          prop: 'basicContractName'
        },
        // 合同编号
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.contractNum,
          labelWidth: '160px',
          value: 'basicContractCode',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'el-input',
          prop: 'basicContractCode'
        },
        // 发票号码
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.invoiceNum,
          labelWidth: '160px',
          value: 'invoiceNo',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'el-input',
          prop: 'invoiceNo'
        },
        // 发票金额
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.invoiceAmountLabel,
          labelWidth: '160px',
          value: 'taxExcludingAmount',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'el-input',
          prop: 'taxExcludingAmount'
        },
        // 预提单确认状态
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.advanceLadingStatus,
          labelWidth: '160px',
          value: 'planLadingConfirmStatus',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'SelectDictionary',
          dictTypeCode: 'ZD0102',
          reload: this.loadRefinis,
          prop: 'planLadingConfirmStatus',
          disabled: true
        },
        // 正式提单确认状态
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.formalLadingStatus,
          labelWidth: '160px',
          value: 'officalLadingConfirmStatus',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'SelectDictionary',
          dictTypeCode: 'ZD0103',
          reload: this.loadRefinis,
          prop: 'officalLadingConfirmStatus',
          disabled: true
        },
        // 提单方式
        {
          value: 'ladingWay',
          componentName: 'SelectDictionary',
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.ladingMode,
          labelWidth: '170px',
          cols: [8, 8, 8, 8],
          dictTypeCode: 'ZD0101',
          prop: 'ladingWay',
          reload: this.loadRefinis,
          disabled: true
        }
      ],
      formRules: {
        // 项目名称
        debtor: [{ required: true,
          trigger: 'change',
          message: this.$i18n.messages[this.$i18n.locale].common.pleaseSelect + this.$i18n.messages[this.$i18n.locale].ladingManageList.projectName }],
        // 供应商
        creditorId: [
          {
            required: true,
            trigger: 'change',
            message: this.$i18n.messages[this.$i18n.locale].common.pleaseSelect + this.$i18n.messages[this.$i18n.locale].ladingManageList.supplierName
          }
        ],
        // 应收账款金额
        factoringMoney: [
          {
            validator: this.taxExcludingAmountCheck,
            trigger: ['change', 'blur']
          },
          {
            required: true,
            trigger: 'blur',
            message: this.$i18n.messages[this.$i18n.locale].common.pleaseInput + this.$i18n.messages[this.$i18n.locale].ladingManageList.accountReceivableLabel
          }
        ],
        // 发票金额
        taxExcludingAmount: [{ validator: this.taxExcludingAmountCheck, trigger: 'change' }],
        // 合同编号
        basicContractCode: [{ validator: validate.contractNumValidate, trigger: 'change' }],
        // 开户行名称
        receiverBank: [{ validator: validate.openingBankNameValidate, trigger: 'change' }],
        // 收款账号
        receiverBankAccount: [{ validator: validate.openingBankAccountValidate, trigger: 'change' }],
        // 发票号码
        invoiceNo: [{ validator: this.invoiceNumValidate, trigger: 'blur' }],
        // // 收款人名称
        receiverName: [{ validator: validate.nameValidate, trigger: 'change' }],
        // 区域编号
        regionCode: [{ required: true,
          trigger: 'blur',
          message: this.$i18n.messages[this.$i18n.locale].common.pleaseInput + this.$i18n.messages[this.$i18n.locale].ladingManageList.areaCode },
        { validator: validate.areaNumValidate, trigger: 'change' }],
        // 合同名称
        basicContractName: [{ validator: validate.contractNameValidate, trigger: 'change' }],
        // 项目公司名称
        debtorId: [
          { required: true, trigger: 'change', message: this.$i18n.messages[this.$i18n.locale].common.pleaseSelect + this.$i18n.messages[this.$i18n.locale].ladingManageList.projectName },
          { validator: validate.enterpriseNameValidate, trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    api: function () {
      return ladingManageApi.ladingManageList
    }
  },
  methods: {
    invoiceNumValidate (rule, value, callback) {
      return 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 (data.length > 0 && len === 1) {
      //     callback(new Error(this.$i18n.messages[this.$i18n.locale].ladingManageList.inputMuinvoiceNumCheck1))
      //   }
      //   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()
      // }
    },
    accountCheck (rule, value, callback) {
      if (this.UtilJs.isEmpty(value) || regExp.predictLadingAmount.test(value)) {
        callback()
      } else {
        callback(new Error('整数部分14，小数2位'))
      }
    },
    taxExcludingAmountCheck (rule, value, callback) {
      if (value) {
        value = value.toString()
        value = value.replace(/,/g, '')
        this.accountCheck(rule, value, callback)
      } else {
        callback()
      }
    },
    // 保存
    onSaved () {
      this.saveLoading = true
      let temp = Object.assign({}, this.form)
      let keys = Object.keys(temp)
      // 区域专员
      temp.coreAreaAttache = temp.coreAreaAttache.length === 0 ? '-1' : temp.coreAreaAttache
      // 供应商名称
      temp.creditorId = temp.creditorId.length === 0 ? '-1' : temp.creditorId
      // 区域
      temp.coreArea = temp.coreArea.length === 0 ? '-1' : temp.coreArea
      // 收款人名称
      temp.receiverName = temp.receiverName.length === 0 ? ' ' : temp.receiverName
      // 项目名称
      temp.projectContractName = temp.projectContractName.length === 0 ? ' ' : temp.projectContractName
      // 区域专员备注
      temp.coreAreaRemark = temp.coreAreaRemark.length === 0 ? ' ' : temp.coreAreaRemark
      // 开户行
      temp.receiverBank = temp.receiverBank.length === 0 ? ' ' : temp.receiverBank
      // 收款账号
      temp.receiverBankAccount = temp.receiverBankAccount.length === 0 ? ' ' : temp.receiverBankAccount
      // 合同名称
      temp.basicContractName = temp.basicContractName.length === 0 ? ' ' : temp.basicContractName
      // 合同编号
      temp.basicContractCode = temp.basicContractCode.length === 0 ? ' ' : temp.basicContractCode
      // 发票号码
      temp.invoiceNo = temp.invoiceNo.length === 0 ? ' ' : temp.invoiceNo
      keys.forEach(it => {
        if (!temp[it] && this.form[it] !== 0) {
          // 发票金额\应收账款金额
          if (it === 'taxExcludingAmount' || it === 'factoringMoney') {
            temp[it] = '0'
          // } else {
          //   temp[it] = ' '
          }
        } else {
          // 发票金额\应收账款金额
          if (it === 'taxExcludingAmount' || it === 'factoringMoney') {
            temp[it] = temp[it].toString().replace(/,/g, '')
          }
        }
      })
      temp.modifyUserId = this.$store.state.user.user.userId
      temp.modifyUserName = this.$store.state.user.user.userName
      this.api.updateAsset(temp).then(res => {
        if (res && res.data && res.success) {
          this.success(this.$i18n.messages[this.$i18n.locale].common.saveSuccess)
          this.$emit('close')
          this.dialogVisible = false
        } else {
          this.saveLoading = false
          this.error(this.$i18n.messages[this.$i18n.locale].common.saveError)
        }
      }).catch(res => {
        this.saveLoading = false
      })
    },
    selectChanged (obj, index) {
      if (obj.hasOwnProperty('suppliersInfo')) {
        if (!this.UtilJs.isEmpty(obj.suppliersInfo)) {
          this.form.creditor = obj.suppliersInfo.enterpriseName
        } else {
          this.form.creditor = ' '
        }
      }
      // if (!this.UtilJs.isEmpty(obj.projectInfo)) {
      //   this.form.projectName = obj.projectInfo.projectName
      // } else {
      //   this.form.projectName = ''
      // }
      if (obj.hasOwnProperty('coreAreaInfo')) {
        if (!this.UtilJs.isEmpty(obj.coreAreaInfo)) {
          this.form.coreAreaName = obj.coreAreaInfo.areaName
        } else {
          this.form.coreAreaName = ' '
        }
      }
      if (obj.hasOwnProperty('coreAreaAttacheInfo')) {
        if (!this.UtilJs.isEmpty(obj.coreAreaAttacheInfo)) {
          this.form.coreAreaAttacheName = obj.coreAreaAttacheInfo.areaCommissionerName
        } else {
          this.form.coreAreaAttacheName = ' '
        }
      }
      if (obj.hasOwnProperty('debtorInfo')) {
        if (!this.UtilJs.isEmpty(obj.debtorInfo)) {
          this.form.debtor = obj.debtorInfo.enterpriseName
        } else {
          this.form.debtor = ' '
        }
      }
    }
  },
  watch: {
    'form.coreArea' (n, o) {
      if (o === undefined && n) {
      } else {
        this.form.coreAreaAttacheName = ' '
        this.form.coreAreaAttache = ''
      }
    },
    dialogVisible (newVal) {
      if (newVal) {
        this.saveLoading = false
        this.$nextTick(() => {
          this.$set(this.form, 'ladingWay', this.form.ladingWay.toString())
          this.$set(this.form, 'planLadingConfirmStatus', this.form.planLadingConfirmStatus.toString())
          this.$set(this.form, 'officalLadingConfirmStatus', this.form.officalLadingConfirmStatus.toString())
          this.$set(this.form, 'ladingType', this.form.ladingType.toString())
          this.loadRefinis = !this.loadRefinis
          this.form.taxExcludingAmount = format.thousandFilter(this.form.taxExcludingAmount)
          this.form.factoringMoney = format.thousandFilter(this.form.factoringMoney)
          this.form.specialPlanPeriods = !this.form.specialPlanPeriods || this.form.specialPlanPeriods.toString() === '0' ? '' : this.form.specialPlanPeriods
          if (!this.form.coreArea || this.form.coreArea.toString() === '0') this.form.coreArea = ''
          if (!this.form.coreArea || this.form.coreAreaAttache.toString() === '0') this.form.coreAreaAttache = ''
          if (!this.form.coreArea || this.form.creditorId.toString() === '0') this.form.creditorId = ''
          let keys = Object.keys(this.form)
          this.$set(this.componentList[7].attrs, 'defaultOption', [{ keyId: this.form.creditorId, enterpriseName: this.form.creditor }])
          keys.forEach(it => {
            if (this.form[it] === ' ' || this.form[it] === '-1') {
              this.$set(this.form, it, '')
            }
          })
          this.$nextTick(() => {
            if (this.$refs.dialogDym.$refs.editForm) {
              this.$refs.dialogDym.$refs.editForm.clearValidate()
            }
          })
        })
      } else {
        this.form.coreArea = undefined
      }
    }
  }
}
</script>
