<template>
  <div class="review-progress-page">
    <a-card class="mb-4">
      <template #title>评审进度</template>
    </a-card>

    <!-- Tab栏 -->
    <a-tabs v-model:activeKey="activeTab" class="contest-tabs" @change="handleTabChange">
      <a-tab-pane key="individual" tab="个人赛" />
      <a-tab-pane key="team" tab="团队赛" />
    </a-tabs>

    <!-- 搜索表单 -->
    <a-form :model="searchParams" layout="inline" class="search-form" @finish="handleSearch">
      <a-form-item label="赛事名称">
        <a-input
          v-model:value="searchParams.contestName"
          placeholder="请输入赛事名称"
          allow-clear
          style="width: 200px"
        />
      </a-form-item>
      <a-form-item label="评审状态">
        <a-select
          v-model:value="searchParams.reviewStatus"
          placeholder="请选择评审状态"
          allow-clear
          style="width: 150px"
        >
          <a-select-option value="not_started">未开始</a-select-option>
          <a-select-option value="in_progress">进行中</a-select-option>
          <a-select-option value="completed">已完成</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit">
          <template #icon><SearchOutlined /></template>
          搜索
        </a-button>
        <a-button style="margin-left: 8px" @click="handleReset">
          <template #icon><ReloadOutlined /></template>
          重置
        </a-button>
      </a-form-item>
    </a-form>

    <!-- 数据表格 -->
    <a-table
      :columns="columns"
      :data-source="dataSource"
      :loading="loading"
      :pagination="pagination"
      row-key="id"
      @change="handleTableChange"
    >
      <template #bodyCell="{ column, record, index }">
        <template v-if="column.key === 'index'">
          {{ (pagination.current - 1) * pagination.pageSize + index + 1 }}
        </template>
        <template v-else-if="column.key === 'reviewStatus'">
          <a-tag :color="getReviewStatusColor(record)">
            {{ getReviewStatusText(record) }}
          </a-tag>
        </template>
        <template v-else-if="column.key === 'reviewedCount'">
          {{ record.reviewedCount || 0 }} / {{ record.totalWorksCount || 0 }}
        </template>
        <template v-else-if="column.key === 'reviewTime'">
          <div v-if="record.reviewStartTime || record.reviewEndTime">
            <div>{{ formatDate(record.reviewStartTime) }}</div>
            <div>至 {{ formatDate(record.reviewEndTime) }}</div>
          </div>
          <span v-else>-</span>
        </template>
        <template v-else-if="column.key === 'action'">
          <a-button type="link" size="small" @click="handleViewDetail(record)">
            查看详情
          </a-button>
        </template>
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from "vue"
import { useRoute, useRouter } from "vue-router"
import { message } from "ant-design-vue"
import { SearchOutlined, ReloadOutlined } from "@ant-design/icons-vue"
import dayjs from "dayjs"
import { contestsApi, type Contest } from "@/api/contests"

const route = useRoute()
const router = useRouter()
const tenantCode = route.params.tenantCode as string

// Tab状态
const activeTab = ref<"individual" | "team">("individual")

// 列表状态
const loading = ref(false)
const dataSource = ref<Contest[]>([])
const pagination = reactive({
  current: 1,
  pageSize: 10,
  total: 0,
})

// 搜索参数
const searchParams = reactive({
  contestName: "",
  reviewStatus: undefined as string | undefined,
})

// 表格列定义
const columns = [
  { title: "序号", key: "index", width: 70 },
  { title: "赛事名称", dataIndex: "contestName", key: "contestName", width: 200 },
  { title: "评审状态", key: "reviewStatus", width: 100 },
  { title: "已评审/作品数", key: "reviewedCount", width: 130 },
  { title: "评审时间", key: "reviewTime", width: 180 },
  { title: "操作", key: "action", width: 100, fixed: "right" as const },
]

// 获取评审状态颜色
const getReviewStatusColor = (record: Contest) => {
  const now = new Date()
  const start = record.reviewStartTime ? new Date(record.reviewStartTime) : null
  const end = record.reviewEndTime ? new Date(record.reviewEndTime) : null

  if (!start || now < start) return "default"
  if (end && now > end) return "success"
  return "processing"
}

// 获取评审状态文本
const getReviewStatusText = (record: Contest) => {
  const now = new Date()
  const start = record.reviewStartTime ? new Date(record.reviewStartTime) : null
  const end = record.reviewEndTime ? new Date(record.reviewEndTime) : null

  if (!start || now < start) return "未开始"
  if (end && now > end) return "已完成"
  return "进行中"
}

// 格式化日期
const formatDate = (dateStr?: string) => {
  if (!dateStr) return "-"
  return dayjs(dateStr).format("YYYY-MM-DD HH:mm")
}

// 获取列表数据
const fetchList = async () => {
  loading.value = true
  try {
    const contestType = activeTab.value === "individual" ? "individual" : "team"
    const response = await contestsApi.getList({
      page: pagination.current,
      pageSize: pagination.pageSize,
      contestName: searchParams.contestName || undefined,
      contestType,
    })
    dataSource.value = response.list
    pagination.total = response.total
  } catch (error: any) {
    message.error(error?.response?.data?.message || "获取列表失败")
  } finally {
    loading.value = false
  }
}

// Tab切换
const handleTabChange = () => {
  pagination.current = 1
  fetchList()
}

// 搜索
const handleSearch = () => {
  pagination.current = 1
  fetchList()
}

// 重置
const handleReset = () => {
  searchParams.contestName = ""
  searchParams.reviewStatus = undefined
  pagination.current = 1
  fetchList()
}

// 表格分页变化
const handleTableChange = (pag: any) => {
  pagination.current = pag.current
  pagination.pageSize = pag.pageSize
  fetchList()
}

// 查看详情
const handleViewDetail = (record: Contest) => {
  router.push(`/${tenantCode}/contests/reviews/${record.id}/progress?type=${activeTab.value}`)
}

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

<style scoped lang="scss">
$primary: #1890ff;
$primary-dark: #0958d9;
$gradient-primary: linear-gradient(135deg, $primary 0%, $primary-dark 100%);

.review-progress-page {
  :deep(.ant-card) {
    border: none;
    border-radius: 12px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
    margin-bottom: 16px;

    .ant-card-head {
      border-bottom: none;
      padding: 16px 24px;

      .ant-card-head-title {
        font-size: 18px;
        font-weight: 600;
        color: rgba(0, 0, 0, 0.85);
      }
    }

    .ant-card-body {
      padding: 0;
    }
  }

  :deep(.ant-tabs) {
    background: #fff;
    padding: 0 24px;
    border-radius: 12px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
    margin-bottom: 16px;
  }

  :deep(.ant-btn-primary) {
    background: $gradient-primary;
    border: none;
    box-shadow: 0 4px 12px rgba($primary, 0.35);
    transition: all 0.3s ease;

    &:hover {
      background: linear-gradient(135deg, $primary-dark 0%, darken($primary-dark, 8%) 100%);
      box-shadow: 0 6px 16px rgba($primary, 0.45);
      transform: translateY(-1px);
    }

    &:active {
      transform: translateY(0);
    }
  }

  :deep(.ant-table-wrapper) {
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
    overflow: hidden;

    .ant-table {
      .ant-table-thead > tr > th {
        background: #fafafa;
        font-weight: 600;
        color: rgba(0, 0, 0, 0.85);
        border-bottom: 1px solid #f0f0f0;
      }

      .ant-table-tbody > tr {
        transition: all 0.2s ease;

        &:hover > td {
          background: rgba($primary, 0.04);
        }

        > td {
          border-bottom: 1px solid #f5f5f5;
        }
      }
    }

    .ant-table-pagination {
      padding: 16px;
      margin: 0;
    }
  }
}

.search-form {
  margin-bottom: 16px;
  padding: 20px 24px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);

  :deep(.ant-form-item) {
    margin-bottom: 16px;
    margin-right: 16px;

    &:last-child {
      margin-right: 0;
    }
  }
}
</style>
