<template>
  <div class="rating-topic-item-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-select v-model="queryForm.topicId" clearable filterable placeholder="请选择评价主题" @change="handleTopicChange">
              <el-option v-for="item in topicList" :key="item.id" :label="item.title" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <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-alert
      v-if="!queryForm.topicId"
      show-icon
      style="margin-bottom: 20px"
      title="请先在左侧选择一个评价主题，或从主题列表点击跳转过来"
      type="warning"
    />

    <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.avatarUrl"
            :preview-src-list="[row.avatarUrl]"
            :src="getThumbUrl(row.avatarUrl)"
            style="width: 50px; height: 50px; border-radius: 50%"
          />
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="评价项名称" min-width="150" prop="name" 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="排序" prop="sortOrder" width="80" />
      <el-table-column align="center" label="分数" prop="score" width="80" />
      <el-table-column align="center" label="总分数" prop="scoreTotal" width="100" />
      <el-table-column align="center" label="评价人数" prop="scoreCount" width="100" />
      <el-table-column align="center" label="平均分" prop="scoreAvg" width="100" />

      <el-table-column align="center" label="浏览数" prop="viewCount" width="80" />
      <el-table-column align="center" label="评论数" prop="commentCount" width="80" />
      <el-table-column align="center" label="打分人数" prop="ratingCount" width="100" />

      <el-table-column align="center" label="排名分" prop="rankScore" 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>

    <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/topicItem'
  import { getList as getTopicList } from '@/api/rating/topic'
  import { formatTime } from '@/utils'
  import { getThumbUrl } from '@/utils/blessing'

  export default {
    name: 'RatingTopicItemIndex',
    data() {
      return {
        list: [],
        topicList: [],
        listLoading: false,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          keyword: '',
          topicId: '',
        },
      }
    },
    watch: {
      '$route.query.topicId': {
        handler(newVal) {
          if (newVal && newVal !== this.queryForm.topicId) {
            this.queryForm.topicId = newVal
            this.fetchData()
          }
        },
        immediate: true,
      },
    },
    created() {
      this.initData()
    },
    methods: {
      formatTime,
      getThumbUrl,
      async initData() {
        // 先获取下拉列表的主题数据（暂时取100条）
        try {
          const { data } = await getTopicList({ pageNo: 1, pageSize: 100 })
          this.topicList = data.list || []
        } catch (error) {
          console.error(error)
        }
      },
      handleTopicChange() {
        this.queryForm.pageNo = 1
        this.fetchData()
      },
      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() {
        if (!this.queryForm.topicId) {
          this.list = []
          this.total = 0
          return
        }
        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
        }
      },
    },
  }
</script>

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