<template>
  <!-- 团课卡片容器 -->
  <view class="course-card" @click="goDetail">
    <!-- 卡片顶部图片区域 -->
    <view class="card-top">
      <!-- 图片骨架屏 -->
      <view v-if="!imageLoaded" class="skeleton skeleton-image"></view>
      <!-- 课程封面图片 -->
      <image 
        :src="course.coverImage" 
        mode="aspectFill" 
        class="cover-image" 
        :class="{ hidden: !imageLoaded }"
        @load="imageLoaded = true"
        @error="imageLoaded = true"
      />
      <!-- 课程状态标签 -->
      <view :class="['status-tag', statusClass]">
        {{ statusText }}
      </view>
      <!-- 剩余名额标签 -->
      <view class="spots-tag" v-if="course.currentMembers < course.maxMembers">
        <span class="iconfont_courseCard icon-renwu-ren"></span>
        <text>{{ course.maxMembers - course.currentMembers }}个名额</text>
      </view>
    </view>
    
    <!-- 卡片内容区域 -->
    <view class="card-content">
      <!-- 课程名称 -->
      <view class="course-name-wrapper">
        <text class="course-name">{{ course.courseName }}</text>
      </view>
      
      <!-- 课程信息 -->
      <view class="course-info">
        <!-- 上课时间 -->
        <view class="info-item">
          <span class="iconfont_courseCard icon-shijian "></span>
          <text class="info-text">{{ formatTime(course.startTime) }}</text>
        </view>
        <!-- 上课地点 -->
        <view class="info-item">
          <span class="iconfont_courseCard icon-didian"></span>
          <text class="info-text">{{ course.location }}</text>
        </view>
      </view>
      
      <!-- 课程时长 -->
      <view class="course-tags">
        <view class="tag-item duration-tag">
          <uni-icons type="time" size="14" color="#8A99B4" />
          <text>{{ formatDuration(course.startTime, course.endTime) }}</text>
        </view>
        <view 
          v-for="label in courseTypeLabels" 
          :key="label.id" 
          class="tag-item type-tag"
          :style="{ background: `${label.color}15`, color: label.color }"
        >
          <text>{{ label.labelName }}</text>
        </view>
      </view>
    </view>
    
    <!-- 卡片底部操作区域 -->
    <view class="card-footer">
      <!-- 价格信息 -->
      <view class="price-info">
        <!-- 免费课程 -->
        <view v-if="course.storedValueAmount === 0 && course.pointCardAmount === 0" class="price-free">
          <text class="free-text">免费</text>
        </view>
        <!-- 支持多种支付方式 -->
        <view v-else-if="course.storedValueAmount > 0 && course.pointCardAmount > 0" class="price-multi">
          <view class="price-item stored-value">
            <text class="currency">¥</text>
            <text class="amount">{{ course.storedValueAmount }}</text>
            <text class="label">储值卡</text>
          </view>
          <view class="price-divider"></view>
          <view class="price-item point-card">
            <uni-icons type="shop" size="14" color="#FF6B35" />
            <text class="amount">{{ course.pointCardAmount }}次</text>
            <text class="label">次卡</text>
          </view>
        </view>
        <!-- 仅储值卡支付 -->
        <view v-else-if="course.storedValueAmount > 0" class="price-single">
          <text class="price">
            <text class="currency">¥</text>{{ course.storedValueAmount }}
          </text>
          <text class="pay-label">储值卡</text>
        </view>
        <!-- 仅次卡支付 -->
        <view v-else-if="course.pointCardAmount > 0" class="price-single">
          <text class="price points">
            <uni-icons type="shop" size="14" color="#FF6B35" />
            <text>{{ course.pointCardAmount }}次</text>
          </text>
          <text class="pay-label">次卡</text>
        </view>
      </view>
      <!-- 预约按钮 -->
      <view :class="['booking-btn', { disabled: !canBook }]" @click.stop="handleBooking">
        <text>{{ canBook ? '立即预约' : (course.currentMembers >= course.maxMembers ? '已满员' : '已结束') }}</text>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, computed } from 'vue'

// 图片加载状态
const imageLoaded = ref(false)

const props = defineProps({
  course: {
    type: Object,
    required: true
  },
  courseTypeLabels: {
    type: Array,
    default: () => []
  }
})

const emit = defineEmits(['booking', 'detail'])

const statusText = computed(() => {
  const status = props.course.status
  if (status === '0') return '进行中'
  if (status === '1') return '已取消'
  if (status === '2') return '已结束'
  return '未知'
})

const statusClass = computed(() => {
  const status = props.course.status
  if (status === '0') return 'active'
  if (status === '1') return 'canceled'
  if (status === '2') return 'ended'
  return ''
})

const canBook = computed(() => {
  const status = props.course.status
  const isFull = props.course.currentMembers >= props.course.maxMembers
  return status === '0' && !isFull
})

const formatTime = (dateStr) => {
  if (!dateStr) return ''
  const date = new Date(dateStr)
  const month = date.getMonth() + 1
  const day = date.getDate()
  const weekDays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  const weekDay = weekDays[date.getDay()]
  const hours = date.getHours().toString().padStart(2, '0')
  const minutes = date.getMinutes().toString().padStart(2, '0')
  return `${month}月${day}日 ${weekDay} ${hours}:${minutes}`
}

const formatDuration = (startStr, endStr) => {
  if (!startStr || !endStr) return ''
  
  const start = new Date(startStr)
  const end = new Date(endStr)
  
  if (isNaN(start.getTime()) || isNaN(end.getTime())) {
    console.warn(`[CourseCard] 无效的时间格式: startTime=${startStr}, endTime=${endStr}`)
    return ''
  }
  
  const diffMs = end.getTime() - start.getTime()
  if (diffMs <= 0) {
    console.warn(`[CourseCard] 结束时间小于或等于开始时间: startTime=${startStr}, endTime=${endStr}`)
    return ''
  }
  
  const maxDurationMinutes = 24 * 60
  const minutes = Math.floor(diffMs / 60000)
  
  if (minutes > maxDurationMinutes) {
    console.warn(`[CourseCard] 课程时长超过24小时，已修正: ${minutes}分钟 -> ${maxDurationMinutes}分钟`)
    return `${maxDurationMinutes}分钟以上`
  }
  
  return `${minutes}分钟`
}

const goDetail = () => {
  emit('detail', props.course.id)
}

const handleBooking = () => {
  if (canBook.value) {
    emit('booking', props.course)
  }
}
</script>

<style lang="scss" scoped>
	
@import "@/common/style/iconfont_courseCard.css";
	
/* 团课卡片容器 */
.course-card {
  background: #ffffff;
  border-radius: 28rpx;
  overflow: hidden;
  box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.04);
  margin-bottom: 28rpx;
  position: relative;
  z-index: 1;
  transition: all 0.3s ease;
  
  &:active {
    transform: scale(0.98);
    box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.06);
  }
}

/* 卡片顶部图片区域 */
.card-top {
  position: relative;
  height: 320rpx;
  overflow: hidden;
}

/* 课程封面图片 */
.cover-image {
  width: 100%;
  height: 100%;
  transition: opacity 0.3s ease;
  
  &.hidden {
    opacity: 0;
    visibility: hidden;
  }
}

/* 骨架屏基础样式 */
.skeleton {
  position: absolute;
  top: 0;
  left: 0;
  background: linear-gradient(90deg, #f6f7f8 0%, #e0e0e0 20%, #f6f7f8 40%, #f6f7f8 100%);
  background-size: 100% 100%;
  animation: skeleton-loading 1.5s infinite;
  border-radius: inherit;
}

/* 骨架屏动画 */
@keyframes skeleton-loading {
  0% {
    background-position: 100% 0;
  }
  100% {
    background-position: -100% 0;
  }
}

/* 图片骨架屏 */
.skeleton-image {
  width: 100%;
  height: 100%;
}

/* 课程状态标签 */
.status-tag {
  position: absolute;
  top: 24rpx;
  left: 24rpx;
  padding: 10rpx 24rpx;
  border-radius: 24rpx;
  font-size: 22rpx;
  font-weight: 600;
  color: #ffffff;
  backdrop-filter: blur(8rpx);
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.15);
  
  &.active {
    background: linear-gradient(135deg, #10B981 0%, #059669 100%);
  }
  
  &.canceled {
    background: linear-gradient(135deg, #9CA3AF 0%, #6B7280 100%);
  }
  
  &.ended {
    background: linear-gradient(135deg, #D1D5DB 0%, #9CA3AF 100%);
  }
}

/* 剩余名额标签 */
.spots-tag {
  position: absolute;
  top: 24rpx;
  right: 24rpx;
  padding: 10rpx 20rpx;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(8rpx);
  border-radius: 24rpx;
  font-size: 22rpx;
  color: #ffffff;
  display: flex;
  align-items: center;
  gap: 8rpx;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.15);
}

/* 卡片内容区域 */
.card-content {
  padding: 28rpx 32rpx;
}

/* 课程名称容器 */
.course-name-wrapper {
  margin-bottom: 20rpx;
}

/* 课程名称 */
.course-name {
  font-size: 34rpx;
  font-weight: 700;
  color: #1F2937;
  display: block;
  line-height: 1.4;
  letter-spacing: 0.5rpx;
}

/* 课程信息容器 */
.course-info {
  display: flex;
  flex-direction: column;
  gap: 14rpx;
  margin-bottom: 20rpx;
}

/* 信息项 */
.info-item {
  display: flex;
  align-items: center;
  gap: 12rpx;
  padding: 12rpx 16rpx;
  background: linear-gradient(135deg, #F9FAFB 0%, #F3F4F6 100%);
  border-radius: 16rpx;
  transition: all 0.2s ease;
  
  &:active {
    background: linear-gradient(135deg, #F3F4F6 0%, #E5E7EB 100%);
  }
}

/* 信息图标 */
.info-icon {
  display: flex;
  align-items: center;
  flex-shrink: 0;
}

/* 图标字体垂直居中 */
.iconfont_courseCard {
  vertical-align: middle;
}

/* 信息文字 */
.info-text {
  font-size: 26rpx;
  color: #4B5563;
  line-height: 1.5;
  flex: 1;
}

/* 课程标签区域 */
.course-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 12rpx;
}

/* 标签项 */
.tag-item {
  display: inline-flex;
  align-items: center;
  gap: 6rpx;
  padding: 10rpx 20rpx;
  border-radius: 20rpx;
  font-size: 24rpx;
  font-weight: 500;
}

/* 时长标签 */
.duration-tag {
  background: linear-gradient(135deg, #EFF6FF 0%, #DBEAFE 100%);
  color: #3B82F6;
}

/* 类型标签 */
.type-tag {
  font-weight: 600;
}

/* 卡片底部区域 */
.card-footer {
  padding: 24rpx 32rpx 28rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-top: 1rpx solid #F3F4F6;
  margin-top: 4rpx;
}

/* 价格信息 */
.price-info {
  display: flex;
  align-items: center;
  
  .price {
    font-size: 36rpx;
    font-weight: 700;
    color: #FF6B35;
    display: flex;
    align-items: baseline;
    gap: 4rpx;
    
    .currency {
      font-size: 24rpx;
      font-weight: 600;
    }
    
    &.points {
      color: #FF6B35;
      gap: 6rpx;
    }
  }
}

/* 免费课程样式 */
.price-free {
  .free-text {
    font-size: 36rpx;
    font-weight: 700;
    color: #10B981;
  }
}

/* 多种支付方式样式 */
.price-multi {
  display: flex;
  align-items: center;
  gap: 16rpx;
  
  .price-item {
    display: flex;
    align-items: center;
    gap: 6rpx;
    
    .currency {
      font-size: 20rpx;
      font-weight: 600;
      color: #FF6B35;
    }
    
    .amount {
      font-size: 30rpx;
      font-weight: 700;
      color: #FF6B35;
    }
    
    .label {
      font-size: 20rpx;
      color: #6B7280;
      margin-left: 4rpx;
    }
    
    &.stored-value {
      .currency, .amount {
        color: #FF6B35;
      }
    }
    
    &.point-card {
      .amount {
        color: #FF6B35;
      }
    }
  }
  
  .price-divider {
    width: 2rpx;
    height: 32rpx;
    background: #E5E7EB;
  }
}

/* 单一支付方式样式 */
.price-single {
  display: flex;
  align-items: baseline;
  gap: 8rpx;
  
  .pay-label {
    font-size: 20rpx;
    color: #6B7280;
    padding: 4rpx 12rpx;
    background: #F3F4F6;
    border-radius: 8rpx;
  }
}

/* 预约按钮 */
.booking-btn {
  padding: 18rpx 48rpx;
  background: linear-gradient(135deg, #FF6B35 0%, #FF8C5A 100%);
  border-radius: 44rpx;
  font-size: 28rpx;
  font-weight: 600;
  color: #ffffff;
  box-shadow: 0 8rpx 20rpx rgba(255, 107, 53, 0.25);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  letter-spacing: 1rpx;
  
  &:active {
    transform: scale(0.95);
    box-shadow: 0 4rpx 12rpx rgba(255, 107, 53, 0.2);
  }
  
  &.disabled {
    background: linear-gradient(135deg, #F3F4F6 0%, #E5E7EB 100%);
    color: #9CA3AF;
    box-shadow: none;
  }
}
</style>