<template>
  <n-card
    class="course-card"
    hoverable
    @click="$router.push(`/course/${course.id}`)"
  >
    <!-- 课程缩略图 -->
    <div class="course-thumbnail">
      <img :src="course.thumbnail" :alt="course.title" />
      <div class="course-level">
        <n-tag :type="levelType" size="small">
          {{ levelText }}
        </n-tag>
      </div>
    </div>

    <!-- 课程信息 -->
    <div class="course-info">
      <h3 class="course-title">{{ course.title }}</h3>
      <p class="course-description">{{ course.description }}</p>
      
      <!-- 讲师信息 -->
      <div class="instructor">
        <SafeAvatar
          :src="course.instructor?.avatar"
          :name="course.instructor?.name"
          :size="32"
        />
        <span class="instructor-name">{{ course.instructor?.name }}</span>
      </div>

      <!-- 课程统计 -->
      <div class="course-stats">
        <div class="stat-item">
          <n-icon>
            <StarOutline />
          </n-icon>
          <span>{{ course.rating }}</span>
        </div>
        <div class="stat-item">
          <n-icon>
            <PeopleOutline />
          </n-icon>
          <span>{{ formatNumber(course.studentsCount) }}</span>
        </div>
        <div class="stat-item">
          <n-icon>
            <TimeOutline />
          </n-icon>
          <span>{{ course.duration }}</span>
        </div>
      </div>

      <!-- 价格和操作 -->
      <div class="course-footer">
        <div class="price">
          <span class="current-price">¥{{ course.price }}</span>
          <span v-if="course.originalPrice" class="original-price">
            ¥{{ course.originalPrice }}
          </span>
        </div>
        <n-button
          v-if="!course.isEnrolled"
          type="primary"
          size="small"
          @click.stop="handleEnroll"
        >
          立即报名
        </n-button>
        <n-button
          v-else
          type="success"
          size="small"
          @click.stop="$router.push(`/learning/${course.id}`)"
        >
          继续学习
        </n-button>
      </div>

      <!-- 学习进度 -->
      <div v-if="course.isEnrolled && course.progress !== undefined" class="progress">
        <n-progress
          type="line"
          :percentage="course.progress"
          :show-indicator="false"
          :height="4"
        />
        <span class="progress-text">进度: {{ course.progress }}%</span>
      </div>
    </div>
  </n-card>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useMessage } from 'naive-ui'
import { useCourseStore } from '@/stores/course'
import type { Course } from '@/stores/course'
import SafeAvatar from '@/components/common/SafeAvatar.vue'
import {
  StarOutline,
  PeopleOutline,
  TimeOutline
} from '@vicons/ionicons5'

interface Props {
  course: Course
}

const props = defineProps<Props>()
const message = useMessage()
const courseStore = useCourseStore()

// 课程等级类型和文本
const levelType = computed(() => {
  switch (props.course.level) {
    case 'beginner':
      return 'success'
    case 'intermediate':
      return 'warning'
    case 'advanced':
      return 'error'
    default:
      return 'default'
  }
})

const levelText = computed(() => {
  switch (props.course.level) {
    case 'beginner':
      return '初级'
    case 'intermediate':
      return '中级'
    case 'advanced':
      return '高级'
    default:
      return '未知'
  }
})

// 格式化数字
const formatNumber = (num: number): string => {
  if (num >= 1000) {
    return (num / 1000).toFixed(1) + 'k'
  }
  return num.toString()
}

// 处理报名
const handleEnroll = async () => {
  const result = await courseStore.enrollCourse(props.course.id)
  if (result.success) {
    message.success(result.message)
  } else {
    message.error(result.message)
  }
}
</script>

<style scoped>
.course-card {
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.course-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}

.course-thumbnail {
  position: relative;
  width: 100%;
  height: 200px;
  overflow: hidden;
  border-radius: 8px;
  margin-bottom: 16px;
}

.course-thumbnail img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.course-level {
  position: absolute;
  top: 12px;
  right: 12px;
}

.course-info {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.course-title {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 8px;
  color: #333;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.course-description {
  color: #666;
  font-size: 14px;
  line-height: 1.5;
  margin-bottom: 16px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.instructor {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
}

.instructor-name {
  font-size: 14px;
  color: #666;
}

.course-stats {
  display: flex;
  gap: 16px;
  margin-bottom: 16px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 14px;
  color: #666;
}

.course-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: auto;
}

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

.current-price {
  font-size: 18px;
  font-weight: 600;
  color: #18a058;
}

.original-price {
  font-size: 14px;
  color: #999;
  text-decoration: line-through;
}

.progress {
  margin-top: 12px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.progress-text {
  font-size: 12px;
  color: #666;
  white-space: nowrap;
}
</style>
