<!--
 * @Author: zhp
 * @Date: 2023-02-14 15:02:26
 * @LastEditTime: 2023-05-22 16:23:33
 * @LastEditors: zhp
 * @Description: 
-->
<template>
  <el-form :model="dataForm" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" label-width="150px">
    <el-form-item prop="batchNumber" :label="$t('gage.batchTail')">
      <el-input v-model="dataForm.batchNumber" :placeholder="$t('gage.batchTail')">
      </el-input>
    </el-form-item>
    <el-form-item prop="measuringToolAccuracy" :label="$t('gage.measuringToolAccuracy')">
      <el-input v-model="dataForm.measuringToolAccuracy" :placeholder="$t('gage.measuringToolAccuracy')">
      </el-input>
    </el-form-item>
    <el-form-item prop="price" :label="$t('gage.price')">
      <el-input v-model="dataForm.price" :placeholder="$t('gage.price')">
      </el-input>
    </el-form-item>
    <el-form-item prop="parameter" :label="$t('gage.parameter')">
      <el-select v-model="dataForm.parameter" :placeholder="$t('gage.parameter')">
        <el-option v-for="item in parameterList" :key="item.id" :label="item.name" :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item prop="gageId" :label="$t('gage.gageName')">
      <el-select v-model="dataForm.gageId" :placeholder="$t('gage.gageName')" disabled>
        <el-option v-for="item in gageList" :key="item.id" :label="item.name" :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item prop="remark" :label="$t('gage.remark')">
      <el-input v-model="dataForm.remark" :placeholder="$t('gage.remark')">
      </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: "/gage/qmsStationDeviationAnalysis",
        infoURL: "/gage/qmsStationDeviationAnalysis/{ id }",
        getGageListURL: '/gage/qmsGage/page'
      },
      gageList: [],
      userList: [],
      gageTypeList:[],
      departmentList: [],
      parameterList: [
        {
          id: 0,
          name: '基准'
        },
        {
          id: 1,
          name: '校验'
        },
      ],
      listQuery: {
        limit: 999,
        page: 1
      },
      visible: false,
      dataForm: {
        batchNumber:null,
        gageId: null,
        gageResumeId: null,
        id: null,
        measuringToolAccuracy: null,
        parameter: null,
        price: null,
        remark: 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",
        //   },
        // ],
      };
    },
  },
  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.getGageListURL, {
          params: this.listQuery
        })
        .then(({ data: res }) => {
          if (res.code === 0) {
            console.log(res.data);
            this.gageList = 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/qmsStationDeviationAnalysis/${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>
