<template>
  <el-dialog
    :title="inspectTitle"
    :visible.sync="visible"
    append-to-body
    width="1200px"
    @open="dialogOpen"
    @close="dialogClose"
  >
    <!-- 搜索工作栏 -->
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      label-width="98px"
      :rules="rules"
    >
      <el-row :gutter="10">
        <el-col :span="8">
          <el-form-item label="工序名称" prop="processNo">
            <ProcessSelect
              v-model="queryParams.processNo"
              :disabled="processNameShow"
              placeholder="请选择工序名称"
              @change="processChange"
            />
          </el-form-item>
        </el-col>
        <el-col :span="16">
          <el-button
            v-show="copyShow"
            type="primary"
            icon="el-icon-plus"
            @click="handleCopy"
          >新增标准</el-button>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="物料编码" prop="materialNo">
            <IpqcProductSelect
              ref="productSelect"
              v-model="queryParams.materialNo"
              clearable
              :disabled="selectDisable"
              @change="productChange"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="检验标准版本" prop="version">
            <el-input
              placeholder="请输入检验标准版本"
              disabled
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="物料名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入物料名称"
              clearable
              disabled
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>

    <!-- ipqc -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-show="addIpqcShow"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAddIpqc"
        >新增检验项</el-button>
      </el-col>
    </el-row>
    <!-- 列表 -->
    <el-table
      :data="list"
      border
      size="mini"
    >
      <el-table-column label="检验项编码" align="center" prop="inspectionNo">
        <template slot-scope="scope">
          <InspectCaseSelect
            ref="inspectCaseSelect"
            v-model="scope.row.inspectionNo"
            filterable
            :list="list"
            :type="queryParams.type"
            placeholder="请选择检验项名称"
            :disabled="scope.row.isEditCase !== 0"
            @change="inspectCaseChange"
          />
        </template>
      </el-table-column>
      <el-table-column label="检验项名称" align="center" prop="inspectionName" />
      <el-table-column label="检验项类型" align="center" prop="inspectionModel">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.WMS_INSPECTION_TYPE" :value="scope.row.inspectionModel" />
        </template>
      </el-table-column>
      <el-table-column
        label="检查规格/标准"
        align="center"
        prop="standardPre"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-input v-if="scope.row.inspectionModel === 1" v-model="scope.row.standardPre" :disabled="saveDisable" />
          <span v-else>{{ scope.row.standardPre }}</span>
        </template>
      </el-table-column>
      <el-table-column label="检查工具" align="center" prop="tool">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.WMS_INSPECTION_TOOLS" :value="scope.row.tool" />
        </template>
      </el-table-column>
      <el-table-column label="检查数量" align="center" prop="checkQuantity">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.WMS_CHECK_QUANTITY" :value="scope.row.checkQuantity" />
        </template>
      </el-table-column>
      <el-table-column label="下限值" align="center" prop="minValue">
        <template slot-scope="scope">
          <el-input v-model="scope.row.minValue" :disabled="scope.row.inspectionModel === 1 || saveDisable" @change="minValueChange(scope.row, scope.row.minValue)" />
        </template>
      </el-table-column>
      <el-table-column label="标准值" align="center" prop="standValue">
        <template slot-scope="scope">
          <el-input v-model="scope.row.standValue" :disabled="scope.row.inspectionModel === 1 || saveDisable" @change="standValueChange(scope.row, scope.row.standValue)" />
        </template>
      </el-table-column>
      <el-table-column label="上限值" align="center" prop="maxValue">
        <template slot-scope="scope">
          <el-input v-model="scope.row.maxValue" :disabled="scope.row.inspectionModel === 1 || saveDisable" @change="maxValueChange(scope.row, scope.row.maxValue)" />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="120"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            :disabled="saveDisable"
            icon="el-icon-delete"
            class="text-danger"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <span slot="footer">
      <el-button
        type="primary"
        :loading="addLoading"
        :disabled="saveDisable"
        @click="handleSave"
      >{{ btnText === 'save'?'保存':'新增' }}</el-button>
      <el-button
        type="primary"
        :disabled="commitDisable"
        :loading="submitLoading"
        @click="handleSubmit"
      >提交</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getUpdateMaterialItemDetailPage, updateBatchInspection, pushToVerifyInspection } from '@/api/wms/quality/iqcInspectionExecute'
import { createNewInspection } from '@/api/wms/quality/inspectStandardApproval'
import { getPageIpqcNew } from '@/api/wms/quality/ipqcInspectionExecute'
import InspectCaseSelect from '../../components/InspectCaseSelect.vue'
import IpqcProductSelect from '../../iqcInspection/inspectStandardApproval/IpqcProductSelect.vue'
import ProcessSelect from '../../iqcInspection/components/ProcessSelect.vue'

export default {
  name: 'UpdateInspection',
  components: {
    InspectCaseSelect,
    IpqcProductSelect,
    ProcessSelect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 提交遮罩层
      submitLoading: false,
      // 保存/新增遮罩层
      addLoading: false,
      inspectDisable: false,
      inspectTitle: '修改检验标准',
      // 是否需要检验
      refreshDisable: false,
      // 显示搜索条件
      showSearch: true,
      copyShow: false,
      // 总条数
      total: 0,
      // 物料检验方案明细列表
      list: [],
      // 弹出层标题
      title: '',
      // 保存提交按钮名称
      btnText: 'save',
      // 是否显示弹出层
      visible: false,
      queryObj: {
        version: undefined
      },
      // 刷新的参数id
      wmsMaterialAdditionalId: '',
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        wmsMaterialAdditionalId: undefined,
        materialNo: undefined,
        type: 3
      },
      // 保存提交置灰
      saveDisable: false,
      commitDisable: false,
      selectDisable: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // iqc/ipqc
      iqcInspectShow: false,
      processNameShow: false,
      addIpqcShow: true
    }
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      if (this.btnText === 'add') {
        const params = {
          pageNo: 1,
          pageSize: 10,
          code: this.queryParams.materialNo,
          processNo: this.queryParams.processNo,
          type: 3
        }
        // 执行查询
        getPageIpqcNew(params).then(response => {
          this.list = response.data.list
          this.total = response.data.total
        }).finally(() => {
          this.loading = false
        })
      } else {
        const params = {
          pageNo: 1,
          pageSize: 10,
          wmsMaterialAdditionalId: this.queryParams.wmsMaterialAdditionalId,
          name: null,
          type: 3
        }
        // 执行查询
        getUpdateMaterialItemDetailPage(params).then(response => {
          this.list = response.data.list
          this.total = response.data.total
        }).finally(() => {
          this.loading = false
        })
      }
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        wmsMaterialAdditionalId: undefined,
        wmsMaterialItemMasterId: undefined,
        wmsInspectionItemId: undefined,
        code: undefined,
        minValue: undefined,
        standValue: undefined,
        maxValue: undefined,
        standardPre: undefined,
        tool: undefined,
        checkQuantity: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 保存
    handleSave() {
      if (this.queryParams.materialNo === undefined || this.queryParams.processNo === undefined) {
        this.$modal.msgWarning('工序名称/物料编码不能为空')
        return
      }
      this.list = this.list.map(item => {
        item.code = this.queryParams.code === undefined ? this.queryParams.materialNo : this.queryParams.code
        item.inspectionItemId = item.inspectionItemId === undefined ? item.wmsInspectionItemId : item.inspectionItemId
        const {
          code,
          inspectionItemId: wmsInspectionItemId,
          inspectionModel,
          inspectionName,
          inspectionNo,
          maxValue,
          minValue,
          standValue,
          standardPre,
          tool,
          checkQuantity,
          wmsMaterialItemMasterId,
          wmsMaterialAdditionalId
        } = item
        return {
          code,
          wmsInspectionItemId,
          inspectionModel,
          inspectionName,
          inspectionNo,
          maxValue,
          minValue,
          standValue,
          standardPre,
          tool,
          checkQuantity,
          wmsMaterialItemMasterId,
          wmsMaterialAdditionalId
        }
      })
      this.addLoading = true
      if (this.btnText === 'save') {
        const params = {
          processNo: this.queryParams.processNo,
          wmsMaterialAdditionalId: this.queryParams.wmsMaterialAdditionalId,
          code: this.queryParams.code === undefined ? this.queryParams.materialNo : this.queryParams.code,
          updateNewReqVOList: this.list,
          type: this.queryParams.type
        }
        updateBatchInspection(params).then(res => {
          if (res.data === true) {
            this.$modal.msgSuccess('保存成功!')
          }
        }).finally(() => {
          this.addLoading = false
        })
      } else {
        const params = {
          processNo: this.queryParams.processNo,
          code: this.queryParams.code === undefined ? this.queryParams.materialNo : this.queryParams.code,
          createDetailReqVOList: this.list,
          type: this.queryParams.type
        }
        createNewInspection(params).then(res => {
          this.visible = false
          this.$modal.msgSuccess('新增成功!')
          this.$emit('addComplete')
        }).finally(() => {
          this.addLoading = false
        })
      }
    },
    // 提交
    handleSubmit() {
      if (this.queryParams.materialNo === undefined || this.queryParams.processNo === undefined) {
        this.$modal.msgWarning('工序名称/物料编码不能为空')
        return
      }
      // 处理list参数
      const params = {
        processNo: this.queryParams.processNo,
        code: this.queryParams.code === undefined ? this.queryParams.materialNo : this.queryParams.code,
        wmsMaterialAdditionalId: this.queryParams.wmsMaterialAdditionalId,
        updateNewReqVOList: this.list,
        type: this.queryParams.type
      }
      pushToVerifyInspection(params).then(res => {
        if (res.data === true) {
          this.$emit('addComplete')
          this.$modal.msgSuccess('提交成功')
          this.visible = false
        }
      })
    },
    dialogOpen() {
      if (this.btnText === 'add') {
        this.inspectTitle = '新增检验标准'
        this.getList()
      }
      if (this.btnText === 'save') {
        this.getList()
      }
    },
    // 标准值校验
    standValueChange(row, v) {
      const { maxValue, minValue } = row
      v = parseInt(v, 10)
      if ((maxValue !== null && parseInt(maxValue, 10) < v) || (minValue !== null && parseInt(minValue, 10) > v)) {
        this.$modal.notifyWarning('标准值不能大于上限值，不能小于下限值，请重新输入标准值!')
      }
    },
    minValueChange(row, v) {
      const { standValue } = row
      if (standValue !== null && parseInt(v, 10) > parseInt(standValue, 10)) {
        this.$modal.notifyWarning('标准值不能小于下限值，请重新输入标准值!')
      }
    },
    maxValueChange(row, v) {
      const { standValue } = row
      if (standValue !== null && parseInt(standValue, 10) > parseInt(v, 10)) {
        this.$modal.notifyWarning('标准值不能大于上限值，请重新输入标准值!')
      }
    },
    // 更新刷新数据
    caseComplete() {
      this.getList()
      this.$emit('updateCase')
    },
    // 选择物料 --- ipqc
    productChange(v) {
      const { code, name } = v
      this.queryParams.materialNo = code
      this.queryParams.name = name
      // this.getList()
    },
    // 已审核复制标准
    handleCopy(row) {
      this.btnText = 'add'
      this.inspectTitle = '新增检验标准'
      this.queryObj.version = '草稿'
      this.processNameShow = false
      this.addIpqcShow = true
      this.queryParams.processNo = this.queryParams.processName
      // this.getList()
      this.saveDisable = false
      this.selectDisable = false
    },
    // 弹出层关闭
    dialogClose() {
      this.iqcInspectShow = false
      this.list = []
    },
    // 选择工序
    processChange(v) {
      const { processNo } = v
      const { materialNo } = this.queryParams
      this.queryParams.processNo = processNo
      if (materialNo !== undefined) {
        this.getList()
      }
    },
    // 新增ipqc
    handleAddIpqc() {
      this.list.push({ inspectionNo: '', inspectionName: '', inspectionModel: '', standardPre: '', tool: '', checkQuantity: '', minValue: '', maxValue: '', standValue: '', id: '', isEditCase: 0 })
      this.$nextTick(() => {
        this.$refs.inspectCaseSelect.type = this.queryParams.type
        this.$refs.inspectCaseSelect.fetchCaseList()
      })
    },
    // 选择检验项
    inspectCaseChange(v) {
      const { inspectionNo, inspectionName, inspectionModel, inspectionType, inspectionItemId, tool, checkQuantity } = v
      const filterObj = this.list.find(item => item.inspectionNo === inspectionNo)
      filterObj.inspectionModel = inspectionModel
      filterObj.inspectionName = inspectionName
      filterObj.inspectionType = inspectionType
      filterObj.inspectionItemId = inspectionItemId
      filterObj.tool = tool
      filterObj.checkQuantity = checkQuantity
      filterObj.isEditCase = 1
    },
    // 删除检验项 -- ipqc
    handleDelete(row) {
      const { inspectionName, inspectionItemId } = row
      const index = this.list.findIndex(item => item.inspectionItemId === inspectionItemId)
      if (index > -1) {
        this.$modal.confirm('是否确认删除检验项名称为"' + inspectionName + '"的数据项?').then(() => {
          this.list.splice(index, 1)
        }).catch(() => {
          this.$modal.msg('您已取消操作')
        })
      }
    }
  }
}
</script>
