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

    <!-- 列表 -->
    <AutoResizer :given-height="298">
      <template #default="{height}">
        <el-table
          v-if="columnsList.length > 0"
          v-loading="loading"
          :height="height"
          :data="list"
          border
        >
          <el-table-column
            v-if="columnsList[0].visible"
            :key="Math.random()"
            label="检验项编码"
            align="center"
            prop="inspectionNo"
            width="120"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[1].visible"
            :key="Math.random()"
            label="检验项名称"
            align="center"
            prop="inspectionName"
            width="100"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[2].visible"
            :key="Math.random()"
            label="检验项类型"
            align="center"
            prop="inspectionModel"
            width="100"
          >
            <template slot-scope="scope">
              <dict-tag :type="DICT_TYPE.WMS_INSPECTION_TYPE" :value="scope.row.inspectionModel" />
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[3].visible"
            :key="Math.random()"
            label="检验类别"
            align="center"
            prop="inspectionType"
          >
            <template slot-scope="scope">
              <dict-tag :type="DICT_TYPE.WMS_INSPECTION_CATEGORY" :value="scope.row.inspectionType" />
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[4].visible"
            :key="Math.random()"
            label="检查类别(大类)"
            align="center"
            prop="checkCategory"
            width="130"
          >
            <template slot-scope="scope">
              <dict-tag :type="DICT_TYPE.WMS_INSPECTION_ITEMS" :value="scope.row.checkCategory" />
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[5].visible"
            :key="Math.random()"
            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
            v-if="columnsList[6].visible"
            :key="Math.random()"
            label="单位"
            align="center"
            prop="standardSuf"
            width="135"
          />
          <el-table-column
            v-if="columnsList[7].visible"
            :key="Math.random()"
            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"
            class-name="small-padding fixed-width"
            fixed="right"
            width="160"
          >
            <template slot-scope="scope">
              <el-button
                v-if="paramsQuery.auditStatus === 0 || paramsQuery.auditStatus === -1"
                v-hasPermi="['wms:inspection-item:update']"
                size="mini"
                type="text"
                icon="el-icon-check"
                @click="handleApproval(scope.row)"
              >{{ paramsQuery.auditStatus === -1 ? '提交' : '审核' }}</el-button>
              <el-button
                v-hasPermi="['wms:inspection-item:update']"
                size="mini"
                type="text"
                :disabled="scope.row.auditStatus !== -1"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >修改</el-button>
              <el-button
                v-hasPermi="['wms:inspection-item:delete']"
                size="mini"
                type="text"
                icon="el-icon-delete"
                class="text-danger"
                :disabled="scope.row.auditStatus === 0"
                @click="handleDelete(scope.row)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </AutoResizer>
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 批核选择 -->
    <el-dialog
      title="审核"
      :visible.sync="auditVisible"
      width="30%"
      @close="auditClose"
    >
      <el-form
        ref="auditForm"
        :model="auditParams"
        size="small"
        label-width="98px"
        :rules="rules"
      >
        <el-row :gutter="20">
          <el-col>
            <el-form-item label="审核结果" prop="flag">
              <el-select v-model="auditParams.flag" clearable placeholder="请选择审核结果">
                <el-option
                  v-for="item in auditSelect"
                  :key="item.code"
                  :label="item.label"
                  :value="item.code"
                />
              </el-select>
            </el-form-item>
            <el-form-item v-show="auditParams.flag === 0" label="驳回原因" prop="auditComment">
              <el-input
                v-model="auditParams.auditComment"
                type="textarea"
                :rows="2"
                placeholder="请输入驳回的原因"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="auditVisible = false">取 消</el-button>
        <el-button type="primary" @click="ensureApproval">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { createInspectionItem, updateInspectionItem, updateInspectionItemAudit, deleteInspectionItem, getInspectionItemPage } from '@/api/wms/quality/inspectionItems'
import { getFormColumnsList } from '@/api/system/saveColumns/index'
import { mapGetters } from 'vuex'
import { isArray } from 'min-dash'

export default {
  name: 'InspectionItem',
  components: {
  },
  inject: ['paramsQuery'],
  data() {
    return {
      // 遮罩层
      loading: true,
      ensureLoading: false,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 审核选择
      auditVisible: false,
      // 总条数
      total: 0,
      // WMS 品质管理-检验项管理列表
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      inspectNoDisabled: false,
      // 审核提交的参数
      approvalObj: null,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10

        // inspectionNo: null,
        // inspectionName: null,
        // inspectionModel: null,
        // inspectionType: null,
        // createTime: []
      },
      // 审核参数
      auditParams: {
        flag: null,
        auditComment: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        flag: [{
          required: true,
          message: '请选择审批意见',
          trigger: 'blur'
        }]

      },
      auditForm: {},
      // 批核选择
      auditSelect: [
        {
          label: '不通过',
          code: 0
        },
        {
          label: '通过',
          code: 1
        }
      ],
      // 显示隐藏列参数
      columns: [
        { key: 0, label: '检验项编码', visible: true },
        { key: 1, label: '检验项名称', visible: true },
        { key: 2, label: '检验项类型', visible: true },
        { key: 3, label: '检验类别', visible: true },
        { key: 4, label: '检查类别(大类)', visible: true },
        { key: 5, label: '工具', visible: true },
        { key: 6, label: '单位', visible: true },
        { key: 7, label: '检查数量', visible: true }
      ],
      // 显示隐藏持久化参数
      columnsList: []
    }
  },
  computed: {
    ...mapGetters([
      'userId'
    ])
  },
  created() {
    // 获取显隐列列表
    this.getColumnsList()
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      const params = {
        ...this.paramsQuery,
        ...this.queryParams
      }
      getInspectionItemPage(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,
        inspectionNo: undefined,
        inspectionName: undefined,
        inspectionModel: undefined,
        inspectionType: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const { id } = row
      this.$emit('updateId', id)
    },
    /** 提交按钮 */
    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.getList()
          }).finally(() => {
            this.ensureLoading = false
            this.inspectNoDisabled = false
          })
          return
        }
        // 添加的提交
        createInspectionItem(this.form).then(response => {
          this.$modal.msgSuccess('新增成功')
          this.open = false
          this.getList()
        }).finally(() => {
          this.ensureLoading = false
        })
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const { id, inspectionName } = row
      this.$modal.confirm('是否确认删除检验项名称为"' + inspectionName + '"的数据项?').then(function() {
        return deleteInspectionItem(id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    // 提交/审核
    handleApproval(row) {
      const { auditStatus, auditComment } = row
      this.approvalObj = row || {}
      this.auditParams.auditComment = auditComment
      auditStatus === -1 ? this.handleCommitOrApproval(row) : this.auditVisible = true
    },
    handleCommitOrApproval(row) {
      const { id, auditStatus, auditComment } = row
      const params = {
        id,
        auditStatus: auditStatus === -1 ? 0 : this.auditParams.flag === 0 ? -1 : 1,
        auditComment
      }
      updateInspectionItemAudit(params).then(() => {
        const msg = auditStatus === -1 ? '提交成功' : '审批成功'
        this.$modal.msgSuccess(msg)
        this.getList()
        this.$emit('submitComplete')
      })
    },
    // 审核确定
    ensureApproval() {
      this.$refs['auditForm'].validate(valid => {
        if (!valid) {
          return
        }
        this.auditVisible = false
        this.handleCommitOrApproval(this.approvalObj)
        this.auditParams.flag === 0 ? this.$emit('resetApproval') : this.$emit('approvalComplete')
      })
    },
    // 审核窗口关闭
    auditClose() {
      this.auditParams.flag = undefined
    },
    // 获取显示隐藏列
    getColumnsList() {
      const { componentName } = this.$route.meta || null
      const params = {
        userId: this.userId,
        vueForm: componentName
      }
      getFormColumnsList(params).then(res => {
        if (isArray(res.data)) {
          this.columnsList = res.data.length === 0 ? this.columns : res.data
        } else {
          this.columnsList = this.columns
        }
      }).catch((err) => {
        if (err) {
          this.columnsList = this.columns
        }
      }).finally(() => {
        this.$emit('columns', this.columnsList)
      })
    }
  }
}
</script>

