<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <div class="filter-container">
      <div class="filter-control">
        <span>机构名称:</span>
        <el-input v-model="listQuery.authParty" placeholder="机构名称" @keyup.enter.native="getList" />
      </div>
      <div class="filter-control">
        <span>审核状态:</span>
        <el-select v-model="listQuery.auditStatus" placeholder="审核状态" clearable @change="getList">
          <el-option label="全部" value="" />
          <el-option label="待审核" :value="2" />
          <el-option label="审核通过" :value="1" />
          <el-option label="审核未通过" :value="0" />
        </el-select>
      </div>
      <div class="filter-control">
        <span>设备认证审核状态:</span>
        <el-select v-model="listQuery.lowerAuditStatus" placeholder="供应商类型" clearable @change="getList">
          <el-option label="全部" value="" />
          <el-option label="已完成审核" :value="1" />
          <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 label="序号" :index="indexMethod" type="index" width="80" align="center" />
      <el-table-column prop="authParty" label="机构名称" 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.lowerAuditStatus === 0" class="status warning">未完成审核</span>
          <span v-if="row.lowerAuditStatus === 1" class="status success">已完成审核</span>
        </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" width="200px">
        <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="280px" 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="true"
            @click="onClick(row, $event)"
          />
          <el-badge :hidden="row.lowerAuditStatus === 1" :value="row.waitAuditNum" :max="99">
            <el-button
              type="primary"
              size="mini"
              @click="navigationTo(`/audit/club/device/list?authId=${row.authId}`)"
            >设备认证审核</el-button>
          </el-badge>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格区域END -->
    <!-- 页码 -->
    <pagination
      :total="total"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="fecthAuthList"
    />
    <!-- 审核弹窗 -->
    <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 { fecthAuthList, auditAuth } from '@/api/auth'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      // 是否显示dialog
      resultVisible: false,
      listLoading: false,
      // 查询参数
      listQuery: {
        listType: 2,
        authParty: '', // 授权机构
        authUserId: '', // 供应商用户id
        pageNum: 1, // 页码
        pageSize: 10, // 分页
        auditStatus: '', // 审核状态
        lowerAuditStatus: '' // 下属内容审核状态
      },
      total: 0,
      dialogData: {
        authId: '', // 授权机构id
        auditStatus: 0, // 审核状态
        invalidReason: '' // 审核信息
      },
      rules: {
        invalidReason: [{ required: true, message: '不通过原因不能为空', tigger: 'blur' }]
      },
      // 机构信息审核列表
      list: []
    }
  },
  computed: {
    ...mapGetters(['authUserId'])
  },
  created() {
    this.listQuery.authUserId = this.$route.query.authUserId
    this.getList()
  },
  methods: {
    // 获取授权列表
    async getList() {
      this.listQuery.pageNum = 1
      this.list = []
      this.fecthAuthList()
    },

    async fecthAuthList() {
      try {
        this.listLoading = true
        const res = await fecthAuthList(this.listQuery)
        const { list, total } = res.data
        this.list = list
        this.total = total
      } 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/club-detail?authId=${row.authId}&type=review`)
    },
    // 驳回
    onResult(row) {
      this.resultVisible = true
      this.dialogData.authId = row.authId
    },
    // 审核
    onAudit(row) {
      this.$router.push(`/audit/club/club-detail?authId=${row.authId}&type=audit`)
    },

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

    // 驳回
    async onAuthResult() {
      try {
        await auditAuth(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>
