<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-control">
        <span>机构名称:</span>
        <el-input
          v-model="listQuery.authParty"
          placeholder="授权机构"
          style="width: 280px"
          class="filter-item"
          @keyup.enter.native="handleFilter"
        />
      </div>
      <div class="filter-control">
        <span>审核状态:</span>
        <el-select
          v-model="listQuery.auditStatus"
          placeholder="审核状态"
          clearable
          style="width: 200px"
          class="filter-item"
          @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.status"
          placeholder="上线状态"
          clearable
          style="width: 200px"
          class="filter-item"
          @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>
    <!-- 表格区域 -->
    <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" align="center" width="80" type="index" />

      <el-table-column label="机构名称" align="center" prop="authParty" />

      <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="120px" align="center">
        <template slot-scope="{ row }">
          <!-- 只有审核通过了才能操作上下线 auditStatus ：审核状态 -->
          <template v-if="row.auditStatus === 1">
            <template v-if="row.status === 0">
              <span style="margin-right: 10px" class="status danger">已下线</span>
            </template>
            <template v-else>
              <span style="margin-right: 10px" class="status success">已上线</span>
            </template>
          </template>
          <template v-else>
            <!-- <el-tag type="warning">待上线</el-tag> -->
            <span style="margin-right: 10px" class="status warning">待上线</span>
          </template>
        </template>
      </el-table-column>

      <el-table-column label="创建时间" class-name="status-col" width="160px">
        <template slot-scope="{ row }">
          <span>{{ row.createTime | formatTime }}</span>
        </template>
      </el-table-column>

      <!-- <el-table-column label="创建人" class-name="status-col" width="160px" prop="createBy" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="240px">
        <template slot-scope="{ row }">
          <el-button type="primary" size="mini" @click="navigationTo(`club-detail?authId=${row.authId}`)">
            查看
          </el-button>
          <el-button type="primary" size="mini" @click="navigationTo(`club-device-list?authId=${row.authId}`)">
            查看设备认证
          </el-button>
        </template>
      </el-table-column>
    </el-table>

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

<script>
import { fecthAuthList } from '@/api/auth'
import { mapGetters } from 'vuex'
export default {
  name: 'ComplexTable',
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      // 查询参数
      listQuery: {
        authParty: '', // 授权机构
        authUserId: '', // 供应商用户id
        pageNum: 1, // 页码
        pageSize: 10, // 分页
        status: ''
      },
      disabled: false,

      // ------------------
      improtDialogVisible: false,
      requestLoading: false,
      uploadFileList: [],
      improtDialogFormData: {
        fileUrl: ''
      },
      improtDialogFormRules: {
        fileUrl: [
          {
            required: true,
            message: '请选择文件',
            trigger: 'change'
          }
        ]
      },
      chooseFile: ''
    }
  },
  computed: {
    ...mapGetters(['authUserId', 'userIdentity', 'proxyInfo', 'copyUserInfo']),
    saveBtnClickable() {
      return this.uploadFileList.length > 0
    }
  },
  created() {
    this.listQuery.authUserId = this.$route.query.id || this.proxyInfo?.authUserId || this.authUserId
    const type = this.$route.query.type
    // 如果是通过路由参数传递的type,则需要同步到store
    if (type) {
      this.$store.commit('user/SET_SHOP_TYPE', parseInt(type))
    }
    this.getList()
  },
  methods: {
    // 获取授权列表
    getList() {
      this.listLoading = true
      fecthAuthList(this.listQuery)
        .then((response) => {
          if (response.code !== 0) {
            return this.$message.error('授权列表信息获取失败')
          }
          const { list, total } = response.data
          this.list = list
          this.total = total
        })
        .catch((err) => {
          console.log(err)
          return this.$message.error('授权列表信息获取失败')
        })
        .finally(() => {
          this.listLoading = false
        })
    },
    // 过滤列表
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },

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

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