<!--
 * @Author: zhp
 * @Date: 2023-02-14 15:02:26
 * @LastEditTime: 2023-05-17 14:21:45
 * @LastEditors: zhp
 * @Description: 
-->
<template>
  <el-form :model="dataForm" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" label-width="150px">
    <el-form-item prop="client" :label="$t('gage.client')">
      <el-input v-model="dataForm.client" :placeholder="$t('gage.client')">
      </el-input>
    </el-form-item>
    <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="certificateType" :label="$t('gage.certificateType')">
      <el-select v-model="dataForm.certificateType" :placeholder="$t('gage.certificateType')">
        <el-option v-for="item in certificateTypeList" :key="item.id" :label="item.name" :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item prop="certificateCodeBh" :label="$t('gage.certificateCodeBh')">
      <el-input v-model="dataForm.certificateCodeBh" :placeholder="$t('gage.certificateCodeBh')">
      </el-input>
    </el-form-item>
    <el-form-item prop="certificateCode" :label="$t('gage.certificateCode')">
      <el-input v-model="dataForm.certificateCode" :placeholder="$t('gage.certificateCode')">
      </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-item prop="measuringToolSpecification" :label="$t('gage.measuringToolSpecification')">
      <el-input v-model="dataForm.measuringToolSpecification" :placeholder="$t('gage.measuringToolSpecification')">
      </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="description" :label="$t('gage.description')">
      <el-input v-model="dataForm.description" :placeholder="$t('gage.description')">
      </el-input>
    </el-form-item>
    <el-form-item prop="equipmentSupplier" :label="$t('gage.supplierName')">
      <el-input v-model="dataForm.equipmentSupplier" :placeholder="$t('gage.supplierName')">
      </el-input>
    </el-form-item>
    <el-form-item prop="internalInspectionCycle" :label="$t('gage.internalInspectionCycle')">
      <el-input v-model="dataForm.internalInspectionCycle" :placeholder="$t('gage.internalInspectionCycle')">
      </el-input>
    </el-form-item>
    <el-form-item prop="externalInspectionCycle" :label="$t('gage.externalInspectionCycle')">
      <el-input v-model="dataForm.externalInspectionCycle" :placeholder="$t('gage.externalInspectionCycle')">
      </el-input>
    </el-form-item>
    <el-form-item prop="internalInspectionTime" :label="$t('gage.internalInspectionTime')">
      <el-date-picker v-model="dataForm.internalInspectionTime" type="datetime"
        :placeholder="$t('gage.internalInspectionTime')" format='yyyy-MM-dd HH:mm:ss' valueFormat='yyyy-MM-ddTHH:mm:ss'>
      </el-date-picker>
    </el-form-item>
    <el-form-item prop="externalInspectionTime" :label="$t('gage.externalInspectionTime')">
      <el-date-picker v-model="dataForm.externalInspectionTime" type="datetime"
        :placeholder="$t('gage.externalInspectionTime')" format='yyyy-MM-dd HH:mm:ss' valueFormat='yyyy-MM-ddTHH:mm:ss'>
      </el-date-picker>
    </el-form-item>
    <el-form-item prop="nextInternalInspectionTime" :label="$t('gage.nextInternalInspectionTime')">
      <el-date-picker v-model="dataForm.nextInternalInspectionTime" type="datetime"
        :placeholder="$t('gage.nextInternalInspectionTime')" format='yyyy-MM-dd HH:mm:ss'
        valueFormat='yyyy-MM-ddTHH:mm:ss'>
      </el-date-picker>
    </el-form-item>
    <el-form-item prop="nextExternalInspectionTime" :label="$t('gage.nextExternalInspectionTime')">
      <el-date-picker v-model="dataForm.nextExternalInspectionTime" type="datetime"
        :placeholder="$t('gage.nextExternalInspectionTime')" format='yyyy-MM-dd HH:mm:ss'
        valueFormat='yyyy-MM-ddTHH:mm:ss'>
      </el-date-picker>
    </el-form-item>
    <el-form-item prop="userDepartment" :label="$t('gage.userDepartment')">
      <el-select v-model="dataForm.userDepartment" :placeholder="$t('gage.userDepartment')">
        <el-option v-for="item in departmentList" :key="item.id" :label="item.name" :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item prop="useLocation" :label="$t('gage.useLocation')">
      <el-input v-model="dataForm.useLocation" :placeholder="$t('gage.useLocation')">
      </el-input>
    </el-form-item>
    <el-form-item prop="recipient" :label="$t('gage.recipientName')">
      <el-select v-model="dataForm.recipient" :placeholder="$t('gage.recipientName')">
        <el-option v-for="item in userList" :key="item.id" :label="item.realName" :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item prop="benchmarkMeasurTool" :label="$t('gage.benchmarkMeasurTool')">
      <el-radio-group v-model="dataForm.benchmarkMeasurTool">
        <el-radio :label="0">否</el-radio>
        <el-radio :label="1">是</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item prop="belongPerson" :label="$t('gage.belongPersonName')">
      <el-select v-model="dataForm.belongPerson" :placeholder="$t('gage.belongPersonName')">
        <el-option v-for="item in userList" :key="item.id" :label="item.realName" :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item prop="gageStatus" :label="$t('gage.gageStatus')">
      <el-select v-model="dataForm.gageStatus" :placeholder="$t('gage.gageStatus')">
        <el-option v-for="item in gageStatusList" :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/qmsGage",
        infoURL: "/gage/qmsGage/{ id }",
        dictionaryDetailList: '/mutual/qmsDataDictionaryDetail/page',
        getUserList: '/sys/user/page',
        getGageTypeList: '/gage/qmsGageType/page',
      },
      gageList: [],
      userList: [],
      gageTypeList:[],
      departmentList: [],
      processList: [
        {
          id: 0,
          name: '未审核'
        },
        {
          id: 1,
          name: '不同意'
        },
        {
          id: 2,
          name: '同意'
        }
      ],
      listQuery: {
        limit: 999,
        gape: 1
      },
      gageStatusList: [
        {
          id: 0,
          name:'在用'
        },
        {
          id: 1,
          name: '备用'
        },
        {
          id: 2,
          name: '停用'
        },
        {
          id: 3,
          name: '封存'
        },
        {
          id: 4,
          name: '报废'
        },
        {
          id: 5,
          name: '降级使用'
        }
      ],
      certificateTypeList: [
        {
          id: 0,
          name: '检定证书'
        },
        {
          id: 1,
          name: '校准证书'
        },
        {
          id: 2,
          name: '检测报告'
        },
        {
          id: 3,
          name: '自校报告'
        }
      ],
      visible: false,
      dataForm: {
        belongPerson:'1067246875800000001',
        benchmarkMeasurTool: null,
        certificateCode: null,
        certificateCodeBh: null,
        certificateType: null,
        client: null,
        code: null,
        createDate: null,
        description: null,
        equipmentSupplier: null,
        externalInspectionCycle: null,
        externalInspectionTime: null,
        gageStatus: null,
        gageTypeId: null,
        id: null,
        internalInspectionCycle: null,
        internalInspectionTime: null,
        measuringToolAccuracy: null,
        measuringToolSpecification: null,
        name: null,
        nextExternalInspectionTime: null,
        nextInternalInspectionTime: null,
        recipient: '1067246875800000001',
        remark: null,
        updateDate: null,
        useLocation: null,
        userDepartment: 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.getUserList, this.listQuery)
        .then(({ data: res }) => {
          if (res.code === 0) {
            console.log(res.data);
            this.userList = res.data.list
          }
        })
        .catch(() => {
        });
      this.$http
        .get(this.urlOptions.dictionaryDetailList, this.listQuery)
        .then(({ data: res }) => {
          if (res.code === 0) {
            console.log(res.data);
            this.departmentList = res.data.list
          }
        })
        .catch(() => {
        })
      this.$http
        .get(this.urlOptions.getGageTypeList, 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/qmsGage/${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>
