<template>
  <div class="school-course-list-page">
    <a-card :bordered="false">
      <template #title>
        <span>我的校本课程包</span>
      </template>
      <template #extra>
        <a-button type="primary" @click="handleCreate">
          <template #icon><PlusOutlined /></template>
          创建校本课程包
        </a-button>
      </template>

      <a-table
        :columns="columns"
        :data-source="dataSource"
        :loading="loading"
        row-key="id"
      >
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'sourceCourse'">
            <div class="source-info">
              <img
                v-if="record.sourceCourse?.coverImagePath"
                :src="record.sourceCourse.coverImagePath"
                class="cover"
              />
              <span>{{ record.sourceCourse?.name }}</span>
            </div>
          </template>
          <template v-else-if="column.key === 'status'">
            <a-tag :color="record.status === 'ACTIVE' ? 'success' : 'default'">
              {{ record.status === 'ACTIVE' ? '启用' : '禁用' }}
            </a-tag>
          </template>
          <template v-else-if="column.key === 'action'">
            <a-space>
              <a-button type="link" size="small" @click="handleView(record)">查看</a-button>
              <a-button type="link" size="small" @click="handleEdit(record)">编辑</a-button>
              <a-popconfirm title="确定要删除吗？" @confirm="handleDelete(record)">
                <a-button type="link" size="small" danger>删除</a-button>
              </a-popconfirm>
            </a-space>
          </template>
        </template>
      </a-table>
    </a-card>
  </div>
</template>

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

const router = useRouter();

const loading = ref(false);
const dataSource = ref<SchoolCourse[]>([]);

const columns = [
  { title: 'ID', dataIndex: 'id', key: 'id', width: 80 },
  { title: '名称', dataIndex: 'name', key: 'name' },
  { title: '源课程包', key: 'sourceCourse' },
  { title: '修改说明', dataIndex: 'changesSummary', key: 'changesSummary' },
  { title: '使用次数', dataIndex: 'usageCount', key: 'usageCount', width: 100 },
  { title: '状态', dataIndex: 'status', key: 'status', width: 80 },
  { title: '操作', key: 'action', width: 180 },
];

const fetchData = async () => {
  loading.value = true;
  try {
    const res = await getTeacherSchoolCourseList() as any;
    dataSource.value = res || [];
  } catch (error) {
    console.error('获取校本课程包列表失败', error);
  } finally {
    loading.value = false;
  }
};

const handleCreate = () => {
  router.push('/teacher/school-courses/create');
};

const handleView = (record: any) => {
  router.push(`/teacher/school-courses/${record.id}`);
};

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

const handleDelete = async (record: any) => {
  try {
    await deleteTeacherSchoolCourse(record.id);
    message.success('删除成功');
    fetchData();
  } catch (error) {
    message.error('删除失败');
  }
};

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

<style scoped>
.school-course-list-page {
  padding: 24px;
}

.source-info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.cover {
  width: 40px;
  height: 40px;
  object-fit: cover;
  border-radius: 4px;
}
</style>
