<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-control">
        <span>设备名称:</span>
        <el-input v-model="listQuery.productName" placeholder="设备名称" @keyup.enter.native="handleFilter" />
      </div>
      <div class="filter-control">
        <span>设备SN码:</span>
        <el-input v-model="listQuery.snCode" placeholder="设备SN码" @keyup.enter.native="handleFilter" />
      </div>
      <div class="filter-control">
        <el-button type="primary" @click="handleFilter">查询</el-button>
      </div>
    </div>
    <!-- 表格区域 -->
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
      header-row-class-name="tableHeader"
    >
      <el-table-column label="序号" :index="indexMethod" type="index" align="center" width="80" />
      <el-table-column label="设备名称" align="center" prop="productName" />
      <el-table-column label="设备SN码" align="center" prop="snCode" />
      <el-table-column label="创建时间" class-name="status-col" width="160px" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.createTime | formatTime }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column v-if="false" label="创建人" width="180px" align="center" prop="createBy" /> -->
      <el-table-column label="操作" align="center" width="240px" class-name="small-padding fixed-width">
        <template slot-scope="{ row }">
          <el-button type="primary" size="mini" @click="$_navigationTo(`/logistics/device-detail?productId=${row.productId}`)">
            查看
          </el-button>
          <el-button :disabled="row.auditStatus !== 1" type="primary" size="mini" @click="handleShowQRcode(row)">
            二维码
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 页码 -->
    <pagination :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="getList" />

    <!-- 二维码 -->
    <transition name="fade">
      <qrcode-device v-if="showQRcode" :product-info="productInfo" @close="showQRcode = false" />
    </transition>
  </div>
</template>

<script>
import { getProdList } from '@/api/product'
import QrcodeDevice from '@/components/QrcodeDevice'
import { mapGetters } from 'vuex'
import { setStorage } from '@/utils/storage'
export default {
  name: 'ComplexTable',
  components: { QrcodeDevice },

  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        status: '',
        auditStatus: '',
        authId: '',
        productName: '',
        snCode: '',
        pageNum: 1,
        pageSize: 10
      },
      showQRcode: false,
      productInfo: {}
    }
  },
  computed: {
    ...mapGetters(['userIdentity'])
  },
  created() {
    this.listQuery.authId = this.$route.query.authId
    // 保存机构id，用于设备管理
    setStorage('device-setting-authId', this.listQuery.authId)
    this.getList()
  },

  methods: {
    // 获取列表信息
    getList() {
      getProdList(this.listQuery)
        .then((res) => {
          const { total, list } = res.data
          this.total = total
          this.list = list
        })
        .finally(() => {
          this.listLoading = false
        })
    },
    // 过滤列表
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    // 显示二维码
    handleShowQRcode(item) {
      this.productInfo = item
      this.productInfo.authParty = item.authParty
      this.showQRcode = true
    },
    indexMethod(index) {
      return index + this.listQuery.pageSize * (this.listQuery.pageNum - 1) + 1
    }
  }
}
</script>

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