<template>
  <div class="admin-comments">
    <!-- 搜索和筛选区域 -->
    <div class="filter-area">
      <el-form :inline="true" :model="searchForm" class="filter-form">
        <el-form-item label="内容关键词">
          <el-input v-model="searchForm.keyword" placeholder="评论内容" clearable @keyup.enter="handleSearch" />
        </el-form-item>
        
        <el-form-item label="帖子ID">
          <el-input v-model="searchForm.postId" placeholder="帖子ID" clearable @keyup.enter="handleSearch" />
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon> 搜索
          </el-button>
          <el-button @click="resetForm">
            <el-icon><RefreshLeft /></el-icon> 重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    
    <!-- 数据表格 -->
    <div class="table-area">
      <el-table
        v-loading="loading"
        :data="commentList"
        style="width: 100%"
        border
        stripe
        row-key="id"
      >
        <el-table-column type="index" width="60" align="center" label="序号" />
        
        <el-table-column label="评论内容" min-width="300">
          <template #default="{ row }">
            <div class="comment-content">
              <el-tooltip :content="row.content" placement="top" :show-after="1000">
                <p>{{ row.content }}</p>
              </el-tooltip>
              
              <div v-if="row.replyUsername" class="reply-info">
                <span>回复 @{{ row.replyUsername }}</span>
              </div>
            </div>
          </template>
        </el-table-column>
        
        <el-table-column label="评论者" width="150">
          <template #default="{ row }">
            <div class="user-info">
              <el-avatar :size="30" :src="row.userAvatar" />
              <span class="username">{{ row.username }}</span>
            </div>
          </template>
        </el-table-column>
        
        <el-table-column label="所属帖子" min-width="200">
          <template #default="{ row }">
            <router-link :to="`/post/${row.postId}`" class="post-link" target="_blank">
              {{ row.postTitle }}
            </router-link>
          </template>
        </el-table-column>
        
        <el-table-column prop="createTime" label="评论时间" width="180" sortable />
        
        <el-table-column label="点赞数" width="100" align="center">
          <template #default="{ row }">
            <span>{{ row.likeCount }}</span>
          </template>
        </el-table-column>
        
        <el-table-column label="操作" width="120" fixed="right">
          <template #default="{ row }">
            <el-popconfirm
              title="确定要删除该评论吗？此操作不可恢复！"
              @confirm="handleDelete(row)"
            >
              <template #reference>
                <el-button size="small" type="danger">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          background
          layout="total, sizes, prev, pager, next, jumper"
          v-model:current-page="pagination.page"
          v-model:page-size="pagination.size"
          :total="pagination.total"
          :page-sizes="[10, 20, 50, 100]"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import { Search, RefreshLeft } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';
import axios from 'axios';

// 搜索表单
const searchForm = reactive({
  keyword: '',
  postId: ''
});

// 分页
const pagination = reactive({
  page: 1,
  size: 10,
  total: 0
});

// 数据加载状态
const loading = ref(false);

// 评论列表
const commentList = ref([]);

// 加载评论列表
const loadComments = async () => {
  try {
    loading.value = true;
    const response = await axios.get('/api/admin/comments', {
      params: {
        page: pagination.page,
        size: pagination.size,
        keyword: searchForm.keyword || null,
        postId: searchForm.postId ? Number(searchForm.postId) : null
      }
    });
    
    if (response.data.code === 0) {
      const data = response.data.data;
      commentList.value = data.records;
      pagination.total = data.total;
    } else {
      ElMessage.error(response.data.msg || '加载评论列表失败');
    }
  } catch (error) {
    console.error('加载评论列表失败:', error);
    ElMessage.error('网络错误，请稍后重试');
  } finally {
    loading.value = false;
  }
};

// 搜索
const handleSearch = () => {
  pagination.page = 1;
  loadComments();
};

// 重置表单
const resetForm = () => {
  searchForm.keyword = '';
  searchForm.postId = '';
  handleSearch();
};

// 分页大小变化
const handleSizeChange = (size) => {
  pagination.size = size;
  loadComments();
};

// 当前页变化
const handleCurrentChange = (page) => {
  pagination.page = page;
  loadComments();
};

// 删除评论
const handleDelete = async (row) => {
  try {
    const response = await axios.delete(`/api/admin/comments/${row.id}`);
    
    if (response.data.code === 0) {
      ElMessage.success('评论已删除');
      loadComments();
    } else {
      ElMessage.error(response.data.msg || '删除失败');
    }
  } catch (error) {
    console.error('删除评论失败:', error);
    ElMessage.error('网络错误，请稍后重试');
  }
};

// 组件挂载时加载数据
onMounted(() => {
  loadComments();
});
</script>

<style scoped>
.admin-comments {
  width: 100%;
}

.filter-area {
  margin-bottom: 20px;
  padding: 15px;
  background-color: #f8f8f8;
  border-radius: 4px;
}

.filter-form {
  display: flex;
  flex-wrap: wrap;
}

.comment-content {
  max-width: 500px;
}

.comment-content p {
  margin: 0;
  max-height: 60px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  word-break: break-word;
  line-height: 1.5;
}

.reply-info {
  margin-top: 5px;
  font-size: 12px;
  color: #909399;
}

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

.username {
  font-size: 14px;
  color: #333;
}

.post-link {
  color: #409EFF;
  text-decoration: none;
  display: inline-block;
  max-width: 300px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.post-link:hover {
  text-decoration: underline;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

/* 表格响应式调整 */
@media screen and (max-width: 1200px) {
  .el-table {
    width: 100%;
    overflow-x: auto;
  }
}
</style> 