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

    <!-- 搜索工作栏 -->
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      label-width="68px"
    >
      <QueryFormWrapper>
        <el-col :span="6">
          <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="6">
          <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="6">
          <el-form-item label="存货类别名称" prop="categoryName" label-width="98px">
            <el-select
              v-model="queryParams.categoryName"
              placeholder="请选择存货类别"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in getDictDatas(DICT_TYPE.MES_MATERIAL_CATEGORIES)"
                :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="size">
            <el-input
              v-model="queryParams.size"
              placeholder="请输入规格型号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </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">
      <right-toolbar
        :show-search.sync="showSearch"
        :columns="columnsList"
        @queryTable="handleQuery"
        @saveSetting="getColumnsList"
      />
    </el-row>

    <!-- 列表 -->
    <AutoResizer :given-height="177">
      <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="code"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[1].visible"
            :key="Math.random()"
            label="产品名称"
            align="center"
            prop="name"
            width="150"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[2].visible"
            :key="Math.random()"
            label="规格型号"
            align="center"
            prop="size"
            width="300"
          />
          <el-table-column
            v-if="columnsList[3].visible"
            :key="Math.random()"
            label="存货类别名称"
            align="center"
            prop="categoryName"
          />
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
            width="180"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                v-hasPermi="['wms:material-item:query']"
                size="mini"
                type="text"
                icon="el-icon-magic-stick"
                @click="handleInspectionCase(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"
    />
    <!-- 检验方案 -->
    <!-- <HandleCase ref="handleCase" :inspection-type="1" /> -->
  </div>
</template>

<script>
import { getMaterialItemfqcPage } from '@/api/wms/quality/fqcInspectionCase'
// import HandleCase from '../components/HandleCase.vue'
import { getFormColumnsList } from '@/api/system/saveColumns/index'
import { mapGetters } from 'vuex'
import { isArray } from 'min-dash'

export default {
  name: 'FqcInspectionCase',
  components: {
    // HandleCase
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料列表
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        erpId: null,
        code: null,
        name: null,
        status: null,
        docunentStatus: null,
        size: null,
        categoryNumber: null,
        categoryName: null,
        purPrice: null,
        unitCode: null,
        createTime: [],
        erpMark: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 显示隐藏列参数
      columns: [
        { key: 0, label: '产品编码', visible: true },
        { key: 1, label: '产品名称', visible: true },
        { key: 2, label: '规格型号', visible: true },
        { key: 3, label: '存货类别名称', visible: true }
      ],
      // 显示隐藏持久化参数
      columnsList: []
    }
  },
  computed: {
    ...mapGetters([
      'userId'
    ])
  },
  watch: {
    // 监听路由参数的变化
    '$route'() {
      // 判断 id 是否存在并调用 getList 方法
      if (this.$route.query) {
        this.getList()
      }
    }
  },
  created() {
    // 获取显隐列列表
    this.getColumnsList()
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      getMaterialItemfqcPage(this.queryParams).then(response => {
        const { list = [] } = response.data || {}
        this.list = list.map(item => {
          item.erpMark = item.erpMark + ''
          return item
        })
        this.list = response.data.list
        this.total = response.data.total
        this.loading = false
      })
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        erpId: undefined,
        code: undefined,
        name: undefined,
        status: undefined,
        docunentStatus: undefined,
        size: undefined,
        categoryNumber: undefined,
        categoryName: undefined,
        purPrice: undefined,
        unitCode: undefined,
        erpMark: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 检验方案 */
    handleInspectionCase(row) {
      const { code, name, id, auditStatus } = row
      this.$router.push({ path: '/quality/inspect-case-operate', query: { caseInspectionType: '1', type: 1, code, name, id, auditStatus }})
      // const { code } = row
      // this.$refs.handleCase.queryParams.code = code
      // this.$refs.handleCase.queryParams.type = 1
      // this.$refs.handleCase.code = code
      // this.$refs.handleCase.viewOpen = true
    },
    // 获取显示隐藏列
    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>

