<!--
 * @Author: zhp
 * @Date: 2023-07-14 13:44:46
 * @LastEditTime: 2023-07-18 15:01:40
 * @LastEditors: zhp
 * @Description: 
-->
<template>
  <div>
    <el-drawer close-on-press-escape :title="$t('add')" :append-to-body="true" :before-close="handleClose"
      :visible.sync="innerDrawer">
      <el-form :model="dataForm" ref="dataForm" :rules="dataRule" @keyup.enter.native="dataFormSubmitHandle()"
        label-width="120px">
        <el-form-item prop="productId" :label="$t('basic.productName')">
          <el-select v-model="dataForm.productId" :placeholder="$t('basic.productName')">
            <el-option v-for="item in productList" :key="item.id" :label="item.productName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="processId" :label="$t('researchquality.processName')">
          <el-select v-model="dataForm.processId" :placeholder="$t('researchquality.processName')">
            <el-option v-for="item in processList" :key="item.id" :label="item.processName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="measuringToolsType" :label="$t('quality.measuringToolsType')">
          <el-select v-model="dataForm.measuringToolsType" :placeholder="$t('quality.measuringToolsType')">
            <el-option v-for="item in gageTypeList" :key="item.id" :label="item.name" :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="monitor" :label="$t('quality.monitor')">
          <el-select v-model="dataForm.monitor" :placeholder="$t('quality.monitor')">
            <el-option v-for="item in monitorList" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="characteristicsType" :label="$t('quality.characteristicsType')">
          <el-select v-model="dataForm.characteristicsType" :placeholder="$t('quality.characteristicsType')">
            <el-option v-for="item in characteristicsTypeList" :key="item.value" :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="inspectionType" :label="$t('quality.inspectionType')">
          <el-select v-model="dataForm.inspectionType" :placeholder="$t('quality.inspectionType')">
            <el-option v-for="item in inspectionTypeList" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="parameterCode" :label="$t('quality.parameterCode')">
          <el-input v-model="dataForm.parameterCode" :placeholder="$t('quality.parameterCode')">
          </el-input>
        </el-form-item>
        <el-form-item prop="parameterName" :label="$t('quality.parameterName')">
          <el-input v-model="dataForm.parameterName" :placeholder="$t('quality.parameterName')">
          </el-input>
        </el-form-item>
        <el-form-item prop="specifications" :label="$t('quality.specifications')">
          <el-input v-model="dataForm.specifications" :placeholder="$t('quality.specifications')">
          </el-input>
        </el-form-item>
        <el-form-item prop="source" :label="$t('quality.source')">
          <el-input v-model="dataForm.source" :placeholder="$t('quality.source')">
          </el-input>
        </el-form-item>
        <el-form-item prop="company" :label="$t('quality.company')">
          <el-input v-model="dataForm.company" :placeholder="$t('quality.company')">
          </el-input>
        </el-form-item>
        <el-form-item prop="decimalDigits" :label="$t('quality.decimalDigits')">
          <el-input v-model="dataForm.decimalDigits" :placeholder="$t('quality.decimalDigits')">
          </el-input>
        </el-form-item>
        <el-form-item prop="sampleSize" :label="$t('quality.sampleSize')">
          <el-input v-model="dataForm.sampleSize" :placeholder="$t('quality.sampleSize')">
          </el-input>
        </el-form-item>
        <el-form-item prop="analyzeDrawings" :label="$t('quality.analyzeDrawings')">
          <el-input v-model="dataForm.analyzeDrawings" :placeholder="$t('quality.analyzeDrawings')">
          </el-input>
        </el-form-item>
        <el-form-item prop="upperSpecificationLimit" :label="$t('quality.upperSpecificationLimit')">
          <el-input v-model="dataForm.upperSpecificationLimit" :placeholder="$t('quality.upperSpecificationLimit')">
          </el-input>
        </el-form-item>
        <el-form-item prop="specificationCenterline" :label="$t('quality.specificationCenterline')">
          <el-input v-model="dataForm.specificationCenterline" :placeholder="$t('quality.specificationCenterline')">
          </el-input>
        </el-form-item>
        <el-form-item prop="specificationOffline" :label="$t('quality.specificationOffline')">
          <el-input v-model="dataForm.specificationOffline" :placeholder="$t('quality.specificationOffline')">
          </el-input>
        </el-form-item>
        <el-form-item prop="targetCpk" :label="$t('quality.targetCpk')">
          <el-input v-model="dataForm.targetCpk" :placeholder="$t('quality.targetCpk')">
          </el-input>
        </el-form-item>
        <el-form-item prop="targetPpk" :label="$t('quality.targetPpk')">
          <el-input v-model="dataForm.targetPpk" :placeholder="$t('quality.targetPpk')">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="innerDrawer = false">{{ $t('cancel') }} </el-button>
          <el-button type="primary" @click="dataFormSubmit"> {{ $t('confirm') }}</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import i18n from "@/i18n"
import basicAdd from "@/mixins/basic-add"
import debounce from "lodash/debounce"

export default {
  mixins: [basicAdd],
    data() {
      return {
        urlOptions: {
          submitURL: "/code/characteristics",
          getDictDataDetail: '/mutual/qmsDataDictionaryDetail/page',
          getProductListURL: '/basic/qmsProduct/page',
          getProcessURL: '/basic/qmsProcess/page',
          getGageTypeURL: "/gage/qmsGageType/page",
        },
        monitorList: [
          {
            value: 0,
            label: '否'
          },
          {
            value: 1,
            label: '是'
          },
        ],
        characteristicsTypeList: [
          {
            value: 0,
            label: '计量'
          },
          {
            value: 1,
            label: '计数'
          },
        ],
        inspectionTypeList: [{
          value: 0,
          label: '监控'
        },
        {
          value: 1,
          label: '电芯来料检验'
        },
        {
          value: 2,
          label: 'IQC抽检'
        },
        {
          value: 3,
          label: 'IQC抽检2'
        },
        {
          value: 4,
          label: '原料抽检'
        },
        {
          value: 5,
          label: '进货外观检验'
        },
        {
          value: 6,
          label: '库内原料检验'
        },
        {
          value: 7,
          label: '来料检验'
        },
        {
          value: 8,
          label: '胶片'
        },
        {
          value: 9,
          label: '抽检'
        },
        {
          value: 10,
          label: '巡检'
        }, {
          value: 11,
          label: '首检'
        },
        {
          value: 12,
          label: '末检'
        },
        {
          value: 13,
          label: '实时监测'
        },
        {
          value: 14,
          label: 'FQC抽检'
        },
        {
          value: 15,
          label: 'OQC抽检'
        },
        ],
        processList: [],
        productList: [],
        gageTypeList:[],
        dataForm: {
          analyzeDrawings:null,
          characteristicsType: null,
          company: null,
          decimalDigits: null,
          id: null,
          inspectionType: null,
          measuringToolsType: null,
          monitor: null,
          parameterName: null,
          parameterCode: null,
          processId: null,
          productId: null,
          remark: null,
          sampleSize: null,
          serialNo: null,
          source: null,
          specificationCenterline: null,
          specificationOffline: null,
          specifications: null,
          targetCpk: null,
          targetPpk: null,
          upperSpecificationLimit: null,
        },
        innerDrawer: false,
        listQuery: {
          limit: 10,
          page:1,
        },
        categoryList:[],
        requirementList: [],
        knowledgeBaseList:[],
      }
  },
  computed: {
    dataRule() {
      return {
        productId: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "change",
          },
        ],
        parameterCode: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        parameterName: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ]
      };
    },
  },
  mounted () {
    this.getDict()
    // this.getAccessPath()
  },
    methods: {
      init(obj) {
        this.dataForm.productId = obj.productId || ''
        this.dataForm.id = obj.id || ''
        this.innerDrawer = true
        this.$nextTick(() => {
          this.$refs["dataForm"].resetFields()
          if (this.dataForm.id) {
            this.getInfo()
          } else {
            // this.getCode()
          }
        })
      },
      handleClose() {
        this.innerDrawer = true
        this.$refs.dataForm.resetFields()
      },
      getDict() {
        this.$http
          .get(this.urlOptions.getProductListURL, {
            params: {
              limit: 999,
              page: 1,
            }
          })
          .then(({ data: res }) => {
            if (res.code === 0) {
              this.productList = res.data.list
            }
          })
        this.$http
          .get(this.urlOptions.getProcessURL, {
            params: {
              limit: 999,
              page: 1,
            }
          })
          .then(({ data: res }) => {
            if (res.code === 0) {
              this.processList = res.data.list
            }
          })
        this.$http
          .get(this.urlOptions.getGageTypeURL, {
            params: {
              limit: 999,
              page: 1,
            }
          })
          .then(({ data: res }) => {
            if (res.code === 0) {
              this.gageTypeList = res.data.list
            }
          })
      },
      getInfo() {
        this.$http
          .get(`/code/characteristics/${this.dataForm.id}`)
          .then(({ data: res }) => {
            if (res.code !== 0) {
              return this.$message.error(res.msg);
            }
            this.dataForm = {
              ...this.dataForm,
              ...res.data,
            };
          })
          .catch(() => { });
      },
      // 表单提交
      dataFormSubmit() {
        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.innerDrawer = false;
                  this.$emit("refreshDataList");
                },
              });
            })
            .catch(() => { });
        });
      },
    },
  }
</script>

<style lang="scss" scoped>

</style>
