<template>
  <el-dialog
    title="检验"
    :visible.sync="visible"
    width="1200px"
    append-to-body
    @open="dialogOpen"
  >
    <!-- 列表 -->
    <el-table
      v-loading="loading"
      :data="list"
      border
      size="mini"
    >
      <!-- <el-table-column
        label="检验单号"
        width="170"
        align="center"
        prop="inspectionNo"
      />
      <el-table-column label="检验项类型" align="center" prop="inspectionType">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.WMS_INSPECTION_TYPE" :value="scope.row.inspectionType" />
        </template>
      </el-table-column> -->
      <el-table-column
        label="产品编码"
        width="120"
        align="center"
        prop="productCode"
      />
      <el-table-column
        label="产品名称"
        align="center"
        prop="productName"
      />
      <el-table-column label="检验项名称" align="center" prop="inspectionName" />
      <el-table-column label="标准值" align="center" prop="standValue" />
      <el-table-column label="单项检验结果" align="center" prop="singleResult">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.MES_QUALITY_IPQC_SINGLE_RESULT" :value="scope.row.singleResult" />
        </template>
      </el-table-column>
      <el-table-column label="不良项(不良代码)" align="center" prop="badId" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="180"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['wms:material-item-actual:query']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleEditTestItem(scope.row)"
          >实测</el-button>
          <el-button

            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >编辑检验结果</el-button>
        </template>
      </el-table-column>
    </el-table>
    <span slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" :loading="submitLoading" @click="handleTesting">检验完成</el-button>
    </span>
    <!-- 实测 -->
    <MeasuredDialog ref="measuredDialog" />
    <!-- 检验结果 -->
    <EditInspectItem ref="editInspectItem" @complete="getList" />
    <!-- 处理结果 -->
    <el-dialog
      :visible.sync="open"
      title="不合格处理"
      append-to-body
      width="400px"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        size="mini"
        label-width="110px"
      >
        <el-form-item label="处理结果" prop="manage">
          <el-select
            v-model="form.manage"
            placeholder="请选择单项检验结果"
            clearable
          >
            <el-option
              v-for="dict in getDictDatas(DICT_TYPE.WMS_INSPECT_DEAL_RESULTS)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary" @click="handleComplete(1)">确定</el-button>
      </div>
    </el-dialog>

    <!-- 检验完成 -->
    <el-dialog
      :visible.sync="finishOpen"
      title="检验结果"
      append-to-body
      width="400px"
    >
      <!-- <span slot="footer"> -->
      <el-col class="text-center">
        <el-button @click="open = true">不合格</el-button>
        <el-button type="primary" :loading="submitLoading" @click="handleComplete(0)">合格</el-button>
      </el-col>

      <!-- </span> -->
    </el-dialog>

  </el-dialog>
</template>

<script>
import { getFqcpage, finishOqcMaterialItem } from '@/api/wms/quality/fqcInspectionExecute'
import MeasuredDialog from '../../iqcInspection/components/MeasuredDialog.vue'
import EditInspectItem from '../../iqcInspection/components/EditInspectItem.vue'

export default {
  components: {
    MeasuredDialog,
    EditInspectItem
  },
  data() {
    return {
      visible: false,
      finishOpen: false,
      open: false,
      loading: false,
      submitLoading: false,
      // 总条数
      // total: 0,
      // 品质检验执行主列表
      list: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        inspectionRegisterId: null,
        inspectionNo: null,
        inspectionResult: null,
        badNo: null,
        createTime: [],
        saleOutNo: null,
        warehousingOrderNo: null,
        type: null
      },
      form: {
        manage: null
      },
      rules: {
        manage: [{
          required: true,
          message: '请选择检验结果',
          trigger: 'blur'
        }]
      }
    }
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      getFqcpage(this.queryParams).then(response => {
        this.list = response.data.list
      }).finally(() => {
        this.loading = false
      })
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
    },
    reset() {
      this.form = {
        manage: undefined
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    dialogOpen() {
      this.$nextTick(() => {
        this.handleQuery()
      })
    },
    handleTesting() {
      this.finishOpen = true
    },
    handleComplete(qualified) {
      this.submitLoading = true
      const params = {
        inspection: qualified,
        saleOutNo: this.queryParams.saleOutNo
      }
      finishOqcMaterialItem(params).then(res => {
        const msg = qualified === 0 ? '合格' : '不合格'
        this.$modal.msgSuccess('检验完成！产品' + msg)
        this.$emit('complete')
        this.visible = false
        this.open = false
        this.finishOpen = false
        this.reset()
      }).finally(() => {
        this.submitLoading = false
      })
    },
    // 填入实测值
    handleEditTestItem(row) {
      const { id } = row
      this.$refs.measuredDialog.form = row
      this.$refs.measuredDialog.queryParams.materialItemResultId = id
      this.$refs.measuredDialog.visible = true
    },
    handleUpdate(row) {
      const { id } = row
      this.$refs.editInspectItem.visible = true
      this.$refs.editInspectItem.form.id = id
    }
  }
}
</script>
