<template>
  <dialog-edit :visible.sync="dialogVisible" :editModel="editModel" :form="formValue" ref="editRef"
               :title="$t('invoiceInformationList.editPageHead')" :api="api" :autoSave="false" :loading="editLoading"
               :openPerms="true" :menuCode="menuCode" savePerms="btnSavePerms"
               :footerBtnShow="true" :formRules="formRules" @onSaved="save">
    <dym-form :componentList="componentList" :formValue.sync="formValue"></dym-form>
  </dialog-edit>
</template>

<script>
import { DialogEdit, dymForm, authButton, validate } from 'newepoch-components'
import BasePage from '@/pages/baseEditPage'
import invoiceApi from '@/api/invoiceApi'
import * as format from '@/filters/index'
import { regExp } from '@/validator/index'
export default {
  extends: new BasePage(),
  name: 'invoiceInformationEdit',
  components: {
    DialogEdit,
    dymForm,
    authButton
  },
  props: {
    menuCode: {
      type: String,
      default: null
    }
  },
  computed: {
    api () {
      return invoiceApi.assetmgrinvoice
    }
  },
  data () {
    return {
      editLoading: false,
      formValue: {},
      formRules: {
        // 不含税金额
        taxExcludingAmount: [
          { validator: this.accountCheck, trigger: 'change' }
        ],
        // 价税合计
        totalAmount: [
          { validator: this.accountCheck, trigger: 'change' }
        ],
        // 发票号码
        invoiceNo: [
          { validator: validate.invoiceNumValidate, trigger: 'change' }
        ],
        // 银行账号
        bankAccount: [
          { validator: validate.bankAccountValidate, trigger: 'change' }
        ],
        // 发票代码
        invoiceCode: [
          { validator: validate.invoiceCodeValidate, trigger: 'change' }
        ],
        taxRate: [
          { validator: validate.rateValidate, trigger: 'change' }
        ]
      },
      componentList: [
        // 供应商名称
        {
          value: 'buyTaxpayerName',
          disabled: false,
          clearable: true,
          componentName: 'el-input',
          label: this.$i18n.messages[this.$i18n.locale].invoiceInformationList.supplierName,
          labelWidth: '130px',
          placeholder: this.$i18n.messages[this.$i18n.locale].invoiceInformationList.inputsupplierName,
          cols: [24, 12, 8, 8],
          prop: 'buyTaxpayerName'
        },
        // 供应商税号
        {
          value: 'buyTaxpayerNo',
          disabled: false,
          clearable: true,
          componentName: 'el-input',
          label: this.$i18n.messages[this.$i18n.locale].invoiceInformationList.supplierTaxNum,
          labelWidth: '130px',
          placeholder: this.$i18n.messages[this.$i18n.locale].invoiceInformationList.inputsupplierTaxNum,
          cols: [24, 12, 8, 8],
          prop: 'buyTaxpayerNo'
        },
        // 银行账号
        {
          value: 'bankNo',
          disabled: false,
          clearable: true,
          componentName: 'el-input',
          label: this.$i18n.messages[this.$i18n.locale].invoiceInformationList.bankAccount,
          labelWidth: '130px',
          placeholder: this.$i18n.messages[this.$i18n.locale].invoiceInformationList.inputbankAccount,
          cols: [24, 12, 8, 8],
          prop: 'bankNo'
        },
        // 地址电话
        {
          value: 'addressPhone',
          disabled: false,
          clearable: true,
          componentName: 'el-input',
          label: this.$i18n.messages[this.$i18n.locale].invoiceInformationList.addressTelephone,
          labelWidth: '130px',
          placeholder: this.$i18n.messages[this.$i18n.locale].invoiceInformationList.inputaddressTelephone,
          cols: [24, 12, 8, 8],
          prop: 'addressPhone'
        },
        // 发票代码
        {
          value: 'invoiceCode',
          disabled: false,
          clearable: true,
          componentName: 'el-input',
          label: this.$i18n.messages[this.$i18n.locale].invoiceInformationList.invoiceCode,
          labelWidth: '130px',
          placeholder: this.$i18n.messages[this.$i18n.locale].invoiceInformationList.inputinvoiceCode,
          cols: [24, 12, 8, 8],
          prop: 'invoiceCode'
        },
        // 发票号码
        {
          value: 'invoiceNo',
          disabled: false,
          clearable: true,
          componentName: 'el-input',
          label: this.$i18n.messages[this.$i18n.locale].invoiceInformationList.invoiceNum,
          labelWidth: '130px',
          placeholder: this.$i18n.messages[this.$i18n.locale].invoiceInformationList.inputinvoiceNum,
          cols: [24, 12, 8, 8],
          prop: 'invoiceNo'
        },
        // 开票日期
        {
          value: 'openDate',
          disabled: false,
          clearable: true,
          componentName: 'el-date-picker',
          label: this.$i18n.messages[this.$i18n.locale].invoiceInformationList.invoiceDate,
          labelWidth: '130px',
          placeholder: this.$i18n.messages[this.$i18n.locale].invoiceInformationList.selectOpenDate,
          cols: [24, 12, 8, 8],
          prop: 'openDate',
          valueFormat: 'timestamp'
        },
        // 商品名称
        {
          value: 'itemName',
          disabled: false,
          clearable: true,
          componentName: 'el-input',
          label: this.$i18n.messages[this.$i18n.locale].invoiceInformationList.commodityName,
          labelWidth: '130px',
          placeholder: this.$i18n.messages[this.$i18n.locale].invoiceInformationList.inputcommodityName,
          cols: [24, 12, 8, 8],
          prop: 'itemName'
        },
        // 不含税金额
        {
          value: 'taxExcludingAmount',
          disabled: false,
          clearable: true,
          componentName: 'el-input',
          label: this.$i18n.messages[this.$i18n.locale].invoiceInformationList.taxFreeAmount,
          labelWidth: '130px',
          placeholder: this.$i18n.messages[this.$i18n.locale].invoiceInformationList.inputtaxFreeAmount,
          cols: [24, 12, 8, 8],
          prop: 'taxExcludingAmount'
        },
        // 税率
        {
          value: 'taxRate',
          clearable: true,
          disabled: false,
          componentName: 'el-input',
          label: this.$i18n.messages[this.$i18n.locale].invoiceInformationList.taxRate,
          labelWidth: '130px',
          placeholder: this.$i18n.messages[this.$i18n.locale].invoiceInformationList.inputtaxRate,
          cols: [24, 12, 8, 8],
          prop: 'taxRate'
        },
        // 价税合计
        {
          value: 'totalAmount',
          disabled: false,
          clearable: true,
          componentName: 'el-input',
          label: this.$i18n.messages[this.$i18n.locale].invoiceInformationList.TaxTotal,
          labelWidth: '130px',
          placeholder: this.$i18n.messages[this.$i18n.locale].invoiceInformationList.inputTaxTotal,
          cols: [24, 12, 8, 8],
          prop: 'totalAmount'
        },
        // // 保理起息日期
        // {
        //   value: 'factoringStartDate',
        //   disabled: false,
        //   clearable: true,
        //   componentName: 'el-date-picker',
        //   label: this.$i18n.messages[this.$i18n.locale].invoiceInformationList.factorStartDate,
        //   labelWidth: '130px',
        //   placeholder: this.$i18n.messages[this.$i18n.locale].invoiceInformationList.selectfactorStartDate,
        //   cols: [24, 12, 8, 8],
        //   prop: 'factoringStartDate'
        // },
        // // 保理到期日期
        // {
        //   value: 'factoringExpirationDate',
        //   disabled: false,
        //   clearable: true,
        //   componentName: 'el-date-picker',
        //   label: this.$i18n.messages[this.$i18n.locale].invoiceInformationList.factorEndDate,
        //   labelWidth: '130px',
        //   placeholder: this.$i18n.messages[this.$i18n.locale].invoiceInformationList.selectfactorEndDate,
        //   cols: [24, 12, 8, 8],
        //   prop: 'factoringExpirationDate'
        // },
        // 计息日期  保理起息日期 - 保理到期日期
        {
          label: this.$i18n.messages[this.$i18n.locale].invoiceInformationList.accrualDate,
          value: 'accrualDate',
          name: 'accrualDate',
          componentName: 'el-date-picker',
          labelWidth: '130px',
          type: 'daterange',
          cols: [24, 12, 8, 8],
          valueFormat: 'timestamp',
          attrs: {
            startPlaceholder: this.$i18n.messages[this.$i18n.locale].invoiceInformationList.selectfactorStartDate,
            endPlaceholder: this.$i18n.messages[this.$i18n.locale].invoiceInformationList.selectfactorEndDate
          }
        }
      ]
    }
  },
  methods: {
    /**
     * 金额校验
     */
    accountCheck: function (rule, value, callback) {
      if (this.UtilJs.isEmpty(value) || regExp.predictLadingAmount.test(value)) {
        callback()
      } else {
        callback(new Error('整数部分14，小数4位'))
      }
    },
    save (data) {
      let saveValue = JSON.parse(JSON.stringify(this.formValue))
      if (Number(saveValue.taxRate) !== 0) {
        saveValue.taxRate = Number(saveValue.taxRate) / 100
      }
      if (this.UtilJs.isEmpty(saveValue.accrualDate) || this.UtilJs.isEmpty(saveValue.accrualDate[0]) || this.UtilJs.isEmpty(saveValue.accrualDate[1])) {
        saveValue.factoringStartDate = -2209017943000
        saveValue.factoringExpirationDate = -2209017943000
      } else {
        saveValue.factoringStartDate = saveValue.accrualDate[0]
        saveValue.factoringExpirationDate = saveValue.accrualDate[1]
      }
      if (this.UtilJs.isEmpty(saveValue.openDate)) {
        saveValue.openDate = -2209017943000
      }
      // 不含税金额（元）
      saveValue.taxExcludingAmount = this.UtilJs.isEmpty(saveValue.taxExcludingAmount) ? '0' : saveValue.taxExcludingAmount
      // 价税合计（元）
      saveValue.totalAmount = this.UtilJs.isEmpty(saveValue.totalAmount) ? '0' : saveValue.totalAmount
      // 税率（%）
      saveValue.taxRate = this.UtilJs.isEmpty(saveValue.taxRate) ? '0' : saveValue.taxRate
      this.editLoading = true
      invoiceApi.assetmgrinvoice.putSave(saveValue).then(res => {
        if (res.data) {
          this.success(this.$i18n.messages[this.$i18n.locale].documentSealingConfig.editseccessinfo)
          this.dialogVisible = false
          this.$emit('back')
        } else {
          this.error(this.$i18n.messages[this.$i18n.locale].documentSealingConfig.editfialinfo)
        }
      }).finally(() => {
        this.editLoading = false
      })
    },
    parentDialogVisible () {
      this.formValue = {}
      this.$nextTick(() => {
        try {
          this.form.taxRate = format.accMul(Number(this.form.taxRate), 100)
        } catch (e) {
          this.form.taxRate = 0
        }
        this.form.openDate = format.formatDateTpcModifyS(this.form.openDate)
        this.form.factoringStartDate = format.formatDateTpcModifyS(this.form.factoringStartDate)
        this.form.factoringExpirationDate = format.formatDateTpcModifyS(this.form.factoringExpirationDate)
        if (this.form.openDate === '') {
          this.form.openDate = null
        }
        if (this.form.factoringStartDate === '' || this.form.factoringExpirationDate === '') {
          this.form.accrualDate = null
        } else {
          this.form.accrualDate = [this.form.factoringStartDate, this.form.factoringExpirationDate]
        }
        if (this.form.taxExcludingAmount === 0) {
          this.form.taxExcludingAmount = ''
        }
        if (this.form.totalAmount === 0) {
          this.form.totalAmount = ''
        }
        if (this.form.taxRate === 0) {
          this.form.taxRate = ''
        }
        this.formValue = Object.assign({}, this.form)
        this.$nextTick(() => {
          this.$refs.editRef.$refs.editForm.clearValidate()
        })
      })
    }
  }
}
</script>

<style scoped>
</style>
