<!--提单统筹编辑-->
<template>
  <div>
    <dialog-edit :visible.sync="dialogVisible" :title="$t('ladingPlanManageList.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, validate, SelectSmallArea } from 'newepoch-components'
import BaseEditPage from '@/pages/baseEditPage'
import ladingManageApi from '@/api/ladingManageApi'
import SelectSupplierName from '@/components/element/shourang/SelectSupplierName'
import SelectEntryName from '@/components/element/shouji/SelectEntryName'
import SelectArea from '@/components/element/shourang/SelectArea'
import SelectRegionAttache from '@/components/element/shourang/SelectRegionAttache'
import SelectCompanyName from '@/components/element/shourang/SelectCompanyName'
export default {
  extends: new BaseEditPage(''),
  components: {
    DialogEdit,
    dymForm,
    SelectSupplierName,
    SelectEntryName,
    SelectArea,
    SelectRegionAttache,
    SelectCompanyName
  },
  data () {
    return {
      saveLoading: false,
      formRules: {
        // 应收账款金额
        factoringMoney: [{ validator: this.taxExcludingAmountCheck, trigger: 'change' }],
        // 开户行
        receiverBank: [{ validator: validate.openingBankNameValidate, trigger: 'change' }],
        // 收款账号
        receiverBankAccount: [{ validator: validate.openingBankAccountValidate, trigger: 'change' }],
        // 收款人名称
        receiverName: [{ validator: validate.nameValidate, trigger: 'change' }],
        // 区域编号
        regionCode: [{ validator: validate.areaNumValidate, trigger: 'change' }],
        // 发票号码
        invoiceNo: [{ validator: validate.invoiceNumValidate, trigger: 'change' }],
        // 发票金额
        taxExcludingAmount: [{ validator: this.taxExcludingAmountCheck, trigger: 'change' }],
        // 合同编号
        basicContractCode: [{ validator: validate.contractNumValidate, trigger: 'change' }],
        // 合同名称
        basicContractName: [{ validator: validate.contractNameValidate, trigger: 'change' }]
      },
      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',
          clearable: true,
          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',
          clearable: true,
          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',
          clearable: true,
          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',
          clearable: true,
          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',
          clearable: true,
          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',
          clearable: true,
          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',
          clearable: true,
          disabled: true
        },
        // 供应商名称
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.supplierName,
          labelWidth: '160px',
          value: 'creditor',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: SelectSupplierName,
          prop: 'creditor',
          clearable: true,
          name: 'suppliers'
        },
        // 收款人名称
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.payee,
          labelWidth: '160px',
          value: 'receiverName',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'el-input',
          clearable: true,
          prop: 'receiverName'
        },
        // 项目名称
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.projectName,
          labelWidth: '160px',
          value: 'projectContractName',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'el-input',
          clearable: true,
          prop: 'projectContractName'
        },
        // 区域
        {
          label: this.$i18n.messages[this.$i18n.locale].common.area,
          labelWidth: '160px',
          value: 'region',
          placeholder: '',
          cols: [8, 8, 8, 8],
          queryName: 'coreCompanyId',
          queryValue: 'coreEnterpriseId',
          componentName: SelectSmallArea,
          clearable: true,
          prop: 'region'
        },
        // 区域编号
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.areaCode,
          labelWidth: '160px',
          value: 'regionCode',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'el-input',
          clearable: true,
          prop: 'regionCode'
        },
        // 区域专员
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.areaCommissioner,
          labelWidth: '160px',
          value: 'coreAreaAttache',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: SelectRegionAttache,
          prop: 'coreAreaAttache',
          clearable: true,
          name: 'coreAreaAttache'
        },
        // 区域专员备注
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.areaCommissionerMemo,
          labelWidth: '160px',
          value: 'coreAreaRemark',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'el-input',
          clearable: true,
          prop: 'coreAreaRemark'
        },
        // 应收账款金额
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.accountReceivableLabel,
          labelWidth: '160px',
          value: 'factoringMoney',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'el-input',
          clearable: true,
          prop: 'factoringMoney'
        },
        // 项目合同名称
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.projectCompanyName,
          labelWidth: '160px',
          value: 'debtor',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'el-input',
          clearable: true,
          prop: 'debtor'
          // name: 'projectCompany'
        },
        // 开户行
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.openingBank,
          labelWidth: '160px',
          value: 'receiverBank',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'el-input',
          clearable: true,
          prop: 'receiverBank'
        },
        // 收款账号
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.payAccount,
          labelWidth: '160px',
          value: 'receiverBankAccount',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'el-input',
          clearable: true,
          prop: 'receiverBankAccount'
        },
        // 合同名称
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.contractName,
          labelWidth: '160px',
          value: 'basicContractName',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'el-input',
          clearable: true,
          prop: 'basicContractName'
        },
        // 合同编号
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.contractNum,
          labelWidth: '160px',
          value: 'basicContractCode',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'el-input',
          clearable: true,
          prop: 'basicContractCode'
        },
        // 发票号码
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.invoiceNum,
          labelWidth: '160px',
          value: 'invoiceNo',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'el-input',
          clearable: true,
          maxlength: 8,
          prop: 'invoiceNo'
        },
        // 发票金额
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.invoiceAmountLabel,
          labelWidth: '160px',
          value: 'taxExcludingAmount',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'el-input',
          clearable: true,
          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',
          prop: 'planLadingConfirmStatus',
          clearable: true,
          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',
          prop: 'officalLadingConfirmStatus',
          clearable: true,
          disabled: true
        },
        // 提单方式
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.ladingMode,
          labelWidth: '160px',
          value: 'ladingWay',
          placeholder: '',
          cols: [8, 8, 8, 8],
          componentName: 'SelectDictionary',
          dictTypeCode: 'ZD0100',
          prop: 'ladingWay',
          clearable: true,
          disabled: true
        }
      ]
    }
  },
  mounted () {
  },
  computed: {
    api: function () {
      return ladingManageApi.ladingManageList
    }
  },
  methods: {
    // 应收账款金额、发票金额 校验
    taxExcludingAmountCheck (rule, value, callback) {
      if (value) {
        value = value.toString()
        value = value.replace(/,/g, '')
        validate.yuanValidate(rule, value, callback)
      } else {
        callback()
      }
    },
    // 保存
    onSaved (data) {
      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, '')
        }
      })
      this.saveLoading = true
      this.api.updateAsset(this.form).then(res => {
        if (res && res.data) {
          this.success(this.$i18n.messages[this.$i18n.locale].common.saveSuccess)
          this.dialogVisible = false
        } 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.coreAreaAttacheInfo)) {
        this.form.coreAreaAttacheName = obj.coreAreaAttacheInfo.areaCommissionerName
      } else {
        this.form.coreAreaAttacheName = ''
      }
      if (!this.UtilJs.isEmpty(obj.projectCompanyInfo)) {
        this.form.projectCompanyName = obj.projectCompanyInfo.projectCompanyName
      } else {
        this.form.projectCompanyName = ''
      }
    }
  },
  watch: {
    dialogVisible (newVal) {
      if (newVal) {
        this.saveLoading = false
        this.$set(this.form, 'ladingWay', this.form.ladingWay.toString())
      } else {
        this.$emit('close')
      }
    }
  }
}
</script>
