<template>
  <div class="admin-comments-page">
    <div class="page-header">
      <h2 class="page-title">评论管理</h2>
      <div class="header-actions">
        <el-select v-model="statusFilter" placeholder="状态筛选" clearable style="width: 120px">
          <el-option label="全部" value="" />
          <el-option label="正常" value="active" />
          <el-option label="已删除" value="deleted" />
        </el-select>
      </div>
    </div>

    <el-table :data="filteredComments" style="width: 100%" v-loading="loading">
      <el-table-column label="用户" width="180">
        <template #default="{ row }">
          <div class="user-cell">
            <el-avatar :size="32" :src="row.userAvatar">{{ row.userName?.charAt(0) }}</el-avatar>
            <span>{{ row.userName }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="Skill" width="150">
        <template #default="{ row }">
          {{ getSkillName(row.skillId) }}
        </template>
      </el-table-column>
      <el-table-column prop="rating" label="评分" width="100">
        <template #default="{ row }">
          <el-rate v-model="row.rating" disabled size="small" />
        </template>
      </el-table-column>
      <el-table-column prop="content" label="内容" min-width="200">
        <template #default="{ row }">
          <div class="content-cell">{{ row.content }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="likes" label="点赞" width="80" />
      <el-table-column prop="status" label="状态" width="100">
        <template #default="{ row }">
          <el-tag :type="row.status === 'active' ? 'success' : 'danger'" size="small">
            {{ row.status === 'active' ? '正常' : '已删除' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createdAt" label="时间" width="180" />
      <el-table-column label="操作" fixed="right" width="120">
        <template #default="{ row }">
          <el-button
            v-if="row.status === 'active'"
            text
            type="danger"
            size="small"
            @click="deleteComment(row)"
          >
            删除
          </el-button>
          <el-button
            v-else
            text
            type="success"
            size="small"
            @click="restoreComment(row)"
          >
            恢复
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useAdminStore, useSkillStore } from '@/stores'
import { ElMessage, ElMessageBox } from 'element-plus'

const adminStore = useAdminStore()
const skillStore = useSkillStore()

const loading = ref(false)
const statusFilter = ref('')

const comments = computed(() => adminStore.comments)

const filteredComments = computed(() => {
  if (!statusFilter.value) return comments.value
  return comments.value.filter(c => c.status === statusFilter.value)
})

onMounted(() => {
  adminStore.loadComments()
  skillStore.loadSkills()
})

const getSkillName = (skillId) => {
  const skill = skillStore.skills.find(s => s.id === skillId)
  return skill?.name || '未知'
}

const deleteComment = (comment) => {
  ElMessageBox.confirm('确定要删除该评论吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    adminStore.deleteComment(comment.id)
    ElMessage.success('已删除')
  }).catch(() => {})
}

const restoreComment = (comment) => {
  adminStore.restoreComment(comment.id)
  ElMessage.success('已恢复')
}
</script>

<style lang="scss" scoped>
.admin-comments-page {
  .page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;

    .page-title {
      font-size: 20px;
      color: #303133;
    }
  }

  .user-cell {
    display: flex;
    align-items: center;
    gap: 8px;
  }

  .content-cell {
    max-width: 300px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
</style>
