<!--
 * @Author: zhp
 * @Date: 2023-02-14 15:02:26
 * @LastEditTime: 2023-07-11 15:53:48
 * @LastEditors: zhp
 * @Description: 
-->
<template>
  <el-form :model="dataForm" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" label-width="130px">
    <el-form-item prop="customSamplingCode" :label="$t('basic.code')">
      <el-input v-model="dataForm.customSamplingCode" :placeholder="$t('basic.code')">
      </el-input>
    </el-form-item>
    <el-form-item prop="customSamplingName" :label="$t('basic.name')">
      <el-input v-model="dataForm.customSamplingName" :placeholder="$t('basic.name')"></el-input>
    </el-form-item>
    <el-form-item prop="planCategory" :label="$t('quality.planCategory')">
      <el-input v-model="dataForm.planCategory" :placeholder="$t('quality.planCategory')"></el-input>
    </el-form-item>
    <el-form-item prop="criticalAcceptable" :label="$i18nForm([$t('quality.fatalFlaw'), $t('quality.acceptable')])">
      <el-input v-model="dataForm.criticalAcceptable"
        :placeholder="$i18nForm([$t('quality.fatalFlaw'), $t('quality.acceptable')])">
      </el-input>
    </el-form-item>
    <el-form-item prop="criticalSampleSize" :label="$i18nForm([$t('quality.fatalFlaw'), $t('quality.sampleSize')])">
      <el-input v-model="dataForm.criticalSampleSize"
        :placeholder="$i18nForm([$t('quality.fatalFlaw'), $t('quality.sampleSize')])">
      </el-input>
    </el-form-item>
    <el-form-item prop="criticalUnacceptable" :label="$i18nForm([$t('quality.fatalFlaw'), $t('quality.unacceptable')])">
      <el-input v-model="dataForm.criticalUnacceptable"
        :placeholder="$i18nForm([$t('quality.fatalFlaw'), $t('quality.unacceptable')])">
      </el-input>
    </el-form-item>
    <el-form-item prop="seriousDisadvantageAcceptable"
      :label="$i18nForm([$t('quality.seriousDisadvantage'), $t('quality.acceptable')])">
      <el-input v-model="dataForm.seriousDisadvantageAcceptable"
        :placeholder="$i18nForm([$t('quality.seriousDisadvantage'), $t('quality.acceptable')])">
      </el-input>
    </el-form-item>
    <el-form-item prop="seriousDisadvantageSampleSize"
      :label="$i18nForm([$t('quality.seriousDisadvantage'), $t('quality.sampleSize')])">
      <el-input v-model="dataForm.seriousDisadvantageSampleSize"
        :placeholder="$i18nForm([$t('quality.seriousDisadvantage'), $t('quality.sampleSize')])">
      </el-input>
    </el-form-item>
    <el-form-item prop="seriousDisadvantageUnacceptable"
      :label="$i18nForm([$t('quality.seriousDisadvantage'), $t('quality.unacceptable')])">
      <el-input v-model="dataForm.seriousDisadvantageUnacceptable"
        :placeholder="$i18nForm([$t('quality.seriousDisadvantage'), $t('quality.unacceptable')])">
      </el-input>
    </el-form-item>
    <el-form-item prop="minorAcceptable" :label="$i18nForm([$t('quality.minor'), $t('quality.acceptable')])">
      <el-input v-model="dataForm.minorAcceptable"
        :placeholder="$i18nForm([$t('quality.minor'), $t('quality.acceptable')])">
      </el-input>
    </el-form-item>
    <el-form-item prop="minorSampleSize" :label="$i18nForm([$t('quality.minor'), $t('quality.sampleSize')])">
      <el-input v-model="dataForm.minorSampleSize"
        :placeholder="$i18nForm([$t('quality.minor'), $t('quality.sampleSize')])">
      </el-input>
    </el-form-item>
    <el-form-item prop="minorUnacceptable" :label="$i18nForm([$t('quality.minor'), $t('quality.unacceptable')])">
      <el-input v-model="dataForm.minorUnacceptable"
        :placeholder="$i18nForm([$t('quality.minor'), $t('quality.unacceptable')])">
      </el-input>
    </el-form-item>
    <!-- <el-form-item prop="totalAcceptable" :label="$i18nForm([$t('quality.total'), $t('quality.acceptable')])">
      <el-input v-model="dataForm.totalAcceptable"
        :placeholder="$i18nForm([$t('quality.total'), $t('quality.acceptable')])">
      </el-input>
    </el-form-item>
    <el-form-item prop="totalUnacceptable" :label="$i18nForm([$t('quality.total'), $t('quality.unacceptable')])">
      <el-input v-model="dataForm.totalUnacceptable"
        :placeholder="$i18nForm([$t('quality.total'), $t('quality.unacceptable')])">
      </el-input>
    </el-form-item> -->
    <!-- <el-form-item prop="criticalSampleSize" :label="[$t('quality.fatalFlaw') + $t('quality.sampleSize')]">
      <el-input v-model="dataForm.criticalSampleSize"
        :placeholder="[$t('quality.fatalFlaw') + $t('quality.sampleSize')]">
      </el-input>
    </el-form-item>
    <el-form-item prop="criticalUnacceptable" :label="[$t('quality.fatalFlaw') + $t('quality.unacceptable')]">
      <el-input v-model="dataForm.criticalUnacceptable"
        :placeholder="[$t('quality.fatalFlaw') + $t('quality.unacceptable')]">
      </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/qmsCustomSampling",
        infoURL: "/quality/qmsCustomSampling/{id}",
        getCodeURL: '/quality/qmsCustomSampling/getCode'
      },
      options: [{
        value: 0,
        label: '不可用'
      },
      {
        value: 1,
        label: '可用'
      }],
      visible: false,
      dataForm: {
        id: null,
        planCategory:null,
        customSamplingCode:null,
        customSamplingName:null,
        criticalUnacceptable: null,
        criticalAcceptable: null,
        criticalSampleSize: null,
        minorAcceptable:null,
        minorSampleSize:null,
        minorUnacceptable:null,
        seriousDisadvantageAcceptable:null,
        seriousDisadvantageSampleSize:null,
        seriousDisadvantageUnacceptable:null,
        totalAcceptable:null,
        totalUnacceptable:null
      },
    };
  },
  computed: {
    dataRule() {
      return {
        // dictLabel: [
        //   {
        //     required: true,
        //     message: this.$t("validate.required"),
        //     trigger: "blur",
        //   },
        // ],
        // dictValue: [
        //   {
        //     required: true,
        //     message: this.$t("validate.required"),
        //     trigger: "blur",
        //   },
        // ],
        // sort: [
        //   {
        //     required: true,
        //     message: this.$t("validate.required"),
        //     trigger: "blur",
        //   },
        // ],
      };
    },
  },
  methods: {
    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.customSamplingCode = res.data
          }
        })
        .catch(() => {
        });
    },
    // 获取信息
    getInfo() {
      this.$http
        .get(`/quality/qmsCustomSampling/${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>
