<template>
  <div class="rating-topic-review-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">
      <!-- 展开行，展示评价项(topicItems) -->
      <el-table-column type="expand">
        <template #default="{ row }">
          <div v-if="row.topicItems && row.topicItems.length > 0" class="expand-content">
            <h4>包含的评价项列表</h4>
            <el-table border :data="row.topicItems" size="mini">
              <el-table-column align="center" label="评价项" width="200">
                <template #default="scope">
                  <div class="item-info">
                    <el-image v-if="scope.row.avatarUrl" class="item-avatar" :src="getThumbUrl(scope.row.avatarUrl)" />
                    <span>{{ scope.row.name }}</span>
                  </div>
                </template>
              </el-table-column>
              <el-table-column align="center" label="分类" prop="category" width="100">
                <template #default="{ row: item }">
                  <el-tag v-if="item.category" effect="plain" type="info">{{ item.category }}</el-tag>
                  <span v-else>-</span>
                </template>
              </el-table-column>

              <el-table-column align="center" label="状态" width="100">
                <template #default="scope">
                  <el-tag v-if="scope.row.status === 1" size="mini" type="warning">审核中</el-tag>
                  <el-tag v-else-if="scope.row.status === 2" size="mini" type="success">已审通过</el-tag>
                  <el-tag v-else-if="scope.row.status === 3" size="mini" type="danger">已拒绝</el-tag>
                  <el-tag v-else-if="scope.row.status === 4" size="mini" type="info">禁用</el-tag>
                  <span v-else>-</span>
                </template>
              </el-table-column>
              <el-table-column align="center" label="创建时间" prop="createdAt" width="160">
                <template #default="scope">
                  {{ formatTime(scope.row.createdAt) }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="操作" min-width="150">
                <template #default="scope">
                  <el-button size="mini" type="success" @click="handleReview(scope.row, 2, 'topicItem')">通过</el-button>
                  <el-button size="mini" type="danger" @click="handleReview(scope.row, 3, 'topicItem')">拒绝</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <div v-else class="expand-empty">该话题下暂无评价项</div>
        </template>
      </el-table-column>

      <el-table-column align="left" label="评价主题" min-width="250" width="200">
        <template #default="{ row }">
          <div class="topic-info-cell">
            <div class="topic-details">
              <div class="topic-title">{{ row.title }}</div>
              <div v-if="row.description" class="topic-desc" :title="row.description">{{ row.description }}</div>
            </div>
          </div>
        </template>
      </el-table-column>

      <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="createdAt" width="160">
        <template #default="{ row }">
          {{ formatTime(row.createdAt) }}
        </template>
      </el-table-column>

      <el-table-column align="center" fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button size="mini" type="success" @click="handleReview(row, 2, 'topic')">通过</el-button>
          <el-button size="mini" type="danger" @click="handleReview(row, 3, 'topic')">拒绝</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"
    />

    <review-edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { getPendingList } from '@/api/rating/topic'
  import { formatTime } from '@/utils'
  import { getThumbUrl } from '@/utils/blessing'
  import ReviewEdit from './components/ReviewEdit'

  export default {
    name: 'RatingTopicReviewIndex',
    components: { ReviewEdit },
    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 getPendingList(this.queryForm)
          this.list = data.list
          this.total = data.totalCount
        } catch (error) {
          console.error(error)
        } finally {
          this.listLoading = false
        }
      },
      handleReview(row, status, type) {
        this.$refs['edit'].showEdit(row, status, type)
      },
    },
  }
</script>

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

  .expand-content {
    padding: 10px 40px;
    background-color: #fafafa;
    border-radius: 4px;
  }
  .expand-content h4 {
    margin: 0 0 10px 0;
    color: #606266;
  }
  .expand-empty {
    padding: 20px;
    text-align: center;
    color: #909399;
  }
  .item-info {
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .item-avatar {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    margin-right: 10px;
  }

  .topic-info-cell {
    display: flex;
    align-items: center;
  }

  .topic-cover,
  .topic-cover-placeholder {
    width: 50px;
    height: 50px;
    border-radius: 4px;
    margin-right: 12px;
    flex-shrink: 0;
  }

  .topic-cover-placeholder {
    background-color: #f0f2f5;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #909399;
    font-size: 20px;
  }

  .topic-details {
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  .topic-title {
    font-weight: bold;
    color: #303133;
    margin-bottom: 4px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .topic-desc {
    font-size: 12px;
    color: #909399;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .stat-cell {
    display: flex;
    flex-direction: column;
    gap: 4px;
  }

  .stat-row {
    display: flex;
    align-items: center;
    font-size: 13px;
  }

  .stat-label {
    color: #909399;
    width: 90px;
  }

  .stat-value {
    color: #606266;
  }
</style>
