<template>
  <div class="results-detail-page">
    <!-- 顶部导航 -->
    <div class="page-header">
      <div class="header-left">
        <a-button type="text" @click="handleBack">
          <template #icon><ArrowLeftOutlined /></template>
          返回
        </a-button>
        <span class="page-title">{{ contestInfo?.contestName || "成果发布" }}</span>
      </div>
      <div v-if="!isSuperAdmin" class="header-right">
        <a-button
          type="primary"
          :loading="publishLoading"
          @click="handlePublish"
        >
          {{ contestInfo?.resultState === "published" ? "撤回发布" : "发布成果" }}
        </a-button>
      </div>
    </div>

    <!-- 搜索表单 -->
    <a-form
      :model="searchParams"
      layout="inline"
      class="search-form"
      @finish="handleSearch"
    >
      <a-form-item label="作品编号">
        <a-input
          v-model:value="searchParams.workNo"
          placeholder="请输入作品编号"
          allow-clear
          style="width: 180px"
        />
      </a-form-item>
      <a-form-item label="报名账号">
        <a-input
          v-model:value="searchParams.accountNo"
          placeholder="请输入报名账号"
          allow-clear
          style="width: 180px"
        />
      </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 === 'workNo'">
          <a @click="handleViewWorkDetail(record)">{{ record.workNo || "-" }}</a>
        </template>
        <template v-else-if="column.key === 'finalScore'">
          <span v-if="record.judgeScore !== null && record.judgeScore !== undefined" class="score">
            {{ Number(record.judgeScore).toFixed(2) }}
          </span>
          <span v-else-if="record.finalScore !== null" class="score">
            {{ Number(record.finalScore).toFixed(2) }}
          </span>
          <span v-else>-</span>
        </template>
        <template v-else-if="column.key === 'nickname'">
          {{ record.registration?.user?.nickname || "-" }}
        </template>
        <template v-else-if="column.key === 'username'">
          {{ record.registration?.user?.username || "-" }}
        </template>
        <template v-else-if="column.key === 'org'">
          <div>
            <div>{{ record.registration?.user?.tenant?.name || "-" }}</div>
            <div v-if="record.registration?.user?.student?.class" class="org-detail">
              {{ record.registration.user.student.class.grade?.name || "" }}
              {{ record.registration.user.student.class.name || "" }}
            </div>
          </div>
        </template>
        <template v-else-if="column.key === 'teachers'">
          {{ formatTeachers(record.registration?.teachers) }}
        </template>
      </template>
    </a-table>

    <!-- 作品详情弹框 -->
    <WorkDetailModal
      v-model:open="workDetailModalVisible"
      :work-id="currentWorkId"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed, onMounted } from "vue"
import { useRoute, useRouter } from "vue-router"
import { message, Modal } from "ant-design-vue"
import {
  ArrowLeftOutlined,
  SearchOutlined,
  ReloadOutlined,
} from "@ant-design/icons-vue"
import { useAuthStore } from "@/stores/auth"
import { resultsApi } from "@/api/contests"
import WorkDetailModal from "../components/WorkDetailModal.vue"

const route = useRoute()
const router = useRouter()
const authStore = useAuthStore()
const isSuperAdmin = computed(() => authStore.hasAnyRole(['super_admin']))
const tenantCode = route.params.tenantCode as string
const contestId = Number(route.params.id)

// 活动信息
const contestInfo = ref<{
  id: number
  contestName: string
  resultState: string
} | null>(null)

// 作品详情弹框
const workDetailModalVisible = ref(false)
const currentWorkId = ref<number | null>(null)

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

// 搜索参数
const searchParams = reactive({
  workNo: "",
  accountNo: "",
})

// 表格列定义
const columns = [
  {
    title: "序号",
    key: "index",
    width: 70,
  },
  {
    title: "作品编号",
    key: "workNo",
    width: 120,
  },
  {
    title: "评委评分",
    key: "finalScore",
    width: 100,
  },
  {
    title: "姓名",
    key: "nickname",
    width: 120,
  },
  {
    title: "账号",
    key: "username",
    width: 120,
  },
  {
    title: "机构信息",
    key: "org",
    width: 180,
  },
  {
    title: "指导老师",
    key: "teachers",
    width: 150,
  },
]

// 格式化指导老师
const formatTeachers = (teachers: any[] | undefined) => {
  if (!teachers || teachers.length === 0) return "-"
  return teachers
    .map((t) => t.user?.nickname || t.user?.username)
    .filter(Boolean)
    .join("、") || "-"
}

// 获取列表数据
const fetchList = async () => {
  loading.value = true
  try {
    const response = await resultsApi.getResults(contestId, {
      page: pagination.current,
      pageSize: pagination.pageSize,
      workNo: searchParams.workNo || undefined,
      accountNo: searchParams.accountNo || undefined,
    })
    contestInfo.value = response.contest
    dataSource.value = response.list
    pagination.total = response.total
  } catch (error: any) {
    message.error(error?.response?.data?.message || "获取列表失败")
  } finally {
    loading.value = false
  }
}

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

// 重置
const handleReset = () => {
  searchParams.workNo = ""
  searchParams.accountNo = ""
  pagination.current = 1
  fetchList()
}

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

// 返回
const handleBack = () => {
  router.push(`/${tenantCode}/contests/results`)
}

// 查看作品详情
const handleViewWorkDetail = (record: any) => {
  currentWorkId.value = record.id
  workDetailModalVisible.value = true
}

// 发布/撤回成果
const handlePublish = () => {
  const isPublished = contestInfo.value?.resultState === "published"

  Modal.confirm({
    title: isPublished ? "确定撤回成果发布吗？" : "确定发布成果吗？",
    content: isPublished
      ? "撤回后，成果将不再对外公开显示"
      : "发布后，活动结果将公开显示",
    onOk: async () => {
      publishLoading.value = true
      try {
        if (isPublished) {
          await resultsApi.unpublish(contestId)
          message.success("已撤回发布")
        } else {
          await resultsApi.publish(contestId)
          message.success("发布成功")
        }
        fetchList()
      } catch (error: any) {
        message.error(error?.response?.data?.message || "操作失败")
      } finally {
        publishLoading.value = false
      }
    },
  })
}

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

<style scoped lang="scss">
$primary: #6366f1;

.results-detail-page {
  :deep(.ant-btn-primary) {
    background: linear-gradient(135deg, $primary 0%, darken($primary, 10%) 100%);
    border: none;
    box-shadow: 0 4px 12px rgba($primary, 0.35);
    transition: all 0.3s ease;

    &:hover {
      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;
    }
  }
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 24px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  margin-bottom: 16px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 8px;
}

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

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

  :deep(.ant-form-item) {
    margin-bottom: 0;
    margin-right: 0;
  }
}

.org-detail {
  font-size: 12px;
  color: #666;
  margin-top: 2px;
}

.score {
  font-weight: bold;
  color: #52c41a;
}
</style>
