<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { candidateApi } from '@/api'
import type { Candidate, CandidateListParams } from '@/api'

const router = useRouter()

// 表格数据
const tableData = ref<Candidate[]>([])
const total = ref(0)
const loading = ref(false)

// 查询参数
const queryParams = ref<CandidateListParams>({
  page: 1,
  pageSize: 20,
  keyword: '',
  status: '',
})

// 状态选项
const statusOptions = [
  { label: '全部', value: '' },
  { label: '待面试', value: 'pending' },
  { label: '进行中', value: 'ongoing' },
  { label: '已完成', value: 'completed' },
]

// 获取列表数据
async function fetchData() {
  loading.value = true
  try {
    const response = await candidateApi.getList(queryParams.value)
    tableData.value = response.data.list
    total.value = response.data.total
  } catch (error) {
    console.error('Fetch candidates error:', error)
  } finally {
    loading.value = false
  }
}

// 搜索
function handleSearch() {
  queryParams.value.page = 1
  fetchData()
}

// 重置
function handleReset() {
  queryParams.value = {
    page: 1,
    pageSize: 20,
    keyword: '',
    status: '',
  }
  fetchData()
}

// 分页变化
function handlePageChange(page: number) {
  queryParams.value.page = page
  fetchData()
}

// 查看详情
function handleView(row: Candidate) {
  router.push(`/admin/candidates/${row.sessionId}`)
}

// 格式化状态
function formatStatus(status: string) {
  const statusMap: Record<string, { label: string; type: string }> = {
    pending: { label: '待面试', type: 'info' },
    ongoing: { label: '进行中', type: 'warning' },
    completed: { label: '已完成', type: 'success' },
  }
  return statusMap[status] || { label: status, type: 'info' }
}

// 格式化日期
function formatDate(dateStr: string) {
  const date = new Date(dateStr)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
  })
}

onMounted(() => {
  fetchData()
})
</script>

<template>
  <div class="candidate-list">
    <!-- 页面标题 -->
    <div class="mb-6">
      <h1 class="text-2xl font-bold text-gray-800">候选人管理</h1>
      <p class="text-gray-500 mt-1">查看和管理所有面试候选人</p>
    </div>

    <!-- 搜索栏 -->
    <div class="bg-white rounded-lg p-4 mb-6 shadow-sm">
      <el-form :inline="true" :model="queryParams" class="flex flex-wrap gap-4">
        <el-form-item label="姓名">
          <el-input
            v-model="queryParams.keyword"
            placeholder="请输入姓名"
            clearable
            style="width: 200px"
            @keyup.enter="handleSearch"
          />
        </el-form-item>
        <el-form-item label="状态">
          <el-select
            v-model="queryParams.status"
            placeholder="请选择状态"
            style="width: 120px"
          >
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon class="mr-1"><Search /></el-icon>
            搜索
          </el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 数据表格 -->
    <div class="bg-white rounded-lg shadow-sm">
      <el-table
        v-loading="loading"
        :data="tableData"
        stripe
        style="width: 100%"
      >
        <el-table-column prop="name" label="姓名" width="120" />
        <el-table-column prop="createdAt" label="面试时间" width="180">
          <template #default="{ row }">
            {{ formatDate(row.createdAt) }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="formatStatus(row.status).type as any" size="small">
              {{ formatStatus(row.status).label }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="score" label="综合评分" width="120">
          <template #default="{ row }">
            <span v-if="row.score" class="font-medium">
              {{ row.score }} 分
            </span>
            <span v-else class="text-gray-400">-</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleView(row)">
              查看详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="flex justify-end p-4">
        <el-pagination
          v-model:current-page="queryParams.page"
          :page-size="queryParams.pageSize"
          :total="total"
          layout="total, prev, pager, next"
          @current-change="handlePageChange"
        />
      </div>
    </div>
  </div>
</template>

<style scoped>
</style>
