<template>
  <div class="eval-manage">
    <!-- Page Header -->
    <div class="page-header">
      <div class="header-left">
        <h2>评估管理</h2>
        <p class="header-desc">评估数据集质量和模型性能</p>
      </div>
      <el-button type="primary" @click="showEvalDialog = true" class="create-btn">
        <el-icon><Plus /></el-icon>
        新建评估
      </el-button>
    </div>

    <!-- Tabs -->
    <el-tabs v-model="activeTab" class="eval-tabs">
      <el-tab-pane label="评估数据集" name="eval">
        <div class="eval-list" v-loading="loading">
          <div v-if="!loading && evalDatasets.length === 0" class="empty-state">
            <el-icon size="48"><DataAnalysis /></el-icon>
            <h3>暂无评估数据集</h3>
            <p>创建您的第一个评估数据集</p>
            <el-button type="primary" @click="showEvalDialog = true">新建评估</el-button>
          </div>

          <div v-else class="evals-grid">
            <div v-for="(evalSet, index) in evalDatasets" :key="evalSet.id" class="eval-card" :style="{ '--delay': index * 0.08 + 's' }">
              <div class="eval-header">
                <div class="eval-icon">
                  <el-icon size="24"><DataLine /></el-icon>
                </div>
                <div class="eval-actions">
                  <el-button type="primary" size="small" @click="runEval(evalSet)">运行</el-button>
                  <el-button size="small" @click="viewResults(evalSet)">结果</el-button>
                </div>
              </div>
              <h3 class="eval-name">{{ evalSet.name }}</h3>
              <div class="eval-meta">
                <el-tag size="small" effect="dark">{{ getTypeName(evalSet.question_type) }}</el-tag>
                <span class="meta-count">{{ evalSet.question_count || 0 }} 题</span>
              </div>
              <span class="eval-date">{{ formatDate(evalSet.created_at) }}</span>
            </div>
          </div>
        </div>
      </el-tab-pane>

      <el-tab-pane label="盲测系统" name="blind-test">
        <div class="blind-test" v-loading="loading">
          <div v-if="!loading && blindTasks.length === 0" class="empty-state">
            <el-icon size="48"><Aim /></el-icon>
            <h3>暂无盲测任务</h3>
            <p>创建盲测来对比模型表现</p>
            <el-button type="primary" @click="showBlindTestDialog = true">创建测试</el-button>
          </div>

          <div v-else class="tasks-grid">
            <div v-for="(task, index) in blindTasks" :key="task.id" class="task-card" :style="{ '--delay': index * 0.08 + 's' }">
              <div class="task-header">
                <div class="task-icon" :class="task.status">
                  <el-icon size="20"><component :is="getTaskIcon(task.status)" /></el-icon>
                </div>
                <el-tag :type="getStatusType(task.status)" size="small" effect="dark">{{ getStatusText(task.status) }}</el-tag>
              </div>
              <h3 class="task-name">{{ task.name }}</h3>
              <el-progress :percentage="task.progress || 0" :stroke-width="6" />
              <div class="task-meta">已完成: {{ task.completed || 0 }}/{{ task.total || 0 }}</div>
              <div class="task-actions">
                <el-button v-if="task.status === 'running'" type="danger" size="small" @click="stopTask(task)">停止</el-button>
                <el-button type="primary" size="small" @click="continueTask(task)">{{ task.status === 'running' ? '查看' : '继续' }}</el-button>
              </div>
            </div>
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>

    <!-- Eval Dialog -->
    <el-dialog v-model="showEvalDialog" title="新建评估数据集" width="520px" class="eval-dialog">
      <el-form :model="evalForm" label-position="top">
        <el-form-item label="数据集名称">
          <el-input v-model="evalForm.name" placeholder="输入名称" size="large" />
        </el-form-item>
        <div class="form-row">
          <el-form-item label="问题类型">
            <el-select v-model="evalForm.question_type" placeholder="选择类型" size="large" style="width: 100%">
              <el-option label="混合" value="mixed" />
              <el-option label="事实性" value="fact" />
              <el-option label="推理" value="reasoning" />
            </el-select>
          </el-form-item>
          <el-form-item label="问题数量">
            <el-input-number v-model="evalForm.count" :min="10" :max="500" size="large" style="width: 100%" />
          </el-form-item>
        </div>
      </el-form>
      <template #footer>
        <el-button @click="showEvalDialog = false">取消</el-button>
        <el-button type="primary" @click="createEval">创建</el-button>
      </template>
    </el-dialog>

    <!-- Blind Test Dialog -->
    <el-dialog v-model="showBlindTestDialog" title="创建盲测任务" width="520px" class="blind-dialog">
      <el-form :model="blindForm" label-position="top">
        <el-form-item label="任务名称">
          <el-input v-model="blindForm.name" placeholder="输入任务名称" size="large" />
        </el-form-item>
        <el-form-item label="选择模型">
          <el-checkbox-group v-model="blindForm.model_ids" class="model-check-group">
            <el-checkbox-button label="gpt-4">GPT-4</el-checkbox-button>
            <el-checkbox-button label="claude-3">Claude-3</el-checkbox-button>
            <el-checkbox-button label="gemini-pro">Gemini Pro</el-checkbox-button>
            <el-checkbox-button label="llama-3">Llama 3</el-checkbox-button>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showBlindTestDialog = false">取消</el-button>
        <el-button type="primary" @click="createBlindTest">创建</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, computed } from 'vue'
import { useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { evalApi } from '@/api'

const route = useRoute()
const projectId = computed(() => route.params.id)

const loading = ref(false)
const activeTab = ref('eval')
const evalDatasets = ref([])
const blindTasks = ref([])
const showEvalDialog = ref(false)
const showBlindTestDialog = ref(false)

const evalForm = reactive({
  name: '',
  question_type: 'mixed',
  count: 50
})

const blindForm = reactive({
  name: '',
  model_ids: []
})

const fetchEvalDatasets = async () => {
  loading.value = true
  try {
    const res = await evalApi.list(projectId.value)
    evalDatasets.value = res.data.datasets || []
  } catch (error) {
    evalDatasets.value = []
  } finally {
    loading.value = false
  }
}

const createEval = async () => {
  try {
    await evalApi.create(projectId.value, evalForm)
    ElMessage.success('创建成功')
    showEvalDialog.value = false
    fetchEvalDatasets()
  } catch (error) {
    ElMessage.error('创建失败')
  }
}

const runEval = async (evalSet) => {
  try {
    await evalApi.run(projectId.value, evalSet.id)
    ElMessage.success('评估已启动')
  } catch (error) {
    ElMessage.error('启动失败')
  }
}

const viewResults = (evalSet) => {
  ElMessage.info('结果查看功能开发中')
}

const createBlindTest = () => {
  ElMessage.info('盲测功能开发中')
  showBlindTestDialog.value = false
}

const stopTask = (task) => ElMessage.info('停止功能开发中')
const continueTask = (task) => ElMessage.info('继续功能开发中')

const getTypeName = (type) => {
  const map = { mixed: '混合', fact: '事实性', reasoning: '推理' }
  return map[type] || type
}

const getStatusType = (status) => {
  const map = { pending: 'info', running: 'warning', completed: 'success', failed: 'danger' }
  return map[status] || 'info'
}

const getStatusText = (status) => {
  const map = { pending: '等待中', running: '运行中', completed: '已完成', failed: '失败' }
  return map[status] || '未知'
}

const getTaskIcon = (status) => {
  const map = { pending: 'Clock', running: 'Loading', completed: 'CircleCheck', failed: 'CircleClose' }
  return map[status] || 'Clock'
}

const formatDate = (date) => {
  if (!date) return ''
  const d = new Date(date)
  return `${d.getMonth() + 1}/${d.getDate()}`
}

onMounted(() => fetchEvalDatasets())
</script>

<style scoped>
.eval-manage {
  padding: 32px;
  max-width: 1200px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 28px;
}

.header-left h2 {
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 4px;
}

.header-desc {
  font-size: 14px;
  color: var(--text-tertiary);
}

.create-btn {
  padding: 10px 20px;
  font-weight: 500;
  border-radius: var(--radius-md);
}

.eval-tabs {
  background: var(--bg-secondary);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: 20px;
}

.evals-grid, .tasks-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 16px;
}

.eval-card, .task-card {
  background: var(--bg-tertiary);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: 20px;
  transition: all var(--transition-base);
  animation: cardIn 0.4s ease backwards;
  animation-delay: var(--delay);
}

@keyframes cardIn {
  from { opacity: 0; transform: translateY(15px); }
}

.eval-card:hover, .task-card:hover {
  border-color: var(--accent-primary);
  transform: translateY(-3px);
}

.eval-header, .task-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 16px;
}

.eval-icon {
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--accent-primary-muted);
  border-radius: var(--radius-md);
  color: var(--accent-primary);
}

.task-icon {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-md);
  background: var(--bg-secondary);
}

.task-icon.running {
  background: rgba(251, 191, 36, 0.15);
  color: var(--warning);
  animation: pulse 2s infinite;
}

.task-icon.completed {
  background: var(--success-muted);
  color: var(--success);
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.7; }
}

.eval-actions {
  display: flex;
  gap: 8px;
}

.eval-name, .task-name {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 12px;
}

.eval-meta {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 13px;
  color: var(--text-tertiary);
}

.meta-count {
  color: var(--text-muted);
}

.eval-date {
  display: block;
  margin-top: 12px;
  font-size: 12px;
  color: var(--text-muted);
}

.task-meta {
  font-size: 13px;
  color: var(--text-secondary);
  margin: 12px 0;
}

.task-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
}

.empty-state .el-icon {
  color: var(--text-muted);
  margin-bottom: 16px;
}

.empty-state h3 {
  font-size: 18px;
  margin-bottom: 8px;
}

.empty-state p {
  color: var(--text-tertiary);
  margin-bottom: 20px;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

.model-check-group {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
</style>
