<!--设置保理日期-->
<template>
  <div>
    <dialog-edit :visible.sync="dialogVisible"
                 :title="$t('guideSheetManage.batchSetFactApplicationDate')"
                 width="700px"
                 :api="api"
                 :form="form"
                 :autoSave="false"
                 :footerVisible="true"
                 :saveFlag="false"
                 @onSaved="onSave"
                 :editModel="editModel">
      <dym-form :formValue.sync="newFormValue"
                :componentList="componentList"></dym-form>
      <template slot="footer">
        <auth-button size="medium"
                     :loading="saveLoading" :menuCode="menuCode" perms="setSubmit" :openPerms="true"
                     type="primary"
                     :label="$t('common.submit')"
                     @click="save"></auth-button>
      </template>
    </dialog-edit>
  </div>
</template>

<script>
import { DialogEdit, dymForm, authButton } from 'newepoch-components'
import BaseEditPage from '@/pages/baseEditPage'
import guideSheetManageApi from '@/api/guideSheetManageApi'

export default {
  extends: new BaseEditPage('guideSheetManage'),
  name: 'guideSheetManageBatchSet',
  components: {
    DialogEdit,
    authButton,
    dymForm
  },
  props: {
    menuCode: {
      type: String,
      default: null
    }
  },
  data () {
    return {
      saveLoading: false,
      keyIDItems: [],
      newFormValue: {},
      componentList: [
        {
          // 保理申请日期
          label: this.$i18n.messages[this.$i18n.locale].guideSheetManage.factApplicationDate,
          disabled: false,
          cols: [20, 20, 20, 20],
          value: 'factApplicationDate',
          prop: 'factApplicationDate',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseSelect,
          labelWidth: '160px',
          clearable: true,
          componentName: 'el-date-picker'
        }
      ]
    }
  },
  mounted () {
  },
  computed: {
    api () {
      return guideSheetManageApi.guideSheetManage
    }
  },
  methods: {
    save () {
      let data = []
      this.saveLoading = true
      this.keyIDItems.forEach((it, index) => {
        let time = '1900-01-01'
        if (this.newFormValue.factApplicationDate) {
          time = this.newFormValue.factApplicationDate
        }
        data.push({
          keyId: it.keyId,
          factoringApplyTime: time,
          officalLadingConfirmStatus: it.formalBillConfirmStatus
        })
      })
      guideSheetManageApi.guideSheetManage.getFactoringaPplytime(data).then(res => {
        if (res.data) {
          this.dialogVisible = false
          this.$emit('savefinish')
        } else {
          this.saveLoading = false
        }
      }).catch(res => {
        this.saveLoading = false
      })
    }
  },
  watch: {
    dialogVisible (newVal) {
      if (newVal) {
        this.saveLoading = false
        this.$set(this.newFormValue, 'factApplicationDate', new Date())
        this.form.data.forEach(it => {
          this.keyIDItems.push({ keyId: it.keyId, formalBillConfirmStatus: it.formalBillConfirmStatus })
        })
      } else {
        this.keyIDItems = []
      }
    }
  }
}
</script>
<style scoped lang="scss">
  @import '~@/styles/sj/guideSheetManage/guideSheetManageBatchSet.scss';
</style>
