<!--
 * @Author: zhp
 * @Date: 2023-02-14 15:02:26
 * @LastEditTime: 2023-05-18 15:30:28
 * @LastEditors: zhp
 * @Description: 
-->
<template>
  <div>
    <el-form :model="dataForm" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" label-width="150px">
      <el-form-item prop="gageId" :label="$t('gage.gageName')">
        <el-select v-model="dataForm.gageId" :placeholder="$t('gage.gageName')">
          <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="gageCode" :label="$t('gage.gageCode')">
      <el-input v-model="dataForm.gageCode" :placeholder="$t('gage.gageCode')">
      </el-input>
    </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="measurToolEvent" :label="$t('gage.measurToolEvent')">
        <el-select @change="changeShow" v-model="dataForm.measurToolEvent" :placeholder="$t('gage.measurToolEvent')">
          <el-option v-for="item in eventList" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
        <el-tooltip class="item" effect="dark" :content="$t('gage.tips')" placement="top">
          <i class="el-icon-warning"></i>
        </el-tooltip>
      </el-form-item>
      <el-form-item v-if="receiveShow || returnShow || maintainShow || transferShow || detectionShow"
        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 v-if="receiveShow || returnShow || maintainShow" prop="useLocation" :label="$t('gage.useLocation')">
        <el-input v-model="dataForm.useLocation" :placeholder="$t('gage.useLocation')">
        </el-input>
      </el-form-item>
      <el-form-item v-if="!scrapShow" 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="processUserId" :label="$t('gage.processUserName')">
        <el-select v-model="dataForm.processUserId" :placeholder="$t('gage.processUserName')">
          <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="process" :label="$t('gage.process')">
        <el-select v-model="dataForm.process" :placeholder="$t('gage.process')">
          <el-option v-for="item in processList" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="receiveShow || maintainShow || detectionShow" prop="expectedReturnDate"
        :label="$t('gage.expectedReturnDate')">
        <el-date-picker v-model="dataForm.expectedReturnDate" type="datetime"
          :placeholder="$t('gage.expectedReturnDate')" format='yyyy-MM-dd HH:mm:ss' valueFormat='yyyy-MM-ddTHH:mm:ss'>
        </el-date-picker>
      </el-form-item>
      <el-form-item v-if="transferShow" prop="transferDate" :label="$t('gage.transferDate')">
        <el-date-picker v-model="dataForm.transferDate" type="datetime" :placeholder="$t('gage.transferDate')"
          format='yyyy-MM-dd HH:mm:ss' valueFormat='yyyy-MM-ddTHH:mm:ss'>
        </el-date-picker>
      </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>
    <el-form v-if="detectionShow && showForm" :model="parameterData" ref="parameterData" label-width="150px">
      <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="batchNumber" :label="$t('gage.batchTail')">
        <el-input v-model="parameterData.batchNumber" :placeholder="$t('gage.batchTail')">
        </el-input>
      </el-form-item>
      <el-form-item prop="measuringToolAccuracy" :label="$t('gage.measuringToolAccuracy')">
        <el-input v-model="parameterData.measuringToolAccuracy" :placeholder="$t('gage.measuringToolAccuracy')">
        </el-input>
      </el-form-item>
      <el-form-item prop="price" :label="$t('gage.price')">
        <el-input v-model="parameterData.price" :placeholder="$t('gage.price')">
        </el-input>
      </el-form-item>
      <el-form-item prop="remark" :label="$t('gage.parameterRemark')">
        <el-input v-model="parameterData.remark" :placeholder="$t('gage.parameterRemark')">
        </el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import debounce from "lodash/debounce";
// import basicAdd from "@/mixins/basic-add";
export default {
  // mixins: [basicAdd],
  data() {
    return {
      urlOptions: {
        submitURL: "/gage/qmsGageResume",
        infoURL: "/gage/qmsGageResume/{ id }",
        dictionaryDetailList: '/mutual/qmsDataDictionaryDetail/page',
        getUserList: '/sys/user/page',
        getGageList: '/gage/qmsGage/page',
        submitStationDeviationAnalysisURL: '/gage/qmsStationDeviationAnalysis',
      },
      gageList: [],
      gageResumeList: [],
      userList: [],
      showForm:true,
      receiveShow: false,
      returnShow: false,
      scrapShow: false,
      maintainShow: false,
      purchaseShow: false,
      detectionShow: false,
      transferShow: false,
      departmentList: [],
      processList: [
        {
          id: 0,
          name: '未审核'
        },
        {
          id: 1,
          name: '不同意'
        },
        {
          id: 2,
          name: '同意'
        }
      ],
      parameterList: [
        {
          id: 0,
          name: '基准 '
        },
        {
          id: 1,
          name: '校验'
        }
      ],
      listQuery: {
        limit: 999,
        page: 1
      },
      eventList: [
        {
          id: 0,
          name: '领用'
        },
        {
          id: 1,
          name: '归还'
        },
        {
          id: 2,
          name: '量具报废'
        },
        {
          id: 3,
          name: '量具维修'
        },
        {
          id: 4,
          name: '量具购买'
        },
        {
          id: 5,
          name: '量具检测'
        },
        {
          id: 6,
          name: '量具转移'
        },
      ],
      visible: false,
      parameterData: {
        gageResumeId:null,
        batchNumber: null,
        measuringToolAccuracy: null,
        parameter: null,
        price: null,
        remark: null,
      },
      dataForm: {
        id: null,
        expectedReturnDate:null,
        gageCode: null,
        gageId: '',
        measuringToolSpecification:null,
        measurToolEvent: null,
        process: null,
        processUserId: '1067246875800000001',
        recipient: '1067246875800000001',
        remark: null,
        transferDate: null,
        useLocation: null,
        userDepartment: 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();
  },
  destroyed () {
    this.receiveShow = false
    this.returnShow = false
    this.scrapShow = false
    this.maintainShow = false
    this.purchaseShow = false
    this.detectionShow = false
    this.transferShow = false
  },
  methods: {
    changeShow(val) {
      if (val === 0) {
        this.receiveShow = true
        this.returnShow = false 
        this.scrapShow = false
        this.maintainShow = false
        this.purchaseShow = false
        this.detectionShow = false
        this.transferShow = false
      } else if(val === 1 ) {
        this.returnShow = true 
        this.receiveShow = false
        this.scrapShow = false
        this.maintainShow = false
        this.purchaseShow = false
        this.detectionShow = false
        this.transferShow = false
      } else if (val === 2) {
        this.scrapShow = true
        this.returnShow = false
        this.receiveShow = false
        this.maintainShow = false
        this.purchaseShow = false
        this.detectionShow = false
        this.transferShow = false
      } else if (val === 3) {
        this.scrapShow = false
        this.returnShow = false
        this.receiveShow = false
        this.maintainShow = true
        this.purchaseShow = false
        this.detectionShow = false
        this.transferShow = false
      } else if (val === 4) {
        this.purchaseShow = true
        this.scrapShow = false
        this.returnShow = false
        this.receiveShow = false
        this.maintainShow = false
        this.detectionShow = false
        this.transferShow = false
      } else if (val === 5) {
        this.detectionShow = true
        this.purchaseShow = false
        this.scrapShow = false
        this.returnShow = false
        this.receiveShow = false
        this.maintainShow = false
        this.transferShow = false
      } else if (val === 6) {
        this.detectionShow = false
        this.purchaseShow = false
        this.scrapShow = false
        this.returnShow = false
        this.receiveShow = false
        this.maintainShow = false
        this.transferShow = true
      }
    },
    init(id, ) {
      this.dataForm.id = id || ""
      if (id) {
        this.showForm = false
      }
      // 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.getGageList, {
          params: this.listQuery
        })
        .then(({ data: res }) => {
          if (res.code === 0) {
            console.log(res.data);
            this.gageList = 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(() => {
        })
    },
    // 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/qmsGageResume/${this.dataForm.id}`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data,
          };
        })
        .catch(() => { })
    },
    formClear() {
      this.$refs.dataForm.resetFields()
      this.tableData = []
    },
    // 表单提交
    dataFormSubmit() {
        this.$refs["dataForm"].validate((valid) => {
          if (!valid) {
            return false;
          }
          if (this.dataForm.measurToolEvent === 5) {
            var pattern = /^[0-9]+(_)[0-9]+$/
            if (pattern.test(this.parameterData.batchNumber)) {
              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.parameterData.gageResumeId = res.data
                  this.$http.post(this.urlOptions.submitStationDeviationAnalysisURL, this.parameterData)
                    .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;
                          this.$emit("refreshDataList")
                        }
                      })
                    })

                  this.$message({
                    message: this.$t("prompt.success"),
                    type: "success",
                    duration: 500,
                    onClose: () => {
                      this.visible = false;
                      this.$emit("refreshDataList")
                    }
                  })
                })
                .catch(() => { });
            } else {
              this.$message({
                message: this.$t("gage.batchTips"),
                type: 'warning',
                duration: 500,
              })
            }
          } else { 
            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;
                    this.$emit("refreshDataList")
                  }
                })
              })
              .catch(() => { });
          }
        });
      },
  },
};
</script>
