<template>
  <div class="comment-management">
    <div class="page-header">
      <h2>评论管理</h2>
    </div>
    
    <!-- 搜索和筛选 -->
    <div class="search-bar">
      <el-input
        v-model="searchKeyword"
        placeholder="搜索评论内容"
        class="search-input"
        clearable
        @keyup.enter="handleSearch"
      >
        <template #prefix>
          <el-icon><search /></el-icon>
        </template>
      </el-input>
      
      <el-select
        v-model="selectedTargetType"
        placeholder="目标类型"
        clearable
        @change="handleSearch"
      >
        <el-option label="音乐" :value="1" />
        <el-option label="歌单" :value="2" />
        <el-option label="歌手" :value="3" />
      </el-select>
      
      <el-select
        v-model="selectedStatus"
        placeholder="状态"
        clearable
        @change="handleSearch"
      >
        <el-option label="正常" :value="1" />
        <el-option label="禁用" :value="0" />
      </el-select>
      
      <el-button type="primary" @click="handleSearch">搜索</el-button>
      <el-button @click="resetSearch">重置</el-button>
    </div>
    
    <!-- 评论列表 -->
    <el-table
      :data="commentList"
      style="width: 100%"
      border
      v-loading="loading"
    >
      <el-table-column prop="id" label="ID" width="80" align="center" />
      
      <el-table-column label="评论内容" min-width="300">
        <template #default="scope">
          <div class="comment-content">
            <div v-if="scope.row.parentId" class="reply-tag">回复</div>
            {{ scope.row.content }}
          </div>
        </template>
      </el-table-column>
      
      <el-table-column label="评论用户" width="150">
        <template #default="scope">
          <div class="user-info">
            <el-avatar :size="30" :src="scope.row.user.avatar || 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'" />
            <span>{{ scope.row.user.nickname || scope.row.user.username }}</span>
          </div>
        </template>
      </el-table-column>
      
      <el-table-column label="目标类型" width="100" align="center">
        <template #default="scope">
          {{ formatTargetType(scope.row.targetType) }}
        </template>
      </el-table-column>
      
      <el-table-column label="目标ID" width="100" align="center" prop="targetId" />
      
      <el-table-column label="状态" width="100" align="center">
        <template #default="scope">
          <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
            {{ scope.row.status === 1 ? '正常' : '禁用' }}
          </el-tag>
        </template>
      </el-table-column>
      
      <el-table-column label="创建时间" width="180" align="center">
        <template #default="scope">
          {{ formatDate(scope.row.createTime) }}
        </template>
      </el-table-column>
      
      <el-table-column label="操作" width="200" align="center">
        <template #default="scope">
          <el-button
            v-if="scope.row.status === 1"
            size="small"
            type="warning"
            @click="handleStatusChange(scope.row, 0)"
          >
            禁用
          </el-button>
          <el-button
            v-else
            size="small"
            type="success"
            @click="handleStatusChange(scope.row, 1)"
          >
            启用
          </el-button>
          <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        v-model:current-page="pagination.currentPage"
        v-model:page-size="pagination.pageSize"
        :page-sizes="[9, 18, 27, 36]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagination.total"
        :page-count="Math.ceil(pagination.total / pagination.pageSize)"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Search } from '@element-plus/icons-vue'
import { getCommentList, updateCommentStatus, deleteComment } from '../../api/admin/comment'

// 评论列表
const commentList = ref([])
// 加载状态
const loading = ref(false)
// 分页信息
const pagination = ref({
  currentPage: 1,
  pageSize: 5,
  total: 0
})
// 搜索关键字
const searchKeyword = ref('')
// 选中的目标类型
const selectedTargetType = ref(null)
// 选中的状态
const selectedStatus = ref(null)

// 获取评论列表
const fetchCommentList = async () => {
  loading.value = true
  try {
    const res = await getCommentList(
      pagination.value.currentPage,
      pagination.value.pageSize,
      searchKeyword.value,
      selectedTargetType.value,
      selectedStatus.value
    )
    
    if (res.code === 200) {
      commentList.value = res.data.list
      pagination.value.total = res.data.total
    } else {
      ElMessage.error(res.message || '获取评论列表失败')
    }
  } catch (error) {
    console.error('获取评论列表错误:', error)
    ElMessage.error('获取评论列表失败')
  } finally {
    loading.value = false
  }
}

// 处理搜索
const handleSearch = () => {
  pagination.value.currentPage = 1
  fetchCommentList()
}

// 重置搜索
const resetSearch = () => {
  searchKeyword.value = ''
  selectedTargetType.value = null
  selectedStatus.value = null
  pagination.value.currentPage = 1
  fetchCommentList()
}

// 处理页码变化
const handleCurrentChange = (page) => {
  pagination.value.currentPage = page
  fetchCommentList()
}

// 处理每页大小变化
const handleSizeChange = (size) => {
  pagination.value.pageSize = size
  pagination.value.currentPage = 1
  fetchCommentList()
}

// 处理状态变更
const handleStatusChange = (row, status) => {
  const statusText = status === 1 ? '启用' : '禁用'
  ElMessageBox.confirm(`确定要${statusText}该评论吗？`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      const res = await updateCommentStatus(row.id, status)
      if (res.code === 200) {
        ElMessage.success(`${statusText}成功`)
        // 更新本地数据
        row.status = status
      } else {
        ElMessage.error(res.message || `${statusText}失败`)
      }
    } catch (error) {
      console.error(`${statusText}评论错误:`, error)
      ElMessage.error(`${statusText}失败`)
    }
  }).catch(() => {})
}

// 处理删除
const handleDelete = (row) => {
  ElMessageBox.confirm('确定要删除该评论吗？此操作不可恢复', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      const res = await deleteComment(row.id)
      if (res.code === 200) {
        ElMessage.success('删除成功')
        // 重新获取列表
        fetchCommentList()
      } else {
        ElMessage.error(res.message || '删除失败')
      }
    } catch (error) {
      console.error('删除评论错误:', error)
      ElMessage.error('删除失败')
    }
  }).catch(() => {})
}

// 格式化目标类型
const formatTargetType = (type) => {
  switch (type) {
    case 1: return '音乐'
    case 2: return '歌单'
    case 3: return '歌手'
    default: return '未知'
  }
}

// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return ''
  const date = new Date(dateString)
  return date.toLocaleString()
}

// 页面加载时获取评论列表
onMounted(() => {
  fetchCommentList()
})
</script>

<style scoped>
.comment-management {
  padding: 20px;
  background: linear-gradient(135deg, #e1f5fe 0%, #b3e5fc 100%);
}

.page-header {
  margin-bottom: 15px;
  background: rgba(255, 255, 255, 0.9);
  padding: 10px 15px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.page-header h2 {
  color: #0d47a1;
  margin: 0;
}

.search-bar {
  display: flex;
  margin-bottom: 15px;
  gap: 10px;
  align-items: center;
  background: rgba(255, 255, 255, 0.9);
  padding: 10px 15px;
  border-radius: 12px;
}

.search-input {
  width: 300px;
}

.search-input :deep(.el-input__inner) {
  border-radius: 8px;
  border: 1px solid #b3e5fc;
}

.search-input :deep(.el-input__inner):focus {
  border-color: #4dd0e1;
  box-shadow: 0 0 0 2px rgba(77, 208, 225, 0.2);
}

.el-select :deep(.el-input__inner) {
  border-radius: 8px;
  border: 1px solid #b3e5fc;
}

.el-button {
  border-radius: 8px !important;
  font-weight: bold !important;
  transition: all 0.3s !important;
}

.el-button--primary {
  background: linear-gradient(135deg, #4dd0e1 0%, #26c6da 100%) !important;
  border: none !important;
}

.el-button--primary:hover {
  transform: translateY(-2px) !important;
  box-shadow: 0 4px 8px rgba(38, 198, 218, 0.3) !important;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
  background: rgba(255, 255, 255, 0.9);
  padding: 15px;
  border-radius: 12px;
}

.el-table {
  --el-table-border-color: #b3e5fc;
  --el-table-header-bg-color: #e1f5fe;
  --el-table-row-hover-bg-color: #b3e5fc;
  border-radius: 8px;
  overflow: hidden;
}

.el-table th.el-table__cell {
  background-color: #e1f5fe !important;
  color: #0d47a1 !important;
  font-weight: bold !important;
}

.el-table .el-table__row {
  background-color: rgba(255, 255, 255, 0.9) !important;
  transition: all 0.3s;
}

.el-table .el-table__row:hover {
  background-color: #b3e5fc !important;
  transform: scale(1.01);
}

.user-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.user-info .el-avatar {
  transition: all 0.3s;
}

.user-info .el-avatar:hover {
  transform: scale(1.1);
}

.comment-content {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 8px;
  background-color: rgba(255, 255, 255, 0.7);
  border-radius: 8px;
  transition: all 0.3s;
}

.comment-content:hover {
  background-color: rgba(255, 255, 255, 0.9);
  transform: scale(1.01);
}

.reply-tag {
  background-color: #e8f5e9;
  color: #2e7d32;
  padding: 2px 6px;
  border-radius: 8px;
  font-size: 12px;
  font-weight: bold;
}

.el-tag {
  border-radius: 8px !important;
}

.el-tag--success {
  background-color: #e8f5e9 !important;
  color: #2e7d32 !important;
}

.el-tag--danger {
  background-color: #ffebee !important;
  color: #c62828 !important;
}

.el-tag--warning {
  background-color: #fff8e1 !important;
  color: #f57f17 !important;
}
</style>
