<template>
  <div class="p-6">
    <a-card :bordered="false" :loading="loading">
      <template #title>
        <span>校本课程包详情</span>
      </template>
      <template #extra>
        <a-space>
          <a-button @click="router.back()">返回</a-button>
          <a-button type="primary" @click="handleEdit">编辑</a-button>
        </a-space>
      </template>

      <a-descriptions :column="2" bordered>
        <a-descriptions-item label="名称">{{ detail?.name }}</a-descriptions-item>
        <a-descriptions-item label="状态">
          <a-tag :color="detail?.status === 'ACTIVE' ? 'success' : 'default'">
            {{ detail?.status === 'ACTIVE' ? '启用' : '禁用' }}
          </a-tag>
        </a-descriptions-item>
        <a-descriptions-item label="源课程包">{{ detail?.sourceCourse?.name }}</a-descriptions-item>
        <a-descriptions-item label="使用次数">{{ detail?.usageCount }}</a-descriptions-item>
        <a-descriptions-item label="创建时间">{{ formatDate(detail?.createdAt) }}</a-descriptions-item>
        <a-descriptions-item label="更新时间">{{ formatDate(detail?.updatedAt) }}</a-descriptions-item>
        <a-descriptions-item label="描述" :span="2">{{ detail?.description || '-' }}</a-descriptions-item>
        <a-descriptions-item label="修改说明" :span="2">{{ detail?.changesSummary || '-' }}</a-descriptions-item>
      </a-descriptions>

      <a-divider>课程列表</a-divider>

      <a-table :columns="lessonColumns" :data-source="detail?.lessons || []" row-key="id" :pagination="false">
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'lessonType'">
            <a-tag>{{ getLessonTypeName(record.lessonType) }}</a-tag>
          </template>
        </template>
      </a-table>
    </a-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { message } from 'ant-design-vue';
import { getTeacherSchoolCourseDetail } from '@/api/school-course';
import type { SchoolCourse } from '@/api/school-course';

const router = useRouter();
const route = useRoute();

const loading = ref(false);
const detail = ref<SchoolCourse | null>(null);

const lessonColumns = [
  { title: '课程类型', dataIndex: 'lessonType', key: 'lessonType', width: 120 },
  { title: '目标', dataIndex: 'objectives', key: 'objectives' },
  { title: '准备', dataIndex: 'preparation', key: 'preparation' },
  { title: '修改备注', dataIndex: 'changeNote', key: 'changeNote' },
];

const lessonTypeNames: Record<string, string> = {
  COLLECTIVE: '集体课',
  HEALTH: '健康',
  LANGUAGE: '语言',
  SOCIAL: '社会',
  SCIENCE: '科学',
  ART: '艺术',
  DOMAIN: '领域课',
};

const getLessonTypeName = (type: string) => lessonTypeNames[type] || type;

const formatDate = (date?: string) => {
  if (!date) return '-';
  return new Date(date).toLocaleString();
};

const fetchData = async () => {
  loading.value = true;
  try {
    const id = Number(route.params.id);
    const res = await getTeacherSchoolCourseDetail(id);
    detail.value = res.data;
  } catch (error) {
    message.error('获取详情失败');
  } finally {
    loading.value = false;
  }
};

const handleEdit = () => {
  router.push(`/teacher/school-courses/${route.params.id}/edit`);
};

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