<template>
  <!-- 基础页面 -->
  <div class="app-container">
    <el-form v-show="showSearch" :model="queryParams" ref="queryForm" size="small" inline @submit.native.prevent>
      <el-form-item label="关键字" prop="keyWord">
        <el-input
          v-model="queryParams.keyWord"
          placeholder="请输入关键字"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 240px"
        />
      </el-form-item>
      <!-- <el-form-item label="设备类型" prop="typeCodeId">
        <el-select
          v-model="queryParams.typeCodeId"
          placeholder="请选择设备类型"
          clearable
          filterable
          style="width: 240px"
          @change="handleType"
        >
          <el-option v-for="(item, index) in typeCodeOpts" :key="index" :label="item.typeName" :value="item.typeId" />
        </el-select>
      </el-form-item> -->
      <el-form-item label="规格型号" prop="maModel">
        <el-input
          v-model="queryParams.maModel"
          placeholder="请输入规格型号"
          clearable
          :maxlength="99"
          style="width: 240px"
        />
      </el-form-item>
      <!-- 表单按钮 -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table :data="tableList" fit highlight-current-row style="width: 100%">
      <el-table-column
        type="index"
        width="55"
        label="序号"
        align="center"
        :index="index => (queryParams.pageNum - 1) * queryParams.pageSize + index + 1"
      />
      <el-table-column
        v-for="(column, index) in tableColumns"
        show-overflow-tooltip
        :key="index"
        :label="column.label"
        :prop="column.prop"
        align="center"
      >
        <!-- 插槽 -->
        <template v-slot="{ row }" v-if="column.prop == 'qrCode'">
          <span>
            <el-button v-if="row.qrCode" type="text" size="mini" @click="handleSee(row)">查看</el-button>
            <span v-else>未绑定</span>
          </span>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 二维码 -->
    <QRCodeView ref="qrCodeView" />
  </div>
</template>

<script>
import TreeSelect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import QRCodeView from '@/components/QRCodeView'
import { getMachineInfoApi, getLevelListApi } from '@/api/materialsStation'

export default {
  components: { TreeSelect, QRCodeView },
  data() {
    return {
      showSearch: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyWord: '', // 关键字
        typeCodeId: undefined, // 设备类型ID
        typeCode: '', // 设备类型
        maModel: undefined, // 规格型号ID
        maModel: '' // 规格型号
      },
      typeCodeOpts: [], // 设备类型
      modelTypeOpts: [], // 规格型号
      total: 0, // 总条数
      // 表头
      tableColumns: [
        { label: '物品类型', prop: 'materialName' },
        { label: '设备类型', prop: 'typeCode' },
        { label: '规格型号', prop: 'maModel' },
        { label: '设备编号', prop: 'maCode' },
        // { label: '设备状态', prop: 'maStatus' },
        { label: '本次检验时间', prop: 'thisCheckTime' },
        { label: '本次检验人员', prop: 'checkMan' },
        { label: '下次检验时间', prop: 'nextCheckTime' },
        { label: '二维码', prop: 'qrCode' },
        { label: '出厂时间', prop: 'outFacTime' },
        { label: '入库时间', prop: 'inputTime' }
      ],
      // 表格数据
      tableList: []
    }
  },
  created() {
    this.getList()
    // this.getModelType()
  },
  methods: {
    // 查询
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 重置
    handleReset() {
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 10
      this.$refs.queryForm.resetFields()
      this.queryParams.typeCode = ''
      this.queryParams.maModel = ''
      this.getList()
    },
    // 获取列表
    async getList() {
      console.log('列表-查询', this.queryParams)
      const loading = this.$loading({ text: '加载中...' })
      try {
        const params = { ...this.queryParams }
        const res = await getMachineInfoApi(params)
        console.log('🚀 ~ 获取列表 ~ res:', res)
        this.tableList = res.data.rows
        this.total = res.data.total
        loading.close()
      } catch (error) {
        console.log('🚀 ~ 获取列表 ~ error:', error)
        this.tableList = []
        this.total = 0
        loading.close()
      }
    },
    // 获取设备类型树
    async getModelType() {
      try {
        const res = await getLevelListApi({ level: 3 })
        const res2 = await getLevelListApi({ level: 4 })
        this.typeCodeOpts = res.data
        this.modelTypeOpts = res2.data
      } catch (error) {
        console.log('获取设备类型树失败', error)
      }
    },
    handleType(val) {
      console.log('🚀 ~ handleType ~ val:', val)
      this.queryParams.typeCode = this.typeCodeOpts.find(item => item.typeId === val)?.typeName || ''
    },
    handleModel(val) {
      console.log('🚀 ~ handleModel ~ val:', val)
      this.queryParams.maModel = this.modelTypeOpts.find(item => item.typeId === val)?.typeName || ''
    },
    // normalizer(node) {
    //   if (node.children && !node.children.length) {
    //     delete node.children
    //   }
    //   return {
    //     id: node.typeId,
    //     label: node.typeName,
    //     children: node.children
    //   }
    // },
    handleSee(row) {
      this.$refs.qrCodeView.showQRCode(row.qrCode, row.maCode)
    }
  }
}
</script>

<style lang="scss" scoped></style>
