<!--
 * @Author: zhp
 * @Date: 2023-02-14 15:02:26
 * @LastEditTime: 2023-07-13 09:03:49
 * @LastEditors: zhp
 * @Description: 
-->
<template>
  <el-form :model="dataForm" ref="dataForm" :rules="dataRule" @keyup.enter.native="dataFormSubmitHandle()"
    label-width="150px">
    <el-form-item prop="code" :label="$t('gage.code')">
      <el-input v-model="dataForm.code" :placeholder="$t('gage.code')">
      </el-input>
    </el-form-item>
    <el-form-item prop="name" :label="$t('gage.name')">
      <el-input v-model="dataForm.name" :placeholder="$t('gage.name')">
      </el-input>
    </el-form-item>
    <el-form-item prop="decimalDigits" :label="$t('gage.decimalDigits')">
      <el-input v-model="dataForm.decimalDigits" :placeholder="$t('gage.decimalDigits')">
      </el-input>
    </el-form-item>
    <el-form-item prop="lsl" :label="$t('gage.lsl')">
      <el-input v-model="dataForm.lsl" :placeholder="$t('gage.lsl')">
      </el-input>
    </el-form-item>
    <el-form-item prop="serialNo" :label="$t('gage.serialNo')">
      <el-input v-model="dataForm.serialNo" :placeholder="$t('gage.serialNo')">
      </el-input>
    </el-form-item>
    <el-form-item prop="sl" :label="$t('gage.sl')">
      <el-input v-model="dataForm.sl" :placeholder="$t('gage.sl')">
      </el-input>
    </el-form-item>
    <el-form-item prop="usl" :label="$t('gage.usl')">
      <el-input v-model="dataForm.usl" :placeholder="$t('gage.usl')">
      </el-input>
    </el-form-item>
    <el-form-item prop="gageTypeId" :label="$t('gage.gageTypeName')">
      <el-select v-model="dataForm.gageTypeId" :placeholder="$t('gage.gageTypeName')">
        <el-option v-for="item in gageTypeList" :key="item.id" :label="item.name" :value="item.id">
        </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: "/gage/qmsGageType",
        infoURL: "/gage/qmsGageType/{ id }",
        // getGageListURL: '/gage/qmsGage/page',
        getGageTypeListURL: '/gage/qmsGageType/page',
        // getTeamListURL: '/basic/qmsTeam/gape',
        // getProductURL: '/basic/qmsProduct/gape',
        // getSupplierListURL: '/supplier/qmsSupplier/gape'
      },
      gageTypeList:[],
      // supplierList:[],
      // productList:[],
      // typeList: [],
      // teamList:[],
      listQuery: {
        limit: 999,
        gape: 1
      },
      measurementTypeList: [{
        id: 0,
        name: '计量'
      },
      {
        id: 1,
        name: '计数'
      },
      ],
      visible: false,
      dataForm: {
        id: null,
        decimalDigits:null,	
        gageTypeId: null,
        lsl: null,
        name: null,
        serialNo: null,
        sl: null,
        updateDate: null,
        usl: null,
        code:null
      },
    };
  },
  computed: {
    dataRule() {
      return {
        name: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        code: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ]
      };
    },
  },
  mounted () {
    this.getData();
  },
  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()
        }
      });
    },
    getData() {
      // this.$http
      //   .get(this.urlOptions.getTypeListURL, this.listQuery)
      //   .then(({ data: res }) => {
      //     if (res.code === 0) {
      //       console.log(res.data);
      //       this.typeList = res.data.list
      //     }
      //   })
      //   .catch(() => {
      //   });
      this.$http
        .get(this.urlOptions.getGageTypeListURL, {
          params: this.listQuery
        })
        .then(({ data: res }) => {
          if (res.code === 0) {
            console.log(res.data);
            this.gageTypeList = res.data.list
          }
        })
        .catch(() => {
        })
    },
    // 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(`/gage/qmsGageTypeDifferenceParameter/${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>
