<template>
  <div class="supervisor-view">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>待审核反馈</span>
        </div>
      </template>
      <el-table :data="pendingFeedback" style="width: 100%" v-loading="loading">
        <el-table-column prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="title" label="标题"></el-table-column>
        <el-table-column prop="description" label="描述" show-overflow-tooltip></el-table-column>
        <el-table-column prop="pollutionType" label="污染类型" width="120"></el-table-column>
        <el-table-column prop="severityLevel" label="严重等级" width="120"></el-table-column>
        <el-table-column prop="createdAt" label="提交时间" width="180">
          <template #default="scope">
            {{ formatDateTime(scope.row.createdAt) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button size="small" @click="handleCreateTask(scope.row)">创建任务</el-button>
            <el-button size="small" type="danger" @click="handleRejectFeedback(scope.row)">拒绝</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- TODO: Add Task Management Section -->
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import type { FeedbackResponse } from '@/api/feedback';
import { getPendingFeedback, processFeedback } from '@/api/feedback'; // Assuming processFeedback exists
import { formatDateTime } from '@/utils/formatter';

const pendingFeedback = ref<FeedbackResponse[]>([]);
const loading = ref(false);

const fetchPendingFeedback = async () => {
  loading.value = true;
  try {
    const response = await getPendingFeedback();
    pendingFeedback.value = response;
  } catch (error) {
    ElMessage.error('获取待审核反馈失败');
    console.error(error);
  } finally {
    loading.value = false;
  }
};

const handleCreateTask = (feedback: FeedbackResponse) => {
  // TODO: Implement task creation logic
  console.log('Create task from feedback:', feedback.id);
  ElMessage.info(`准备为反馈 #${feedback.id} 创建任务`);
};

const handleRejectFeedback = async (feedback: FeedbackResponse) => {
  try {
    await ElMessageBox.confirm(`确定要拒绝这条反馈吗？ (ID: ${feedback.id})`, '确认操作', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    });
    
    // This assumes a 'processFeedback' function exists to update feedback status
    // to something like 'REJECTED' or 'CLOSED_INVALID'.
    // We may need to create or adjust this backend logic.
    await processFeedback(feedback.id, { status: 'CLOSED_INVALID' }); // Placeholder status
    ElMessage.success('反馈已拒绝');
    fetchPendingFeedback(); // Refresh list
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('操作失败');
    }
  }
};

onMounted(() => {
  fetchPendingFeedback();
});
</script>

<style scoped>
.supervisor-view {
  padding: 20px;
}
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
