<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">
        <span>审核状态:</span>
        <el-select v-model="listQuery.reviewStatus" placeholder="供应商类型" clearable @change="getList">
          <el-option label="所有类型" value="" />
          <el-option label="待审核" :value="1" />
          <el-option label="审核通过" :value="2" />
          <el-option label="审核未通过" :value="0" />
        </el-select>
      </div>
      <div class="filter-control">
        <el-button type="primary" @click="getList">查询</el-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.auditStatus" :reason="row.invalidReason" />
        </template>
      </el-table-column>
      <el-table-column label="审核时间" width="160px" align="center">
        <template slot-scope="{ row }">
          <span v-if="row.auditStatus !== 2">{{ row.auditTime | formatTime }}</span>
          <span v-else>—</span>
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center">
        <template slot-scope="{ row }">
          <span v-if="row.auditStatus !== 2">{{ row.auditBy }}</span>
          <span v-else>—</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="240px" align="center">
        <template slot-scope="{ row }">
          <audit-button-group
            :review="row.auditStatus !== 2"
            :type="row.auditStatus === 1 ? 'result' : 'audit'"
            :is-dot="row.checkFlag === 0"
            :margin-right="false"
            @click="onClick(row, $event)"
          />
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格区域END -->
    <!-- 页码 -->
    <pagination
      :total="total"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="fetchProductList"
    />

    <!-- 审核弹窗 -->
    <el-dialog title="驳回" :visible.sync="resultVisible" width="30%" @close="onDialogClose">
      <div class="auth-info">
        <el-form ref="formRef" :model="dialogData" label-width="80px" :rules="rules">
          <el-form-item label="原因：" prop="invalidReason">
            <el-input v-model="dialogData.invalidReason" type="textarea" rows="4" placeholder="请说明原因" />
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer">
        <el-button @click="resultVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { auditProduct, getProdList } from '@/api/product'
export default {
  name: 'AuditClubDeviceList',
  data() {
    return {
      listLoading: false,
      resultVisible: false,
      listQuery: {
        listType: 2,
        authId: '',
        productName: '',
        reviewStatus: '',
        snCode: '',
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      list: [],
      dialogData: {
        productId: '', // 授权机构id
        auditStatus: 0, // 审核状态
        invalidReason: '' // 审核信息
      },
      rules: {
        invalidReason: [{ required: true, message: '不通过原因不能为空', tigger: 'blur' }]
      }
    }
  },
  created() {
    this.listQuery.authId = this.$route.query.authId
    this.getList()
  },
  methods: {
    // 获取列表信息
    async 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) {
      const actions = {
        review: this.onReview,
        result: this.onResult,
        audit: this.onAudit
      }
      actions[type](row)
    },

    // 预览
    onReview(row) {
      this.$router.push(
        `/audit/club/device-detail?relationId=${row.relationId}&productId=${row.productId}&authId=${this.listQuery.authId}&type=review`
      )
    },
    // 驳回
    onResult(row) {
      this.resultVisible = true
      this.dialogData.productId = row.productId
    },
    // 审核
    onAudit(row) {
      this.$router.push(
        `/audit/club/device-detail?relationId=${row.relationId}&productId=${row.productId}&authId=${this.listQuery.authId}&type=audit`
      )
    },

    // 审核
    async onSubmit() {
      try {
        await this.$refs.formRef.validate()
        await this.onAuthResult()
      } catch (error) {
        console.log(error)
      }
    },

    // 驳回
    async onAuthResult() {
      try {
        await auditProduct(this.dialogData)
        this.$message.success('驳回成功')
        this.getList()
        this.resultVisible = false
      } catch (error) {
        console.log(error)
      }
    },

    onDialogClose() {
      this.$refs.formRef.resetFields()
    },

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

<style lang="scss" scoped>
.el-table .cell {
  overflow: visible;
}
</style>
