<!--
 * @Author: zhp
 * @Date: 2023-02-14 15:02:26
 * @LastEditTime: 2023-06-30 16:58:55
 * @LastEditors: zhp
 * @Description: 
-->
<template>
  <el-form :model="dataForm" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" label-width="120px">
    <el-form-item prop="controlCode" :label="$t('basic.code')">
      <el-input v-model="dataForm.controlCode" :placeholder="$t('basic.code')"></el-input>
    </el-form-item>
    <el-form-item prop="controlName" :label="$t('basic.name')">
      <el-input v-model="dataForm.controlName" :placeholder="$t('basic.name')"></el-input>
    </el-form-item>
    <el-form-item prop="specification" :label="$t('basic.specification')">
      <el-input v-model="dataForm.specification" :placeholder="$t('basic.specification')"></el-input>
    </el-form-item>
    <el-form-item prop="incomingInspection" :label="$t('basic.stock')">
      <el-switch v-model="dataForm.incomingInspection" :active-value="1" :inactive-value="0">
      </el-switch>
    </el-form-item>
    <el-form-item prop="finishInspection" :label="$t('basic.finishedProduct')">
      <el-switch v-model="dataForm.finishInspection" :active-value="1" :inactive-value="0">
      </el-switch>
    </el-form-item>
    <el-form-item prop="outInspection" :label="$t('basic.shipment')">
      <el-switch v-model="dataForm.outInspection" :active-value="1" :inactive-value="0">
      </el-switch>
    </el-form-item>
    <el-form-item prop="processInspection" :label="$t('basic.course')">
      <el-switch v-model="dataForm.processInspection" :active-value="1" :inactive-value="0">
      </el-switch>
    </el-form-item>
    <el-form-item prop="controlStatus" :label="$t('basic.status')">
      <el-select v-model="dataForm.controlStatus" :placeholder="$t('basic.status')">
        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
      </el-select>
    </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: "/basic/qmsControlMode",
        infoURL: "/basic/qmsControlMode/{id}",
        getCodeURL: '/basic/qmsControlMode/getCode'
      },
      options: [{
        value: 0,
        label: '不可用'
      },
      {
        value: 1,
        label: '可用'
      }],
      visible: false,
      dataForm: {
        id: "",
        controlCode:null,
        controlName:null,
        controlStatus: null,
        finishInspection: null,
        incomingInspection: null,
        outInspection: null,
        processInspection: 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",
          },
        ],
      };
    },
  },
  created () {
  },
  methods: {
    init(id, ) {
      this.dataForm.id = id || ""
      // this.dataForm.dictTypeId = dictTypeId || "";
      this.visible = true
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        } else {
          this.getControlCode()
        }
      });
    },
    // 获取信息
    getInfo() {
      this.$http
        .get(`/basic/qmsControlMode/${this.dataForm.id}`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data,
          };
        })
        .catch(() => {});
    },
    getControlCode() {
      // console.log(111111);
      this.$http
        .post(this.urlOptions.getCodeURL)
        .then(({ data: res }) => {
          if (res.code === 0) {
            console.log(res);
            this.dataForm.controlCode = 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: () => {
                  this.visible = false;
                  console.log(1111);
                  this.$emit("successSubmit");
                },
              });
            })
            .catch(() => {});
        });
      },
      1000,
      { leading: true, trailing: false }
    ),
  },
};
</script>
