<template>
  <div class="package-card" @click="handleClick">
    <!-- 封面区域 -->
    <div class="cover-wrapper">
      <img
        v-if="pkg.coverImagePath"
        :src="getImageUrl(pkg.coverImagePath)"
        class="cover-image"
        alt="课程包封面"
      />
      <div v-else class="cover-placeholder">
        <BookFilled class="placeholder-icon" />
        <span class="placeholder-text">精彩绘本</span>
      </div>
    </div>

    <!-- 内容区域 -->
    <div class="content-wrapper">
      <h3 class="package-name" :title="pkg.name">{{ pkg.name }}</h3>
      <p v-if="pkg.pictureBookName" class="book-name">
        <BookOutlined /> {{ pkg.pictureBookName }}
      </p>

      <!-- 年级标签行 -->
      <div class="tag-row grade-row">
        <span class="grade-tag">
          {{ gradeText }}
        </span>
      </div>

      <!-- 课程配置标签行（参考管理端） -->
      <div v-if="lessonTypes.length > 0" class="tag-row config-row">
        <span
          v-for="lt in lessonTypes"
          :key="lt"
          class="config-tag"
          :style="getLessonTagStyle(lt)"
        >
          {{ getLessonTypeName(lt) }}
        </span>
      </div>

      <!-- 统计信息 -->
      <div class="meta-row">
        <span class="meta-item">
          <ClockCircleOutlined />
          {{ pkg.durationMinutes || 30 }}分钟
        </span>
        <span class="meta-item">
          <TeamOutlined />
          {{ pkg.usageCount || 0 }}次
        </span>
      </div>

      <!-- 操作按钮（学校端只查看详情） -->
      <button class="action-btn" @click.stop="handleView">
        <EyeOutlined />
        <span>查看详情</span>
      </button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue';
import {
  BookOutlined,
  BookFilled,
  ClockCircleOutlined,
  TeamOutlined,
  EyeOutlined,
} from '@ant-design/icons-vue';
import type { CoursePackage } from '@/api/course-center';
import { getLessonTypeName, getLessonTagStyle } from '@/utils/tagMaps';

const props = defineProps<{
  pkg: CoursePackage;
}>();

const emit = defineEmits<{
  (e: 'click', pkg: CoursePackage): void;
  (e: 'view', pkg: CoursePackage): void;
}>();

// 年级文本（多年级用圆点分隔）
const gradeText = computed(() => {
  const grades = props.pkg.gradeTags || [];
  return grades.join(' · ');
});

// 从 courses 提取课程类型列表（去重，与管理端一致）
const lessonTypes = computed(() => {
  const courses = props.pkg.courses || [];
  const types = new Set<string>();
  for (const c of courses) {
    const t = c.lessonType;
    if (t) types.add(t);
  }
  return Array.from(types);
});

// 获取图片完整 URL
const getImageUrl = (path: string) => {
  if (!path) return '';
  if (path.startsWith('http')) return path;
  return `${import.meta.env.VITE_SERVER_BASE_URL || 'http://localhost:3000'}${path}`;
};

const handleClick = () => {
  emit('click', props.pkg);
};

const handleView = () => {
  emit('view', props.pkg);
};
</script>

<style scoped>
.package-card {
  background: #fff;
  border-radius: 12px;
  overflow: hidden;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  border: 2px solid transparent;
}

.package-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 20px rgba(24, 144, 255, 0.15);
  border-color: #1890ff;
}

/* 封面区域 */
.cover-wrapper {
  position: relative;
  height: 160px;
  overflow: hidden;
}

.cover-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.package-card:hover .cover-image {
  transform: scale(1.05);
}

.cover-placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #e6f7ff 0%, #f0f5ff 100%);
}

.placeholder-icon {
  font-size: 48px;
  color: #1890ff;
  margin-bottom: 8px;
}

.placeholder-text {
  font-size: 14px;
  color: #1890ff;
  font-weight: 500;
}

/* 内容区域 */
.content-wrapper {
  padding: 12px;
}

.package-name {
  font-size: 15px;
  font-weight: 600;
  color: #333;
  margin: 0 0 4px;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.book-name {
  font-size: 13px;
  color: #888;
  margin: 0 0 8px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: flex;
  align-items: center;
  gap: 4px;
}

/* 标签行 */
.tag-row {
  margin-bottom: 6px;
}

.grade-tag {
  display: inline-block;
  padding: 2px 10px;
  background: linear-gradient(135deg, #e6f7ff 0%, #f0f5ff 100%);
  color: #1890ff;
  font-size: 12px;
  border-radius: 4px;
  border: 1px solid #91d5ff;
}

.config-row {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.config-tag {
  display: inline-block;
  padding: 2px 8px;
  font-size: 11px;
  border-radius: 4px;
}

/* 统计信息 */
.meta-row {
  display: flex;
  gap: 12px;
  padding-top: 8px;
  border-top: 1px dashed #EEE;
  margin-bottom: 10px;
}

.meta-item {
  font-size: 12px;
  color: #999;
  display: flex;
  align-items: center;
  gap: 4px;
}

/* 操作按钮 */
.action-btn {
  width: 100%;
  padding: 8px 16px;
  border: none;
  border-radius: 20px;
  background: linear-gradient(135deg, #1890ff 0%, #40a9ff 100%);
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: all 0.3s ease;
}

.action-btn:hover {
  background: linear-gradient(135deg, #096dd9 0%, #1890ff 100%);
  transform: scale(1.02);
}
</style>
