<template>
  <div class="rating-topic-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input v-model.trim="queryForm.keyword" clearable placeholder="请输入评价主题" />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table v-loading="listLoading" :data="list" :element-loading-text="elementLoadingText">
      <el-table-column align="center" label="封面" width="100">
        <template #default="{ row }">
          <el-image
            v-if="row.coverUrl"
            :preview-src-list="[row.coverUrl]"
            :src="getThumbUrl(row.coverUrl)"
            style="width: 50px; height: 50px; border-radius: 4px"
          />
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="评价主题" min-width="150" prop="title" show-overflow-tooltip />
      <el-table-column align="center" label="描述" min-width="200" prop="description" show-overflow-tooltip />

      <el-table-column align="center" label="状态" width="100">
        <template #default="{ row }">
          <el-tag v-if="row.status === 1" type="warning">审核中</el-tag>
          <el-tag v-else-if="row.status === 2" type="success">已审通过</el-tag>
          <el-tag v-else-if="row.status === 3" type="danger">已拒绝</el-tag>
          <el-tag v-else-if="row.status === 4" type="info">禁用</el-tag>
          <span v-else>-</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="允许评论" width="100">
        <template #default="{ row }">
          <el-tag v-if="row.allowComment === 1 || row.allowComment === true" type="success">是</el-tag>
          <el-tag v-else type="danger">否</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="参与人数" prop="participantCount" width="100" />
      <el-table-column align="center" label="评价人数" prop="scoreCount" width="100" />
      <el-table-column align="center" label="评论人数" prop="commentCount" width="100" />
      <el-table-column align="center" label="浏览数" prop="viewCount" width="100" />
      <el-table-column align="center" label="热门分数" prop="hotScore" width="100" />

      <el-table-column align="center" label="创建时间" prop="createdAt" width="160">
        <template #default="{ row }">
          {{ formatTime(row.createdAt) }}
        </template>
      </el-table-column>

      <el-table-column align="center" fixed="right" label="操作" width="120">
        <template #default="{ row }">
          <el-button type="text" @click="handleViewItems(row)">查看评分项</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>

<script>
  import { getList } from '@/api/rating/topic'
  import { formatTime } from '@/utils'
  import { getThumbUrl } from '@/utils/blessing'

  export default {
    name: 'RatingTopicIndex',
    data() {
      return {
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          keyword: '',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      formatTime,
      getThumbUrl,
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNo = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        try {
          const { data } = await getList(this.queryForm)
          this.list = data.list
          this.total = data.totalCount
        } catch (error) {
          console.error(error)
        } finally {
          this.listLoading = false
        }
      },
      handleViewItems(row) {
        this.$router.push({
          path: '/rating/topic-item',
          query: { topicId: row.id },
        })
      },
    },
  }
</script>

<style scoped>
  .rating-topic-container {
    padding: 20px;
  }
</style>
