<script setup>
import { computed, onMounted, ref, watch } from 'vue';
import PortalShell from '@/layouts/PortalShell.vue';
import PageState from '@/components/common/PageState.vue';
import AdminCoreWorkspace from './AdminCoreWorkspace.vue';
import AdminExamWorkspace from './AdminExamWorkspace.vue';
import AdminAdmissionWorkspace from './AdminAdmissionWorkspace.vue';
import AdminSystemWorkspace from './AdminSystemWorkspace.vue';
import { api } from '@/lib/api';
import { sessionStore } from '@/stores/session';

const props = defineProps({ page: { type: String, required: true } });
const loading = ref(true);
const error = ref('');
const data = ref({});
const corePages = new Set(['dashboard','schools','organization','admins','account-batches','candidates','indicator-qualifications','registrations','payments','security']);
const examPages = new Set(['exams','admit','results']);
const admissionPages = new Set(['admission-settings','admission-accounts','admission-plans','admission-reporting','admission-supervision']);
const systemPages = new Set(['notices','centers','flows','flow-design','number-rules']);

const meta = computed(() => ({
  dashboard: ['考务工作台', '掌握当前报名、审核和发布任务。'], schools: ['学校管理', '创建和维护学校档案及公开状态。'],
  organization: ['本校组织与权限', '维护本校班级和班级管理员。'], admins: ['分级管理员', '维护管理员账号和权限范围。'],
  'account-batches': ['批量报名号申领', '按班级提交申领人数并跟踪审批结果。'], candidates: ['考生资料审核', '核验实名、学籍与联系信息。'],
  'indicator-qualifications': ['指标分配资格确认', '由生源校逐人确认指标分配资格。'], registrations: ['考试报名审核', '审核考试、科目和报名状态。'],
  payments: ['缴费名单', '查看、导出并维护线下缴费状态。'], admit: ['准考证编排', '查看或批量编排准考证。'],
  exams: ['考试与科目', '创建考试并配置科目和时间。'], results: ['成绩管理中心', '录入、发布并分析考试成绩。'],
  'admission-settings': ['录取设置', '设置志愿窗口和录取阶段。'], 'admission-accounts': ['招生学校账户', '创建、停用和维护招生学校账户。'],
  'admission-plans': ['招生计划', '审核招生计划并查看完成率。'], 'admission-reporting': ['报到与补录', '审批报到统计和补录决定。'],
  'admission-supervision': ['投档与退档监督', '监督投档记录并审批特殊退档。'], notices: ['通知发布', '维护草稿、发布通知和公开状态。'],
  centers: ['考务场所档案', '管理考点、考场容量和变更申请。'], flows: ['流程中心', '处理、转交或监督审批流程。'],
  'flow-design': ['流程设计', '配置各类业务审批步骤。'], 'number-rules': ['报名号规则', '设计报名号组成和流水规则。'], security: ['账户安全', '修改密码并管理二次验证。']
}[props.page] || [props.page, '管理当前业务数据。']));

function endpoint() {
  if (props.page === 'admit') return 'admission-arrangements';
  if (props.page === 'flows') return 'workflow-instances';
  if (props.page === 'flow-design') return 'workflows';
  if (props.page === 'account-batches') return 'candidate-account-batches';
  if (props.page === 'organization') return 'school-organization';
  if (props.page.startsWith('admission-')) return 'admissions';
  return props.page;
}

async function load() {
  loading.value = true; error.value = '';
  try {
    data.value = props.page === 'security' ? await api('/api/auth/totp') : props.page === 'results'
      ? { exams: sessionStore.state.publicData.exams || [], results: [], message: '请选择考试后加载成绩' }
      : await api(`/api/admin/${endpoint()}`);
  } catch (requestError) { error.value = requestError.message; }
  finally { loading.value = false; }
}

watch(() => props.page, load);
onMounted(load);
</script>

<template>
  <PortalShell role="admin" :page="page" :title="meta[0]" :description="meta[1]">
    <PageState :loading="loading" :error="error" @retry="load">
      <AdminCoreWorkspace v-if="corePages.has(page)" :page="page" :data="data" @reload="load" />
      <AdminExamWorkspace v-else-if="examPages.has(page)" :page="page" :data="data" @reload="load" />
      <AdminAdmissionWorkspace v-else-if="admissionPages.has(page)" :page="page" :data="data" @reload="load" />
      <AdminSystemWorkspace v-else-if="systemPages.has(page)" :page="page" :data="data" @reload="load" />
      <div v-else class="page-state page-state--empty"><strong>页面配置不存在</strong><p>请从左侧导航重新选择业务页面。</p></div>
    </PageState>
  </PortalShell>
</template>
