<!--批量设置产品信息-->
<template>
  <div>
    <dialog-edit ref="dialog" :visible.sync="dialogVisible" :title="$t('ladingManageList.batchSetProductInfoTitle')" width="600px" :api="api" :form="localForm" :editModel="editModel"
                 :formRules="formRules" @onSaved="save" :autoSave="false" :loading="saveLoading">
      <dym-form :formValue.sync="localForm" :componentList="componentList" @selectChanged="batchProductInfoEditselectChanged"></dym-form>
    </dialog-edit>
    <import-result-edit :visible.sync="resultFlag" :editModel="resultModel" :form="resultItem"></import-result-edit>
  </div>
</template>

<script>
import { DialogEdit, dymForm, authButton, btnAutoImport, SelectPrimitiveRightsHolderCascade,
  SelectAssetsServiceCascade,
  SelectProductName, SelectPeriodsCascade, SelectCoreEnterpriseCascade, SelectFinancialInstitutionsCascade } from 'newepoch-components'
import BaseEditPage from '@/pages/baseEditPage'
import ladingManageApi from '@/api/ladingManageApi'
import ImportResultEdit from '@/pages/shouji/ladingManage/importResultEdit.vue'
import componentsApi from '@/api/componentsApi'

export default {
  extends: new BaseEditPage(''),
  components: {
    DialogEdit,
    dymForm,
    authButton,
    btnAutoImport,
    ImportResultEdit
  },
  data () {
    return {
      localForm: {},
      resultFlag: false,
      resultModel: '',
      resultItem: {},
      saveLoading: false,
      // formdata 块
      fm: new FormData(),
      formRules: {
        projectId: [{
          required: true,
          // message: '请选择产品名称',
          message: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.selectproductName,
          trigger: 'change'
        }],
        specialPlanId: [{
          required: true,
          message: '请选择期数',
          trigger: 'change'
        }],
        coreEnterpriseId: [{
          required: true,
          // message: '请选择核心企业',
          message: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.selectcoreEnterprise,
          trigger: 'change'
        }],
        planningManagerId: [{
          required: true,
          // message: '请选择计划管理人/再保理银行',
          message: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.selectplanningManagerName,
          trigger: 'change'
        }],
        ladingType: [{
          required: true,
          // message: '请选择提单类型',
          message: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.selectladingType,
          trigger: 'change'
        }],
        assetServiceId: [{
          required: true,
          // message: '请选择资产服务机构',
          message: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.selectassetServiceOrgan,
          trigger: 'change'
        }],
        initialOwnersId: [{
          required: true,
          // message: '请选择原始权益人',
          message: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.ladingNoticeManage,
          trigger: 'change'
        }],
        factoringApplyTime: [{
          required: true,
          // message: '请选择批次日期',
          message: this.$i18n.messages[this.$i18n.locale].ladingPlanManageList.pleaseSelectBatchDateTime,
          trigger: 'change'
        }]
      },
      componentList: [
        // 产品名称
        {
          label: this.$i18n.messages[this.$i18n.locale].common.productName,
          value: 'projectId',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseSelect,
          cols: [24, 24, 24, 24],
          componentName: SelectProductName,
          prop: 'projectId',
          name: 'projectId',
          labelWidth: '170px'
        },
        // 期数
        {
          label: this.$i18n.messages[this.$i18n.locale].common.periodsNum,
          value: 'specialPlanId',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseSelect,
          cols: [24, 24, 24, 24],
          disabled: false,
          componentName: SelectPeriodsCascade,
          queryName: 'projectId',
          queryValue: 'projectId',
          prop: 'specialPlanId',
          name: 'specialPlanId',
          labelWidth: '170px'
        },
        // 核心企业
        {
          label: this.$i18n.messages[this.$i18n.locale].common.coreEnterprises,
          value: 'coreEnterpriseId',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseSelect,
          cols: [24, 24, 24, 24],
          componentName: SelectCoreEnterpriseCascade,
          queryName: 'projectId',
          queryValue: 'projectId',
          prop: 'coreEnterpriseId',
          name: 'coreEnterpriseId',
          labelWidth: '170px'
        },
        // 计划管理人/再保理银行
        {
          label: this.$i18n.messages[this.$i18n.locale].common.refactorBank,
          value: 'planningManagerId',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseSelect,
          cols: [24, 24, 24, 24],
          queryName: 'projectId',
          queryValue: 'projectId',
          componentName: SelectFinancialInstitutionsCascade,
          prop: 'planningManagerId',
          name: 'planningManagerId',
          labelWidth: '170px'
        },
        // 提单类型
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingPlanManageList.ladingType,
          value: 'ladingType',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseSelect,
          cols: [24, 24, 24, 24],
          componentName: 'SelectDictionary',
          dictTypeCode: 'ZD0100',
          store: this.$store,
          prop: 'ladingType',
          labelWidth: '170px'
        },
        // 资产服务机构
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingPlanManageList.assetServiceOrgan,
          value: 'assetServiceId',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseSelect,
          cols: [24, 24, 24, 24],
          componentName: SelectAssetsServiceCascade,
          queryName: 'projectId',
          queryValue: 'projectId',
          prop: 'assetServiceId',
          name: 'assetServiceId',
          labelWidth: '170px'
        },
        // 原始权益人
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingPlanManageList.rightsHolder,
          value: 'initialOwnersId',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseSelect,
          cols: [24, 24, 24, 24],
          componentName: SelectPrimitiveRightsHolderCascade,
          queryName: 'projectId',
          queryValue: 'projectId',
          prop: 'initialOwnersId',
          name: 'initialOwnersId',
          labelWidth: '170px'
        },
        // 批次信息
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingPlanManageList.batchInfo,
          value: 'batchInfo',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          cols: [24, 24, 24, 24],
          componentName: 'el-input',
          prop: 'batchInfo',
          labelWidth: '170px'
        }
      ],
      batchInfoDate: [
        // 批次信息
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingPlanManageList.batchInfo,
          value: 'batchInfo',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          cols: [24, 24, 24, 24],
          componentName: 'el-input',
          prop: 'batchInfo',
          labelWidth: '170px'
        },
        // 批次日期
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingPlanManageList.batchDateTime,
          value: 'factoringApplyTime',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseSelect,
          cols: [24, 24, 24, 24],
          componentName: 'el-date-picker',
          prop: 'factoringApplyTime',
          valueFormat: 'timestamp',
          labelWidth: '170px'
        }
      ]
    }
  },
  mounted () {
  },
  computed: {
    api: function () {
      return ladingManageApi.ladingManageList
    }
  },
  methods: {
    batchProductInfoEditselectChanged (obj, index) {
      if (obj.projectIdInfo) {
        this.localForm.projectName = obj.projectIdInfo.projectName
        this.getTypeById()
      }
      if (obj.specialPlanIdInfo) {
        this.localForm.specialPlanPeriods = obj.specialPlanIdInfo.specialPlanPeriods
      }
      if (obj.coreEnterpriseIdInfo) {
        this.localForm.coreEnterpriseName = obj.coreEnterpriseIdInfo.enterpriseName
      }
      if (obj.planningManagerIdInfo) {
        this.localForm.planningManagerName = obj.planningManagerIdInfo.enterpriseName
      }
      if (obj.assetServiceIdInfo) {
        this.localForm.assetServiceName = obj.assetServiceIdInfo.enterpriseName
      }
      if (obj.initialOwnersIdInfo) {
        this.localForm.initialOwnersName = obj.initialOwnersIdInfo.enterpriseName
      }
      if (Object.keys(obj).some(it => it === 'projectIdInfo')) {
        this.clearInfoList()
        if (!this.localForm.projectId) {
          this.formRules.specialPlanId[0].required = true
          this.componentList[1].disabled = false
        }
      }
    },
    // upload (file) {
    //   this.fm = new FormData()
    //   this.form.fileName = file.name
    //   this.form.importSource = '10' // 导入来源
    //   this.fm.append('file', file)
    //   // this.form.file = file
    // },
    save () {
      this.saveLoading = true
      let $this = this
      let parmas = []
      this.form.data.forEach(it => {
        $this.localForm.assetcollectAssetInfoId = it.keyId
        parmas.push(JSON.parse(JSON.stringify($this.localForm)))
      })
      ladingManageApi.ladingPlanManageList.batchSetProductInfo(parmas).then(res => {
        if (res && res.data && res.success) {
          this.dialogVisible = false
          $this.$emit('close')
          $this.success(this.$i18n.messages[this.$i18n.locale].ladingManageList.batchSetProductInfoOk)
        } else {
          this.saveLoading = false
          $this.error(this.$i18n.messages[this.$i18n.locale].ladingManageList.batchSetProductInfoFaile)
        }
      }).catch(res => {
        this.saveLoading = false
      })
    },
    // 根据ID获取产品类别
    getTypeById () {
      componentsApi.productenterprise.getclassify({ projectId: this.localForm.projectId }).then(res => {
        this.localForm.isAbs = res.data ? res.data.productType : '2'
        this.selectChange(this.localForm.isAbs)
        // this.formRules.specialPlanId[0].required = this.localForm.isAbs === '1'
        // this.componentList[1].disabled = this.localForm.isAbs !== '1'
      })
    },
    // 改变选择信息、日期
    selectChange (isAbs) {
      this.formRules.specialPlanId[0].required = isAbs === '1'
      this.componentList[1].disabled = isAbs !== '1'
      if (isAbs === '1') {
        // 期数-->批次信息
        this.componentList[7] = this.batchInfoDate[0]
      } else {
        // 日期-->批次日期
        this.componentList[7] = this.batchInfoDate[1]
      }
    },
    clearInfoList () {
      delete this.localForm.specialPlanId
      delete this.localForm.coreEnterpriseId
      delete this.localForm.planningManagerId
      delete this.localForm.assetServiceId
      delete this.localForm.initialOwnersId
      delete this.localForm.planningManagerId
      this.$nextTick(() => {
        if (this.$refs.dialog && this.$refs.dialog.$refs.editForm) {
          this.$refs.dialog.$refs.editForm.clearValidate()
        }
      })
    }
  },
  watch: {
    dialogVisible (newVal) {
      if (newVal) {
        this.selectChange('1')
        this.saveLoading = false
      } else {
        this.localForm = Object.create({})
      }
    }
  }
}
</script>

<style lang="scss" scoped src="@/styles/shouji/ladingManage/importEdit.scss">
</style>
