<template>
  <div>
    <el-card style="margin-top: -20px">
      <el-descriptions title="产品信息" :column="2" border style="margin-top: 5px">
        <el-descriptions-item label="产品名称">{{ prescribeData.cpmctymc }}</el-descriptions-item>
        <el-descriptions-item label="产品标识">{{ prescribeData.nameCode }}</el-descriptions-item>
        <el-descriptions-item label="产品规格">{{ prescribeData.ggxh }}</el-descriptions-item>
        <el-descriptions-item label="计量单位">{{ prescribeData.measureUnit }}</el-descriptions-item>
        <el-descriptions-item label="批次号">{{ prescribeData.batchNo }}</el-descriptions-item>
        <el-descriptions-item label="生产日期">{{ prescribeData.productDate }}</el-descriptions-item>
        <el-descriptions-item label="失效日期">{{ prescribeData.expireDate }}</el-descriptions-item>
      </el-descriptions>
      <el-form :model="formData" class="demo-form-inline" style="margin-top: 30px" :rules="rules"
               ref="formRef">
        <el-row :gutter="24">
          <el-col :span="24">
            <el-form-item label="原始追溯码" prop="count" label-width="100px">
              <el-input v-model="prescribeData.finishUdiCode" placeholder=""
                        style="width:90%;"
                        disabled
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="实际追溯码" label-width="100px">
              <el-input
                id="inputer"
                @focus="getInputFocus($event)"
                ref="inputRef"
                style="ime-mode: disabled;width:90%;"
                type="tel"
                placeholder="请扫码或手动录入实际条码，多个条码请 ; 隔开"
                v-model="prescribeData.replaceCode"
              ></el-input>

            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div style="text-align: center; margin-top: 20px;">
        <el-button type="primary" @click="destroyOutOrder" :loading="saveLoading">提交</el-button>
        <el-button @click="closeAddDialog">取消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import {destroyOut} from '@/api/collect/IoDestroy'
import {updateBiz} from "@/api/collect/collectOrder";

export default {
  name: 'DialogReplaceCode',
  props: {
    prescribeData: {
      type: Object,
      required: true
    },
    closeDialog: {
      type: Function,
      required: true
    }
  },
  data() {
    return {
      formData: {},
      produceDate: null,
      expireDate: null,
      saveLoading: false,
      rules: {
        count: [
          {validator: this.validateCount, message: '损耗出库数量不能超出剩余数量', trigger: 'blur'}
        ]
      },

    }
  },
  methods: {
    conversionDate(dateString) {
      const parts = dateString.split('');
      const year = '20' + parts.slice(0, 2).join('');
      const month = parts[2] + parts[3];
      const day = parts[4] + parts[5];
      return `${year}-${month}-${day}`;
    },
    validateCount(rule, value, callback) {
      if (value > this.prescribeData.reCount) {
        callback(new Error('损耗出库数量超出剩余数量'));
      } else {
        callback();
      }
    },
    checkCount() {
      // 这里可以调用表单验证方法
      this.$refs.formRef.validateField('count');
    },
    closeAddDialog() {
      this.closeDialog()
    },

    //损耗出库
    destroyOutOrder() {

      updateBiz(this.prescribeData).then(res => {
        if (res.code == 20000) {
          this.closeDialog()
          return this.$message.success("替换成功！")
        } else {
          return this.$message.error(res.message)
        }
      })
    },

    getInputFocus(event) {
      event.currentTarget.select();
    },

  },
  created() {
    if (this.prescribeData.produceDate != null) {
      this.produceDate = this.conversionDate(this.prescribeData.produceDate)
    }
    if (this.prescribeData.expireDate != null) {
      this.expireDate = this.conversionDate(this.prescribeData.expireDate)
    }
    this.$refs.inputRef.focus();
    this.$refs.inputRef.select();
  }
}
</script>

<style scoped>


</style>
