<!--异常数据调整-->
<template>
  <div>
    <dialog-edit :visible.sync="dialogVisible" :title="$t('ladingManageList.abnormalDataAdjustment')" width="1000px" :api="api" :form="form"
                 :loading="saveLoading" :openPerms="true" :menuCode="menuCode" savePerms="adjuSave"
                 @onSaved="save" :autoSave="false" :editModel="editModel">
      <data-table :operateBtnLayout="operateBtnLayout" :columns="columns" :cellStyle="cellStyle"
                  :menuFlag="false" :sourceData.sync="sourceData" :showPage="false">
      </data-table>
      <data-table :operateBtnLayout="operateBtnLayout1" :columns="columns1" :headTitle="$t('ladingManageList.abnormalInfoList')"
                  :menuFlag="false" :sourceData.sync="sourceData1" :showPage="false" max-height="400">
        <template slot="importValue" slot-scope="scope">
          <span v-if="scope.data.row.projectEn === 'contractType'">{{scope.data.row.importValue | formatDict('ZD0107')}}</span>
          <span v-else-if="scope.data.row.projectEn === 'factoringApplyTime'">{{scope.data.row.importValue | formatCheckDatetime()}}</span>
          <span v-else-if="scope.data.row.projectEn === 'modifyTime'">{{scope.data.row.importValue | formatCheckDatetime()}}</span>
          <span v-else-if="scope.data.row.projectEn === 'pushTime'">{{scope.data.row.importValue | formatCheckDatetime()}}</span>
          <span v-else>{{scope.data.row.importValue}}</span>
        </template>
        <template slot="showCom" slot-scope="scope">
          <select-dictionary v-if="scope.data.row.projectEn === 'contractType'" v-model="scope.data.row.modifyValue" dictTypeCode="ZD0107"></select-dictionary>
<!--          &lt;!&ndash;供应商&ndash;&gt;-->
<!--          <select-supplier v-model="scope.data.row.modifyValue"  v-else-if="scope.data.row.projectEn === 'creditor'"></select-supplier>-->
<!--          &lt;!&ndash;项目公司&ndash;&gt;-->
<!--          <select-project-company-name v-model="scope.data.row.modifyValue"  v-else-if="scope.data.row.projectEn === 'debtor'"></select-project-company-name>-->
          <!--项目名称-->
          <select-product-name v-model="scope.data.row.modifyValue"  v-else-if="scope.data.row.projectEn === 'projectContractName'"></select-product-name>
          <el-date-picker v-else-if="scope.data.row.projectEn === 'factoringApplyTime'" v-model="scope.data.row.modifyValue" value-format="timestamp" format="yyyy-MM-dd" type="date" :placeholder='$t("common.pleaseSelect")'></el-date-picker>
          <el-date-picker v-else-if="scope.data.row.projectEn === 'modifyTime'" v-model="scope.data.row.modifyValue" value-format="timestamp" format="yyyy-MM-dd" type="date" :placeholder='$t("common.pleaseSelect")'></el-date-picker>
          <el-date-picker v-else-if="scope.data.row.projectEn === 'pushTime'" v-model="scope.data.row.modifyValue" value-format="timestamp" format="yyyy-MM-dd" type="date" :placeholder='$t("common.pleaseSelect")'></el-date-picker>
          <el-input v-else-if="scope.data.row.projectEn === 'factoringMoney'" v-model="scope.data.row.modifyValue" :placeholder='$t("common.pleaseInput")'></el-input>
          <el-input v-else-if="scope.data.row.projectEn === 'taxExcludingAmount'" v-model="scope.data.row.modifyValue" :placeholder='$t("common.pleaseInput")'></el-input>
          <el-input v-else v-model="scope.data.row.modifyValue" :placeholder='$t("common.pleaseInput")'></el-input>
        </template>
      </data-table>
    </dialog-edit>
  </div>
</template>

<script>
import { DialogEdit, DataTable, SelectDictionary,
  SelectSupplier, SelectProjectCompanyName, SelectProductName } from 'newepoch-components'
import BaseEditPage from '@/pages/baseEditPage'
import ladingManageApi from '@/api/ladingManageApi'
import * as format from '@/filters/index'
import SelectSupplierName from '@/components/element/shourang/SelectSupplierName'
export default {
  extends: new BaseEditPage(''),
  components: {
    DialogEdit,
    DataTable,
    SelectSupplierName,
    SelectDictionary,
    SelectSupplier,
    // 项目公司
    SelectProjectCompanyName,
    SelectProductName
  },
  props: {
    menuCode: {
      type: String,
      default: null
    }
  },
  data () {
    return {
      saveLoading: false,
      operateBtnLayout: '',
      operateBtnLayout1: '',
      sourceData: [],
      sourceData1: [],
      preSourceData: [],
      columns: [
        // 供应商名称-债权人
        {
          prop: 'creditor',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.supplierName,
          width: '200px'
        },
        // 收款人名称
        {
          prop: 'receiverName',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.payee,
          width: '200px'
        },
        // 项目名称
        {
          prop: 'projectContractName',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.projectName,
          width: '200px'
        },
        // 区域
        {
          prop: 'coreAreaName',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].common.area,
          width: '200px'
        },
        // 区域编号
        {
          prop: 'regionCode',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.areaCode,
          width: '200px'
        },
        // 区域专员
        {
          prop: 'coreAreaAttacheName',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.areaCommissioner,
          width: '200px'
        },
        // 区域专员备注
        {
          prop: 'coreAreaRemark',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.areaCommissionerMemo,
          width: '200px'
        },
        // 产品名称
        {
          prop: 'projectName',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].common.productName,
          width: '200px'
        },
        // 期数
        {
          prop: 'specialPlanPeriods',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].common.periodsNum,
          width: '200px'
        },
        // 核心企业
        {
          prop: 'coreEnterpriseName',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].common.coreEnterprises,
          width: '200px'
        },
        // 计划管理人/再保理银行
        {
          prop: 'planningManagerName',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].common.refactorBank,
          width: '200px'
        },
        // 提单类型
        {
          prop: 'ladingType',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.ladingType,
          width: '200px',
          format: (row) => { return row.ladingType ? format.formatDict(row.ladingType.toString(), 'ZD0100') : '' }
        },
        // 原始权益人
        {
          prop: 'initialOwnersName',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.rightsHolder,
          width: '200px'
        },
        // 资产服务机构
        {
          prop: 'assetServiceName',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.assetServiceOrgan,
          width: '200px'
        },
        // 应收账款金额
        {
          prop: 'factoringMoney',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.accountReceivable,
          width: '200px',
          format: (row) => { return format.thousandFilter(row.factoringMoney) }
        },
        // 项目公司名称
        {
          prop: 'debtor',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.projectCompanyName,
          width: '200px'
        },
        // 开户行
        {
          prop: 'receiverBank',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.openingBank,
          width: '200px'
        },
        // 收款账号
        {
          prop: 'receiverBankAccount',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.payAccount,
          width: '200px'
        },
        // 合同名称
        {
          prop: 'basicContractName',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.contractName,
          width: '200px'
        },
        // 合同编号
        {
          prop: 'basicContractCode',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.contractNum,
          width: '200px'
        },
        // 发票号码
        {
          prop: 'invoiceNo',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.invoiceNum,
          width: '200px'
        },
        // 发票金额
        {
          prop: 'taxExcludingAmount',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.invoiceAmount,
          width: '200px',
          format: (row) => { return format.thousandFilter(row.taxExcludingAmount) }
        },
        // 预提单确认状态
        {
          prop: 'planLadingConfirmStatus',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.advanceLadingStatus,
          width: '200px',
          format: (row) => { return format.formatDict(row.planLadingConfirmStatus, 'ZD0102') }
        },
        // 正式提单确认状态
        {
          prop: 'officalLadingConfirmStatus',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.formalLadingStatus,
          width: '200px',
          format: (row) => { return format.formatDict(row.officalLadingConfirmStatus, 'ZD0103') }
        },
        // 提单方式
        {
          prop: 'ladingWay',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.ladingWay,
          width: '200px',
          format: (row) => { return format.formatDict(row.ladingWay, 'ZD0101') }
        }
      ],
      columns1: [
        // 项目
        {
          prop: 'projectName',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.project,
          width: '200px'
        },
        // 导入值
        {
          prop: 'importValue',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.importVal,
          width: '200px',
          slot: true
        },
        // 异常原因
        {
          prop: 'errorCause',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.abnormalReason,
          width: '200px'
        },
        // 修改值
        {
          prop: 'showCom',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].ladingManageList.modifiedVal,
          width: '200px',
          slot: true
        }
      ]
    }
  },
  mounted () {
  },
  computed: {
    api: function () {
      return ladingManageApi.abnormalDataAdjustmentEdit
    }
  },
  methods: {
    cellStyle ({ row, column, rowIndex, columnIndex }) {
      if (row.errorFields instanceof Array) {
        let flag = false
        row.errorFields.forEach(it => {
          if (it === column.property) {
            flag = true
          }
        })
        if (flag) {
          return 'color: red'
        } else {
          return ''
        }
      } else {
        return ''
      }
    },
    getCompoentName (row) {
      return format.formatDict(row.projectId, 'ZD0109')
    },
    getDictTypeCode (row) {
      return format.formatDictMemo(row.projectId, 'ZD0100')
    },
    save () {
      let $this = this
      // let arr = []
      // this.sourceData1.forEach(it => {
      //   if (!$this.UtilJs.isEmpty(it.importSelectId)) {
      //     arr.push(it)
      //   }
      // })
      // if (arr.length === 0) {
      //   this.warning('没有可更新的数据')
      //   return
      // }
      // let result = true
      // $this.sourceData1.forEach(it => {
      //   if (it.projectEn === 'factoringMoney' || it.projectEn === 'taxExcludingAmount') {
      //     if (!regExp.yuan.test(it.modifyValue)) {
      //       result = false
      //     }
      //   }
      // })
      // if (!result) {
      //   return $this.error(this.$i18n.messages[this.$i18n.locale].ladingManageList.jineValid)
      // }
      let obj = {
        assetInfo: {},
        checkLogList: []
      }
      obj.assetInfo = this.sourceData[0]
      obj.checkLogList = this.deleteEmpty($this.sourceData1)
      if (obj.checkLogList.length === 0) {
        return $this.warning(this.$i18n.messages[this.$i18n.locale].ladingManageList.saveCheckBlock)
      }
      this.saveLoading = true
      ladingManageApi.abnormalDataAdjustmentEdit.updateAdjust(obj).then(res => {
        if (res.success) {
          $this.dialogVisible = false
          this.$emit('close')
        } else {
          this.saveLoading = false
          $this.error(this.$i18n.messages[this.$i18n.locale].common.saveError)
        }
      }).catch(res => {
        this.saveLoading = false
      })
    },
    deleteEmpty (tableData) {
      return tableData.filter(it => {
        return this.preSourceData.some(it1 => {
          return it1.projectEn === it.projectEn && it1.modifyValue.toString() !== it.modifyValue.toString()
        })
      })
    }
  },
  watch: {
    dialogVisible (newVal) {
      if (newVal) {
        this.saveLoading = false
        let $this = this
        $this.sourceData1 = []
        ladingManageApi.abnormalDataAdjustmentEdit.getAdjust({ keyId: this.form.keyId }).then(res => {
          if (res.success) {
            if (res.data.assetInfo && res.data.assetInfo.keyId && res.data.assetInfo.poolInfo) {
              let keyId = res.data.assetInfo.keyId
              res.data.assetInfo = Object.assign({}, res.data.assetInfo, res.data.assetInfo.poolInfo)
              res.data.assetInfo.keyId = keyId
            }
            $this.sourceData.push(res.data.assetInfo)
            $this.sourceData1.push(...res.data.checkLogList)
            $this.preSourceData = JSON.parse(JSON.stringify(res.data.checkLogList))
          } else {
            $this.error(this.$i18n.messages[this.$i18n.locale].ladingManageList.getAbnormalInfoFailed)
          }
        })
      } else {
        this.sourceData = []
        this.sourceData1 = []
      }
    }
  }
}
</script>
