<template>
  <el-dialog
    v-dialogDrag
    :visible.sync="visible"
    :title="title"
    append-to-body
    width="400px"
    @open="onOpen"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      size="mini"
      label-width="110px"
    >
      <el-form-item label="单项检验结果" prop="singleResult">
        <DictSelect v-model="form.singleResult" placeholder="请选择单项检验结果" dict-type="MES_QUALITY_IPQC_SINGLE_RESULT" />
      </el-form-item>
      <el-form-item v-show="allInspectShow" label="不良项" prop="badId">
        <BadcodeSelect v-model="form.badId" @change="badCodeChange" />
      </el-form-item>
      <!-- 非计量型 -->
      <el-form-item v-show="inspectionModel===1 && allInspectShow" label="实测结果" prop="measuredResults">
        <el-input v-model="form.measuredResults" placeholder="请输入实测结果" />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="close">取消</el-button>
      <el-button type="primary" :loading="ensureLoading" @click="handelConfirm">确定</el-button>
    </div>

  </el-dialog>
</template>

<script>
import BadcodeSelect from '../../components/BadCodeSelect.vue'
import { getIqcInspect, updateIqcMaterialItem } from '@/api/wms/quality/iqcInspectionExecute'
import { updateAllInspect } from '@/api/wms/quality/inspectionCase'

export default {
  components: { BadcodeSelect },
  data() {
    return {
      inspectionModel: '',
      checkCategory: undefined,
      ensureLoading: false,
      allInspectShow: true,
      materialLots: undefined,
      title: '',
      form: {
        id: undefined, // 品控方案表ID
        singleResult: '1',
        badId: undefined
      },
      selectRows: [],
      rules: {
        singleResult: [{
          required: true,
          message: '请输入单项检验结果',
          trigger: 'change'
        }]
      },
      visible: false
    }
  },
  methods: {
    onOpen() {
      this.$nextTick(() => {
        this.queryDetails()
      })
    },
    close() {
      this.reset()
      this.visible = false
    },
    reset() {
      this.form = {
        id: undefined,
        singleResult: '1',
        badId: undefined
      }
      this.resetForm('form')
    },
    handelConfirm() {
      this.$refs['form'].validate(valid => {
        if (!valid) return
        this.ensureLoading = true
        if (this.title === '一键检验结果') {
          const selectIds = this.selectRows.map(item => {
            const { id } = item
            return id
          })
          const params = {
            singleResult: this.form.singleResult,
            id: '', // 多余参数
            resultIds: selectIds,
            materialLots: this.materialLots
          }
          updateAllInspect(params).then(res => {
            this.$modal.msgSuccess('操作成功！')
            this.$emit('complete')
            this.close()
          }).finally(() => {
            this.checkCategory = undefined
            this.ensureLoading = false
          })
        } else {
          this.form.processNo = 'PURCHASE'
          updateIqcMaterialItem(this.form).then(res => {
            this.$modal.msgSuccess('操作成功！')
            this.$emit('complete')
            this.close()
          }).finally(() => {
            this.ensureLoading = false
          })
        }
      })
    },
    queryDetails() {
      getIqcInspect(this.form.id).then(res => {
        this.form = res.data
        if ([undefined, null].includes(this.form.singleResult)) {
          this.form.singleResult = '1'
        }
      })
    },
    badCodeChange(v) {
      console.log(v)
    }
  }
}

</script>
  <style>
  </style>
