<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      label-width="88px"
    >
      <QueryFormWrapper>
        <el-col :span="6">
          <el-form-item label="检验项编码" prop="inspectionNo">
            <el-input
              v-model="queryParams.inspectionNo"
              placeholder="请输入检验项编码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="检验项名称" prop="inspectionName">
            <el-input
              v-model="queryParams.inspectionName"
              placeholder="请输入检验项名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="检验类别" prop="inspectionType">
            <el-select
              v-model="queryParams.inspectionType"
              placeholder="请选择检验类别"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in getDictDatas(DICT_TYPE.WMS_INSPECTION_CATEGORY)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="检验项类型" prop="inspectionModel">
            <el-select
              v-model="queryParams.inspectionModel"
              placeholder="请选择检验项类型"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in getDictDatas(DICT_TYPE.WMS_INSPECTION_TYPE)"
                :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:inspection-item:create']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['wms:inspection-item:export']"
          type="warning"
          plain
          size="mini"
          icon="el-icon-download"
          :loading="downLoadLoading"
          @click="handleDownLoad"
        >导出</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="inspectNoCommit" lazy>
        <InspectApprovaled
          ref="inspectNoCommit"
          @submitComplete="submitComplete"
          @updateId="handleUpdate"
          @columns="getColumns"
        />
      </el-tab-pane>
      <el-tab-pane label="待审核" name="inspectToApproval" lazy>
        <InspectApprovaled
          ref="inspectToApproval"
          @updateId="handleUpdate"
          @resetApproval="handleResetApproval"
          @approvalComplete="approvalComplete"
        />
      </el-tab-pane>
      <el-tab-pane label="已审核" name="inspectApprovaled" lazy>
        <InspectApprovaled ref="inspectApprovaled" @updateId="handleUpdate" />
      </el-tab-pane>
    </el-tabs>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog
      v-dialogDrag
      :title="title"
      :visible.sync="open"
      width="900px"
      append-to-body
      @close="dialogClose"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="120px"
      >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="检验项编码" prop="inspectionNo">
              <el-input v-model="form.inspectionNo" disabled placeholder="检验项编码为自动生成，无需填写" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检验项名称" prop="inspectionName">
              <el-input v-model="form.inspectionName" placeholder="请输入检验项名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="检验项类型" prop="inspectionModel">
              <DictSelect
                v-model="form.inspectionModel"
                :dict-type="'WMS_INSPECTION_TYPE'"
                placeholder="请选择检验项类型"
                @change="change"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检验类别" prop="inspectionType">
              <DictSelect
                v-model="form.inspectionType"
                placeholder="请选择检验类别"
                :dict-type="'WMS_INSPECTION_CATEGORY'"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="检查类别(大类)" prop="checkCategory">
              <DictSelect
                v-model="form.checkCategory"
                placeholder="请选择检查类别"
                :dict-type="'WMS_INSPECTION_ITEMS'"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工具" prop="tool">
              <DictSelect
                v-model="form.tool"
                placeholder="请选择工具"
                :dict-type="'WMS_INSPECTION_TOOLS'"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="计量单位" prop="standardSuf">
              <!-- <el-input v-model="form.standardSuf" placeholder="请输入计量单位" /> -->
              <DictSelect v-model="form.standardSuf" placeholder="请选择计量单位" dict-type="WMS_INSPECTION_UNIT" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检查数量" prop="checkQuantity">
              <DictSelect
                v-model="form.checkQuantity"
                placeholder="请选择检查数量"
                clearable
                :dict-type="'WMS_CHECK_QUANTITY'"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" :loading="ensureLoading" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { createInspectionItem, updateInspectionItem, getInspectionItem, exportInspectionItemExcel1 } from '@/api/wms/quality/inspectionItems'
import InspectApprovaled from './components/InspectApprovaled.vue'

export default {
  name: 'InspectionItem',
  components: {
    InspectApprovaled
  },
  provide() {
    return {
      paramsQuery: this.queryParams
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      ensureLoading: false,
      // 导出遮罩层
      exportLoading: false,
      // 下载遮罩层
      downLoadLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // WMS 品质管理-检验项管理列表
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 标签名
      activeName: 'inspectNoCommit',
      inspectNoDisabled: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        inspectionNo: null,
        inspectionName: null,
        inspectionModel: null,
        inspectionType: null,
        createTime: [],
        auditStatus: -1
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // inspectionNo: [{ required: true, message: '检验项编码不能为空', trigger: 'blur' }],
        inspectionName: [{ required: true, message: '检验项名称不能为空', trigger: 'blur' }],
        inspectionModel: [{ required: true, message: '检验项类型不能为空', trigger: 'change' }],
        inspectionType: [{ required: true, message: '检验类别不能为空', trigger: 'change' }],
        checkCategory: [{ required: true, message: '检查类别不能为空', trigger: 'change' }],
        tool: [{ required: true, message: '检查工具不能为空', trigger: 'change' }],
        checkQuantity: [{ required: true, message: '检查数量不能为空', trigger: 'change' }],
        standardSuf: [{ required: false, message: '计量单位不能为空', trigger: 'blur' }]
      },
      // 显隐列
      columnsList: []
    }
  },
  methods: {
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        inspectionNo: undefined,
        inspectionName: undefined,
        inspectionModel: undefined,
        inspectionType: 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.open = true
      this.title = '添加检验项管理'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      getInspectionItem(row).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改WMS 品质管理-检验项管理'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        this.ensureLoading = true
        // 修改的提交
        if (this.form.id != null) {
          updateInspectionItem(this.form).then(response => {
            this.$modal.msgSuccess('修改成功')
            this.open = false
            this.ensureLoading = false
            this.$refs[this.activeName].getList()
          }).finally(() => {
            this.ensureLoading = false
            this.inspectNoDisabled = false
          })
          return
        }
        // 添加的提交
        createInspectionItem(this.form).then(response => {
          this.$modal.msgSuccess('新增成功')
          this.open = false
          this.ensureLoading = false
          this.$refs['inspectNoCommit'].getList()
        }).finally(() => {
          this.ensureLoading = false
        })
      })
    },
    // /** 导出按钮操作 */
    // handleExport() {
    //   // 处理查询参数
    //   const params = { ...this.queryParams }
    //   params.pageNo = undefined
    //   params.pageSize = undefined
    //   this.$modal.confirm('是否确认导出所有WMS 品质管理-检验项管理数据项?').then(() => {
    //     this.exportLoading = true
    //     return exportInspectionItemExcel(params)
    //   }).then(response => {
    //     this.$download.excel(response, 'WMS 品质管理-检验项管理.xls')
    //     this.exportLoading = false
    //   }).catch(() => {})
    // },
    // 计量型校验
    change(item) {
      if (item === '0') {
        this.rules.standardSuf[0].required = true
      } else {
        this.rules.standardSuf[0].required = false
      }
    },
    dialogClose() {
      this.inspectNoDisabled = false
    },
    handleClick(tab, event) {
      this.activeName = tab.name
      if (this.activeName === 'inspectNoCommit') {
        this.queryParams.auditStatus = -1
        this.queryParams.audited = undefined
        if (this.$refs[this.activeName]) {
          this.$refs[this.activeName].getList()
        }
      } else if (this.activeName === 'inspectToApproval') {
        this.queryParams.auditStatus = 0
        this.queryParams.audited = undefined
        if (this.$refs[this.activeName]) {
          this.$refs[this.activeName].getList()
        }
      } else {
        this.queryParams.auditStatus = null
        this.queryParams.audited = 1
        if (this.$refs[this.activeName]) {
          this.$refs[this.activeName].getList()
        }
      }
    },
    // 提交刷新
    submitComplete() {
      const inspectToApproval = this.$refs.inspectToApproval
      inspectToApproval && inspectToApproval.handleQuery()
    },
    // 审核不通过刷新
    handleResetApproval() {
      const inspectNoCommit = this.$refs.inspectNoCommit
      inspectNoCommit && inspectNoCommit.handleQuery()
    },
    // 审核通过刷新
    approvalComplete() {
      const inspectApprovaled = this.$refs.inspectApprovaled
      inspectApprovaled && inspectApprovaled.handleQuery()
    },
    // 检验项导出
    handleDownLoad() {
      this.$modal.confirm('您正在进行导出检验项列表的操作，是否继续?').then(() => {
        this.downLoadLoading = true
        return exportInspectionItemExcel1()
      }).then(response => {
        this.$download.excel(response, '检验项列表.xls')
        this.downLoadLoading = false
      }).catch(() => {})
    },
    getColumns(data) {
      this.columnsList = data
    },
    // 获取显示隐藏列
    getColumnsList() {
      this.$nextTick(() => {
        this.$refs[this.tabActivedName][0].getColumnsList()
      })
    }
  }
}
</script>

