<template>
  <el-dialog
    v-dialogDrag
    title="查看"
    :visible.sync="open"
    width="1200px"
    append-to-body
    @open="dialogOpen"
    @close="dialogClose"
  >

    <!-- 操作工具栏 -->

    <!-- 列表 -->
    <el-table
      v-loading="loading"
      :data="list"
      border
      size="mini"
    >
      <el-table-column label="入库单号" align="center" prop="warehousingOrderNo" />
      <el-table-column label="产品编码" align="center" prop="productCode" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="计划数量" align="center" prop="planAmount" />
      <el-table-column
        label="生产订单编号"
        align="center"
        prop="workOrderNo"
        width="120"
      />
      <el-table-column label="箱号" align="center" prop="caseNo" />
      <el-table-column label="箱数" align="center" prop="inQty" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="100"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['wms:incoming-produce-detail:query']"
            type="text"
            icon="el-icon-magic-stick"
            size="mini"
            @click="handleInspect(scope.row)"
          >检验执行</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" :loading="submitLoading" @click="handleTesting">检验完成</el-button>
    </div>

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

      <!-- </span> -->
    </el-dialog>
    <!-- 检验 -->
    <TempInspectList ref="tempInspectList" />
  </el-dialog>
</template>

<script>
import { getIncomingProduceDetailPage } from '@/api/wms/incoming/production'
import { fqccreateMaterialItemResult, finishFqcMaterialItem } from '@/api/wms/quality/tempInspectionExecute'
import TempInspectList from './TempInspectList.vue'
export default {
  components: {
    TempInspectList
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      finishOpen: false,
      submitLoading: false,
      // 总条数
      total: 0,
      // 入库业务 生产入库明细列表
      list: [],
      // 弹出层标题
      title: '',
      id: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        masterId: null,
        warehousingOrderNo: null,
        productCode: null,
        planAmount: null,
        workOrderNo: null,
        caseNo: null,
        inQty: null,
        stockCode: null,
        areaCode: null,
        locationCode: null,
        createTime: [],
        id: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        masterId: [{ required: true, message: '主表ID不能为空', trigger: 'blur' }],
        productCode: [{ required: true, message: '产品编码不能为空', trigger: 'blur' }],
        workOrderNo: [{ required: true, message: '生产订单编号不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    // this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      getIncomingProduceDetailPage(this.queryParams).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.loading = false
      }).finally(() => {
        this.loading = false
      })
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        masterId: undefined,
        warehousingOrderNo: undefined,
        productCode: undefined,
        planAmount: undefined,
        workOrderNo: undefined,
        caseNo: undefined,
        inQty: undefined,
        stockCode: undefined,
        areaCode: undefined,
        locationCode: undefined
      }
      this.resetForm('form')
    },
    dialogOpen() {
      this.$nextTick(() => {
        this.getList()
      })
    },
    dialogClose() {
      this.getList()
    },
    // 检验执行
    handleInspect(row) {
      const { warehousingOrderNo, workOrderNo, productCode } = row
      const params = {
        warehousingOrderNo,
        materialLots: workOrderNo,
        materialNo: productCode,
        type: 1
      }
      fqccreateMaterialItemResult(params).then(res => {
        if (res.code !== 0) {
          this.$modal.msgWarning('暂未权限~')
          return
        }
        this.$refs.tempInspectList.visible = true
        this.$refs.tempInspectList.queryParams.warehousingOrderNo = warehousingOrderNo
        this.$refs.tempInspectList.queryParams.materialLots = workOrderNo
        this.$refs.tempInspectList.queryParams.type = 1
      })
    },
    handleTesting() {
      this.finishOpen = true
    },
    handleComplete(qualified) {
      this.submitLoading = true
      const params = {
        inspection: qualified,
        warehousingOrderNo: this.queryParams.warehousingOrderNo
      }
      finishFqcMaterialItem(params).then(res => {
        const msg = qualified === 0 ? '合格' : '不合格'
        this.$modal.msgSuccess('检验完成！产品' + msg)
        this.$emit('qualifiedComplete')
        this.visible = false
        this.open = false
        this.finishOpen = false
        this.reset()
      }).finally(() => {
        this.submitLoading = false
      })
    }
  }
}
</script>

