<template>
  <div>
    <a-modal
      width="1000px"
      title="批号维护"
      :visible="value"
      :destroyOnClose='true'
      :confirm-loading="confirmLoading"
      @ok="handleOkreplenishment"
      @cancel="handleCancel"
    >
      <p-grid-form
        ref="formTarget"
        :span="8"
        :label-col="{ span: 8 }"
        :items="deitalFormItems"
        @submit="hanlderSubmit"
      ></p-grid-form>
    </a-modal>
  </div>
</template>

<script>
import moment from "moment";
import { deitalFormItems } from '../config/outerCheck'
import { handleShowProductionDate } from '../script/lineHandleLogic'
import { postbatchAdjust } from '../api'

export default {
  components: {},
  data() {
    return {
      deitalFormItems: deitalFormItems(this),
      confirmLoading: false,
      Visible: false,
      Fdata: {},
      formTargetData: {}
    }
  },
  props: {
    formData: {
      type: Object,
      default() {
        return {}
      }
    },
    value: {
      type: Boolean,
      default: false
    }
  },
  created() {},
  mounted() {
    this.formTargetData = this.formData
    if (this.formTargetData.batchProductionShowNew) {
      let newDate = this.filterData(this.formTargetData.batchProductionShowNew)
      this.formTargetData.batchProductionShowNew = newDate
    }
    if (this.formTargetData.batchExpiredShowNew) {
        this.formTargetData.batchExpiredShowNew = this.filterData(this.formTargetData.batchExpiredShowNew)
    }
    this.$nextTick(() => {
      this.$refs.formTarget.form.setFieldsValue(this.formTargetData)
    })
  },
  computed: {},
  watch: {
    formData: {
      handler(val) {
        if (val) {
          this.formTargetData = val
          // this.$nextTick(() => {
          //   this.$refs.formTarget.form.setFieldsValue(val)
          // })
        }
      },
      deep: true
      // immediate: true
    },
    formTargetData: {
      handler(val) {
        if (val) {
          if (val.batchProductionShowNew) {
            let newDate = this.filterData(val.batchProductionShowNew)
            val.batchProductionShowNew = newDate
          }
          if (val.batchExpiredShowNew) {
             val.batchExpiredShowNew = this.filterData(val.batchExpiredShowNew)
          }
          this.$nextTick(() => {
            this.$refs.formTarget.form.setFieldsValue(val)
          })
        }
      },
      deep: true
      // immediate: true
    }
  },
  methods: {
    filterData(date) {
      let batcharr = date.split('')
      let batchProductionShowNew = batcharr.filter(item => {
        return item != '-'
      })
      let batchdata = batchProductionShowNew.join('')
      return batchdata
    },
    hanlderSubmit(event, form) {
      event.preventDefault()
      this.$refs.formTarget.form.validateFields((error, values) => {
        if (!error) {
        }
      })
    },
    handleOkreplenishment() {
      this.$refs.formTarget.form.validateFields((error, values) => {
        if (!error) {
          values.batchProductionDateNew = handleShowProductionDate(
            values.batchProductionShowNew.length==6?values.batchProductionShowNew+'01':values.batchProductionShowNew
            )
          if (values.batchExpiredShowNew.length == 8){
            values.batchExpiredDateNew =  handleShowProductionDate(values.batchExpiredShowNew)
          }else if (values.batchExpiredShowNew.length == 6){
            let expiredDate = handleShowProductionDate(values.batchExpiredShowNew+'01')
            let a = new Date(values.batchProductionDateNew);
            a.setDate(a.getDate() + (+this.formData.shelfLifeDays || 1));
            if (+(a) > +(new Date(expiredDate))) {
              values.batchExpiredDateNew = moment(a).format('YYYY-MM-DD');
            } else {
              values.batchExpiredDateNew = expiredDate;
            }
          }else if (values.batchExpiredShowNew == '*'){
            values.batchExpiredDateNew = '2099-01-01';
          }
          for (const key in this.formTargetData) {
            if (!values[key]) {
              values[key] =  this.formTargetData[key]
            }
          }
          postbatchAdjust(values).then(res => {
             if (res.success) {
                this.$emit('save')
                this.$message.success('操作成功！')
              
              } else {
                this.$message.error(res.message)
              }
          })

        }
      })
    },
    handleCancel() {
      this.$emit('handleCancel')
    }
  }
}
</script>

<style></style>
