<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="getList" />
      </div>
      <div class="filter-control">
        <span>设备SN码:</span>
        <el-input v-model="listQuery.snCode" placeholder="请输入设备SN码" @keyup.enter.native="getList" />
      </div>
      <div class="filter-control">
        <permission-button type="primary" @click="getList">查询</permission-button>
      </div>
    </div>
    <!-- 搜索区域END -->
    <!-- 表格区域 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      style="width: 100%"
      border
      fit
      highlight-current-row
      header-row-class-name="tableHeader"
      cell-class-name="table-cell"
    >
      <el-table-column :index="indexMethod" label="序号" type="index" width="80" align="center" />
      <el-table-column prop="productName" label="设备名称" align="center" />
      <el-table-column prop="snCode" label="设备SN码" align="center" />
      <el-table-column label="审核状态" width="120px" align="center">
        <template slot-scope="{ row }">
          <audit-status :status="row.shopAuditStatus" :reason="row.invalidReason" />
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center">
        <template slot-scope="{ row }">
          <span v-if="row.shopAuditStatus !== 2">{{ row.shopAuditBy }}</span>
          <span v-else>—</span>
        </template>
      </el-table-column>
      <el-table-column label="审核时间" width="160px" align="center">
        <template slot-scope="{ row }">
          <span v-if="row.shopAuditStatus !== 2">{{ row.shopAuditTime | formatTime }}</span>
          <span v-else>—</span>
        </template>
      </el-table-column>
      <el-table-column label="注册时间" width="160px" align="center">
        <template slot-scope="{ row }">
          <span v-if="row.shopAuditStatus !== 2">{{ row.createTime | formatTime }}</span>
          <span v-else>—</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="240px" align="center">
        <template slot-scope="{ row }">
          <permission-button
            v-if="shopAuditStatus === 0"
            type="primary"
            size="mini"
            @click="onClick(row, 'audit')"
          >审核</permission-button>
          <permission-button v-else type="primary" size="mini" @click="onClick(row, 'review')">查看</permission-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格区域END -->
    <!-- 页码 -->
    <pagination
      :total="total"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="fetchProductList"
    />
  </div>
</template>

<script>
import { getProdList } from '@/api/product'
export default {
  props: {
    shopAuditStatus: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      listLoading: false,
      listQuery: {
        listType: 3,
        authId: '',
        productName: '',
        snCode: '',
        pageNum: 1,
        pageSize: 10,
        shopAuditStatus: 0
      },
      total: 0,
      list: []
    }
  },
  created() {
    this.listQuery.authId = this.$route.query.authId
    this.listQuery.shopAuditStatus = this.shopAuditStatus
    this.getList()
  },
  methods: {
    // 获取列表信息
    getList() {
      this.listQuery.pageNum = 1
      this.list = []
      this.fetchProductList()
    },

    async fetchProductList() {
      try {
        this.listLoading = true
        const res = await getProdList(this.listQuery)
        this.total = res.data.total
        this.list = res.data.list
      } catch (error) {
        console.log(error)
      } finally {
        this.listLoading = false
      }
    },

    onClick(row, type) {
      this.$router.push(
        `/supplier-audit/club/device-detail?relationId=${row.relationId}&productId=${row.productId}&authId=${this.listQuery.authId}&type=${type}`
      )
    },

    indexMethod(index) {
      return index + this.listQuery.pageSize * (this.listQuery.pageNum - 1) + 1
    }
  }
}
</script>
