<!--
 * @Author: zhp
 * @Date: 2023-02-14 15:02:26
 * @LastEditTime: 2023-05-26 11:21:02
 * @LastEditors: zhp
 * @Description: 
-->
<template>
  <el-form :rules="dataRule" :model="dataForm" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
    label-width="130px">
    <el-row>
      <el-col :span="12">
        <el-form-item prop="transferSchemeCode" :label="$t('quality.code')">
          <el-input @input="(e) => handlerShow(e)" v-model="dataForm.transferSchemeCode"
            :placeholder="$t('quality.code')">
          </el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item prop="transferSchemeName" :label="$t('quality.name')">
          <el-input v-model="dataForm.transferSchemeName" :placeholder="$t('quality.name')"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-form-item prop="criticalSampleSize">
      (1)正常检查转入放宽检查：最近
      <el-input v-model="dataForm.normalToRelaxedOne" style="width: 10%;">
      </el-input>
      批中的
      <el-input v-model="dataForm.normalToRelaxedTwo" style="width: 10%;">
      </el-input>
      批检验合格
    </el-form-item>
    <el-form-item prop="criticalSampleSize">
      (2)放宽检查恢复正常检查：最近
      <el-input v-model="dataForm.relaxedToNormalOne" style="width: 10%;">
      </el-input>
      批中的
      <el-input v-model="dataForm.relaxedToNormalTwo" style="width: 10%;">
      </el-input>
      批检验合格
    </el-form-item>
    <el-form-item prop="criticalSampleSize">
      (3)正常检查转入加严检查：最近
      <el-input v-model="dataForm.normalToTightenedOne" style="width: 10%;">
      </el-input>
      批中的
      <el-input v-model="dataForm.normalToTightenedTwo" style="width: 10%;">
      </el-input>
      批检验合格
    </el-form-item>
    <el-form-item prop="criticalSampleSize">
      (4)加严检查恢复正常检查：最近
      <el-input v-model="dataForm.tightenedToNormalOne" style="width: 10%;">
      </el-input>
      批中的
      <el-input v-model="dataForm.tightenedToNormalTwo" style="width: 10%;">
      </el-input>
      批检验合格
    </el-form-item>
  </el-form>
</template>

<script>
import debounce from "lodash/debounce";
import basicAdd from "@/mixins/basic-add";
export default {
  mixins: [basicAdd],
  data() {
    return {
      urlOptions: {
        submitURL: "/quality/qmsTransferScheme",
        infoURL: "/quality/qmsTransferScheme/{id}",
        getCodeURL: "/quality/qmsTransferScheme/getCode"
      },
      options: [{
        value: 0,
        label: '不可用'
      },
      {
        value: 1,
        label: '可用'
      }],
      visible: false,
      dataRule:{
          transferSchemeName: [
            {
              required: true,
              message: this.$t("quality.name"),
              trigger: "blur",
            },
          ],
          transferSchemeCode: [
            {
              required: true,
              message: this.$t("quality.code"),
              trigger: "blur",
            },
          ]
      },
      dataForm: {
        id: null,
        transferSchemeCode: null,
        normalToRelaxedOne: null,
        normalToRelaxedTwo: null,
        normalToTightenedOne: null,
        normalToTightenedTwo: null,
        relaxedToNormalOne: null,
        relaxedToNormalTwo: null,
        tightenedToNormalOne: null,
        tightenedToNormalTwo: null,
        transferSchemeName: null
      },
    };
  },
  // computed: {
   
  // },
  methods: {
    handleShow(e) {
      this.debounce(this.ajax(e),500)
    },
    ajax(e) {
      console.log(e)
    },
    debounce(fn, delay) {
      let timer
      return function () {
        console.log(timer)
        if (timer) {
          console.log('aaaaa')
          clearTimeout(timer)
        }
        timer = setTimeout(() => {
          console.log(arguments)
          fn.apply(this, arguments) // 透传 this和参数
          timer = 0
        }, delay)
      }
    },
    init(id, ) {
      this.dataForm.id = id || ""
      // console.log(11111)
      // this.dataForm.dictTypeId = dictTypeId || "";
      this.visible = true
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.getInfo()
        } else {
          this.getCode()
        }
      });
    },
    getCode() {
      this.$http.post(this.urlOptions.getCodeURL)
        .then(({ data: res }) => {
          if (res.code === 0) {
            console.log(res);
            this.dataForm.transferSchemeCode = res.data
          }
        })
        .catch(() => {
        });
    },
    // 获取信息
    getInfo() {
      this.$http
        .get(`/quality/qmsTransferScheme/${this.dataForm.id}`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data,
          };
        })
        .catch(() => {});
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs["dataForm"].validate((valid) => {
          if (!valid) {
            return false;
          }
          this.$http[!this.dataForm.id ? "post" : "put"](this.urlOptions.submitURL, this.dataForm)
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  console.log(1111);
                  this.visible = false;
                  this.$emit("successSubmit");
                },
              });
            })
            .catch(() => {});
        });
      },
      1000,
      { leading: true, trailing: false }
    ),
  },
};
</script>
