<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      label-width="68px"
    >
      <QueryFormWrapper>
        <el-col :span="8">
          <el-form-item label="方案编码" prop="code">
            <el-input
              v-model="queryParams.code"
              placeholder="请输入方案编码"
              clearable
              @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
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item
            label="状态"
            prop="status"
            label-width="98px"
          >
            <el-select
              v-model="queryParams.status"
              placeholder="请选择状态"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in getDictDatas(
                  DICT_TYPE.MES_COMMON_STATUS
                )"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <div slot="right">
          <el-form-item label-width="0" class="text-right" style="word">
            <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </div>
      </QueryFormWrapper>
    </el-form>
    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['wms:material-item-master:create']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <right-toolbar
        :show-search.sync="showSearch"
        :columns="columnsList"
        @queryTable="handleQuery"
        @saveSetting="getColumnsList"
      />
    </el-row>

    <!-- 列表 -->
    <el-tabs v-model="activeName" type="border-card" @tab-click="handleClick">
      <el-tab-pane label="未提交" name="inspectCaseNoCommit" lazy>
        <InspectionCaseTable
          ref="inspectCaseNoCommit"
          @updateInspectCase="handleInspectionCase"
          @updateDisabled="handleDisabled"
          @updateEnable="handleEnable"
          @submitCaseComplete="submitCaseComplete"
          @columns="getColumns"
        />
      </el-tab-pane>
      <el-tab-pane label="待审核" name="inspectCaseToApproval" lazy>
        <InspectionCaseTable
          ref="inspectCaseToApproval"
          @updateInspectCase="handleInspectionCase"
          @updateDisabled="handleDisabled"
          @updateEnable="handleEnable"
          @resetCaseApproval="resetCaseApproval"
          @approvalCaseComplete="approvalCaseComplete"
        />
      </el-tab-pane>
      <el-tab-pane label="已审核" name="inspectCaseApprovaled" lazy>
        <InspectionCaseTable
          ref="inspectCaseApprovaled"
          @updateInspectCase="handleInspectionCase"
          @updateDisabled="handleDisabled"
          @updateEnable="handleEnable"
        />
      </el-tab-pane>
    </el-tabs>
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="handleQuery"
    />
    <!-- 检验方案 -->
    <!-- <HandleCase ref="handleCase" :inspection-type="2" @caseComplete="handleQuery" /> -->
    <!-- 对话框(添加 / 修改) -->
    <el-dialog
      v-dialogDrag
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
      @close="dialogClose"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="方案编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入方案编码" :disabled="caseCodeDisabled" />
        </el-form-item>
        <el-form-item label="方案名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入方案名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getMaterialItemMaster, updateMaterialItemMaster, createMaterialItemMaster } from '@/api/wms/quality/inspectionCase'
// import HandleCase from '../components/HandleCase.vue'
import InspectionCaseTable from '../components/InspectionCaseTable.vue'

export default {
  name: 'OqcInspectionCase',
  components: {
    // HandleCase,
    InspectionCaseTable
  },
  provide() {
    return {
      paramsQuery: this.queryParams
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      caseCodeDisabled: false,
      // tab页高亮的
      activeName: 'inspectCaseNoCommit',
      // 总条数
      total: 0,
      // 物料列表
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        code: null,
        name: null,
        status: null,
        version: null,
        createTime: [],
        // 根据后端数据库修改oqc的type为1
        type: 1,
        auditStatus: -1
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 显隐列参数
      columnsList: []
    }
  },
  watch: {
    // 监听路由参数的变化
    '$route'() {
      // 判断 id 是否存在并调用 getList 方法
      if (this.$route.query) {
        this.$refs[this.activeName].getList()
      }
    }
  },
  created() {
  },
  methods: {
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        code: undefined,
        name: undefined,
        status: undefined,
        version: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.$refs[this.activeName].getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.$refs[this.activeName].handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.$router.push({ path: '/quality/inspect-case-operate', query: { caseInspectionType: '1', type: 1, auditStatus: null }})
      // this.$refs.handleCase.inspectDisable = false
      // this.$refs.handleCase.inspectNoDisable = false
      // this.$refs.handleCase.queryParams.type = 2
      // this.$refs.handleCase.queryParams.caseInspectionType = '2'
      // this.$refs.handleCase.viewOpen = true
      // this.$refs.handleCase.title = '新增检验方案'
      // this.$refs.handleCase.queryParams.inspectionNo = undefined
      // this.$refs.handleCase.queryParams.inspectionName = undefined
      // this.$refs.handleCase.caseTitle = ''
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id
      getMaterialItemMaster(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改oqc检验方案'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        // 添加的提交
        createMaterialItemMaster(this.form).then(response => {
          this.$modal.msgSuccess('新增成功')
          this.open = false
          this.$refs[this.activeName].getList()
        })
      })
    },
    /** 检验方案 */
    handleInspectionCase(row) {
      const { code, name, id, auditStatus } = row
      // 根据后端数据库修改 oqc的type改为1
      this.$router.push({ path: '/quality/inspect-case-operate', query: { caseInspectionType: '1', type: 1, code, name, id, auditStatus }})
      // const { code, name, type, id, auditStatus } = row
      // this.$refs.handleCase.queryParams.code = code
      // this.$refs.handleCase.queryParams.type = 2
      // this.$refs.handleCase.queryParams.inspectionNo = code
      // this.$refs.handleCase.queryParams.inspectionName = name
      // this.$refs.handleCase.queryParams.caseInspectionType = type + ''
      // this.$refs.handleCase.queryParams.id = id
      // this.$refs.handleCase.code = code
      // this.$refs.handleCase.caseTitle = name
      // this.$refs.handleCase.auditStatus = auditStatus
      // this.$refs.handleCase.viewOpen = true
      // this.$refs.handleCase.title = '修改'
      // this.$refs.handleCase.inspectDisable = true
      // this.$refs.handleCase.inspectNoDisable = true
    },
    handleDisabled(row) {
      this.$modal.confirm('确认要禁用检验方案吗？').then(() => {
        const { id, code, name, version } = row
        const status = 0
        const params = { id, code, name, status, version }
        updateMaterialItemMaster(params).then(res => {
          this.$modal.msgSuccess('操作成功！')
          this.$refs[this.activeName].getList()
        })
      }).catch(() => {
        this.$modal.msg('已取消操作')
      })
    },
    // 启用行
    handleEnable(row) {
      this.$modal.confirm('确认要启用当前检验方案吗？').then(() => {
        const { id, code, name, version } = row
        const status = 1
        const params = { id, code, name, status, version }
        updateMaterialItemMaster(params).then(res => {
          this.$modal.msgSuccess('操作成功！')
          this.$refs[this.activeName].getList()
        })
      }).catch(() => {
        this.$modal.msg('已取消操作')
      })
    },
    handleClick(tab, event) {
      this.activeName = tab.name
      if (this.activeName === 'inspectCaseNoCommit') {
        this.queryParams.auditStatus = -1
        if (this.$refs[this.activeName]) {
          this.$refs[this.activeName].getList()
        }
      } else if (this.activeName === 'inspectCaseToApproval') {
        this.queryParams.auditStatus = 0
        if (this.$refs[this.activeName]) {
          this.$refs[this.activeName].getList()
        }
      } else {
        this.queryParams.auditStatus = 1
        if (this.$refs[this.activeName]) {
          this.$refs[this.activeName].getList()
        }
      }
    },
    // 弹窗关闭
    dialogClose() {
      this.caseCodeDisabled = false
    },
    // 提交刷新
    submitCaseComplete() {
      const inspectCaseToApproval = this.$refs.inspectCaseToApproval
      inspectCaseToApproval && inspectCaseToApproval.handleQuery()
    },
    // 审核不通过刷新
    resetCaseApproval() {
      const inspectCaseNoCommit = this.$refs.inspectCaseNoCommit
      inspectCaseNoCommit && inspectCaseNoCommit.handleQuery()
    },
    // 审核通过刷新
    approvalCaseComplete() {
      const inspectCaseApprovaled = this.$refs.inspectCaseApprovaled
      inspectCaseApprovaled && inspectCaseApprovaled.handleQuery()
    },
    getColumns(data) {
      this.columnsList = data
    },
    // 获取显示隐藏列
    getColumnsList() {
      this.$nextTick(() => {
        this.$refs[this.activeName].getColumnsList()
      })
    }
  }
}
</script>

