<template>
  <div>
    <el-form :model="editData" :rules="formRules" ref="dataForm" label-width="100px">
      <el-row :gutter="20">
        <el-col :span="20">
          <el-form-item label="UDI码:" prop="code">
            <el-input
              v-model="editData.code"
              clearable
              disabled
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="11">
          <el-form-item prop="productDate" label="生产日期">
            <el-input v-model="editData.produceDate" auto-complete="off"
                      oninput="if(value.length>6)value=value.slice(0,6)"
                      type="number"
                      :disabled="editType!=1 || !isProduceDate"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item prop="expireDate" label="失效日期">
            <el-input v-model="editData.expireDate" auto-complete="off"
                      oninput="if(value.length>6)value=value.slice(0,6)"
                      type="number"
                      :disabled="editType!=1 || !isExpireDate"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="11">
          <el-form-item prop="batchNo" label="批次号">
            <el-input v-model="editData.batchNo" auto-complete="off" :disabled="editType!=1 || !isBatchNo"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item prop="serialNo" label="序列号">
            <el-input v-model="editData.serialNo" auto-complete="off" :disabled="editType!=1 || !isSerialNo"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="11">
          <el-form-item prop="count" label="扫码数量">
            <el-input v-model="editData.count" auto-complete="off"
                      type="number"
                      min="1"
                      disabled
                      :disabled="editData.serialNo!=null && editData.serialNo!='' "></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="11">
          <el-form-item>
            <el-checkbox v-model="isUseDyCheck" disabled>是否以使用单元数量出入库</el-checkbox>
          </el-form-item>
        </el-col>
      </el-row>
      <div style="text-align: right;margin-top: 20px">
        <el-button @click="cancelDialog">取 消</el-button>
        <el-button type="primary" @click="saveCode()">确 定</el-button>
      </div>
    </el-form>
  </div>

</template>

<script>

import {
  findByCode,
  saveCode,
  checkTempCode
} from "@/api/inout/code";
import { isBlank } from '@/utils/strUtil'

export default {
  name: "editCode",
  props: {
    editType: {
      type: Object,
      required: true,
    },
    closeCodeDialog: {
      type: Function,
      required: true,
    },
    repeatAddCode: {
      type: Function,
      required: false,
    },
    codeDetail: {
      type: Object,
      required: true,
    },
    editData: null
  },

  data() {
    return {
      xlhEnable: false,
      isUseDyCheck: false,
      isProduceDate: false,
      isExpireDate: false,
      isBatchNo: false,
      isSerialNo: false,
    };
  },

  methods: {
    saveCode() {
      if (this.editType == 1 || this.editType == "1") {
        //校验该单有没有存在同序列号的码
        if (!isBlank(this.editData.serialNo)) {
          checkTempCode(this.editData).then((response) => {
            if (response.code === 20000) {
              this.repeatAddCode(this.editData);
              return
            } else {
              this.$message.error(response.message);
              return
            }
          });
        }else{
          this.repeatAddCode(this.editData);
        }
      } else {
        saveCode(this.editData)
          .then((response) => {
            console.log(response)
            if (response.code === 20000) {
              this.$message.success("提交成功");
              this.codeDetail = this.editData;
              this.closeCodeDialog();
            } else {
              this.$message.error(response.message);
            }
            this.loading = false;
          });
      }


    }
    ,
    cancelDialog() {
      this.closeCodeDialog();
    }
    ,
    findByCode() {
      let query = {udiCode: this.editData.code};
      findByCode(query)
        .then((response) => {
          if (response.code === 20000) {
            if (response.data.scbssfbhxlh == "否") {
              this.xlhEnable = true;
            }
            if (response.data.zxxsbzbhsydysl > 1) {
              this.isUseDyCheck = true;
            }
          } else {
          }
          this.loading = false;
        });
    }

  }
  ,
  created() {

    this.editData = JSON.parse(JSON.stringify(this.codeDetail));
    this.findByCode();
    //判断三区有数据的不允许修改
    if(isBlank(this.editData.produceDate)){
      this.isProduceDate=true
    }
    if(isBlank(this.editData.expireDate)){
      this.isExpireDate=true
    }
    if(isBlank(this.editData.batchNo)){
      this.isBatchNo=true
    }
    if(isBlank(this.editData.serialNo)){
      this.isSerialNo=true
    }
  }
}
</script>

<style scoped>

</style>
