<template>
	<view class="courses-page">
		<!-- 自定义导航栏 -->
		<view class="custom-navbar">
			<view class="navbar-left" @click="goBack">
				<text class="back-icon">←</text>
			</view>
			<view class="navbar-title">精品课程</view>
			<view class="navbar-right"></view>
		</view>

		<!-- 课程列表 -->
		<view class="course-list">
			<view class="course-card" v-for="(item, index) in courseList" :key="index"
				@click="goToDetail(item.id)">
				<image :src="item.cover_image" class="course-image" mode="aspectFill"></image>
				<view class="course-info">
					<view class="course-name">{{ item.name }}</view>
					<view class="course-desc">{{ item.description }}</view>
					<view class="course-meta">
						<text class="course-teacher">👩‍🏫 {{ item.teacher_name }}</text>
						<text class="course-students">👥 {{ item.student_count || 0 }}人学习</text>
					</view>
					<view class="course-footer">
						<view class="course-price">
							<text class="price-symbol">¥</text>
							<text class="price-value">{{ item.price }}</text>
							<text class="original-price" v-if="item.original_price">¥{{ item.original_price }}</text>
						</view>
						<view class="course-btn">立即购买</view>
					</view>
				</view>
				<view class="discount-tag" v-if="item.discount_rate">{{ item.discount_rate }}折</view>
			</view>
		</view>

		<!-- 加载更多 -->
		<view class="load-more" v-if="hasMore">
			<text class="load-text">{{ loading ? '加载中...' : '上拉加载更多' }}</text>
		</view>
		<view class="no-more" v-else>
			<text class="no-more-text">没有更多了</text>
		</view>
	</view>
</template>

<script>
	import api from '@/utils/api.js'
	import { DEFAULT_IMAGES } from '@/config/index.js'

	export default {
		data() {
			return {
				courseList: [
					// 默认课程数据，确保页面有内容
					{
						id: 1,
						name: '恋爱沟通技巧大师课',
						description: '学会有效沟通，让感情更甜蜜。涵盖倾听技巧、表达方式、冲突化解等核心内容。',
						cover_image: DEFAULT_IMAGES.course,
						teacher_name: '张情感老师',
						teacher_desc: '资深情感咨询师，10年经验',
						teacher_avatar: DEFAULT_IMAGES.avatar,
						price: 299,
						original_price: 399,
						discount_rate: 7.5,
						student_count: 1256,
						rating: 4.9,
						chapter_count: 12,
						outline: '第1章：沟通的艺术\n第2章：倾听的力量\n第3章：表达的技巧\n第4章：冲突的化解\n...'
					},
					{
						id: 2, 
						name: '相亲必胜宝典',
						description: '从形象打造到话题聊天，全方位提升相亲成功率。适合单身男女快速脱单。',
						cover_image: DEFAULT_IMAGES.course,
						teacher_name: '李红娘老师',
						teacher_desc: '专业红娘，成功牵线300+对',
						teacher_avatar: DEFAULT_IMAGES.avatar,
						price: 199,
						original_price: 299,
						discount_rate: 6.6,
						student_count: 2580,
						rating: 4.8,
						chapter_count: 8,
						outline: '第1章：形象管理\n第2章：心态调整\n第3章：聊天技巧\n第4章：约会礼仪\n...'
					},
					{
						id: 3,
						name: '婚姻经营智慧课', 
						description: '婚后生活经营秘籍，让婚姻更加幸福美满。处理婆媳关系、夫妻沟通等实用内容。',
						cover_image: DEFAULT_IMAGES.course,
						teacher_name: '王婚姻老师',
						teacher_desc: '婚姻家庭咨询专家',
						teacher_avatar: DEFAULT_IMAGES.avatar,
						price: 399,
						original_price: 599,
						discount_rate: 6.7,
						student_count: 856,
						rating: 5.0,
						chapter_count: 15,
						outline: '第1章：夫妻沟通\n第2章：婆媳关系\n第3章：财务管理\n第4章：亲子教育\n...'
					}
				],
				pageNum: 1,
				pageSize: 10,
				hasMore: true,
				loading: false,
				DEFAULT_IMAGES
			}
		},

		onLoad() {
			console.log('精品课程列表页面加载')
			console.log('默认课程数据:', this.courseList.length)
			
			// 数据健康检查
			this.checkDataHealth()
			
			this.loadCourseList()
		},

		onReachBottom() {
			if (this.hasMore && !this.loading) {
				this.pageNum++
				this.loadCourseList()
			}
		},

		methods: {
			// 加载课程列表
			async loadCourseList() {
				if (this.loading) return
				this.loading = true

				try {
					console.log('尝试加载精品课程API数据...')
					const data = await api.course.getList({
						pageNum: this.pageNum,
						pageSize: this.pageSize,
						status: 1
					})

					if (data && data.length > 0) {
						if (this.pageNum === 1) {
							// 第一页，替换数据
							this.courseList = data
						} else {
							// 后续页，追加数据
							this.courseList = [...this.courseList, ...data]
						}
						this.hasMore = data.length >= this.pageSize
						console.log('API课程数据加载成功:', data.length, '条')
					} else {
						this.hasMore = false
						console.log('API返回课程数据为空，使用默认数据')
					}
				} catch (error) {
					console.error('加载课程列表失败:', error)
					
					// API失败时，如果是第一页，保留默认数据
					if (this.pageNum === 1) {
						console.log('API失败，保留默认课程数据')
						uni.showToast({
							title: '使用示例数据',
							icon: 'none'
						})
					} else {
						uni.showToast({
							title: '加载失败',
							icon: 'none'
						})
					}
					this.hasMore = false
				} finally {
					this.loading = false
				}
			},

			// 跳转到详情
			goToDetail(id) {
				console.log('跳转到课程详情, id:', id)
				uni.navigateTo({
					url: `/pages/courses/detail?id=${id}`,
					success: () => {
						console.log('跳转课程详情成功')
					},
					fail: (err) => {
						console.error('跳转课程详情失败:', err)
						uni.showToast({
							title: '跳转失败',
							icon: 'none'
						})
					}
				})
			},

			// 返回
			goBack() {
				console.log('课程列表返回上一页')
				uni.navigateBack({
					fail: () => {
						// 返回失败时跳转首页
						uni.navigateTo({
							url: '/pages/index/index'
						})
					}
				})
			},
			
			// 数据健康检查
			checkDataHealth() {
				console.log('=== 精品课程列表页面数据健康检查 ===')
				console.log('courseList长度:', this.courseList.length)
				console.log('DEFAULT_IMAGES:', this.DEFAULT_IMAGES)
				console.log('第一个课程:', this.courseList[0])
				
				// 确保每个课程都有必要的字段
				this.courseList.forEach((item, index) => {
					if (!item.id) {
						item.id = index + 1
					}
					if (!item.cover_image) {
						item.cover_image = this.DEFAULT_IMAGES.course
					}
					if (!item.teacher_avatar) {
						item.teacher_avatar = this.DEFAULT_IMAGES.avatar
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.courses-page {
		min-height: 100vh;
		background-color: #FFF9F9;
		padding-top: 90rpx;
	}

	/* 自定义导航栏 */
	.custom-navbar {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		height: 90rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 20rpx;
		background-color: #E91E63;
		z-index: 999;

		.navbar-left,
		.navbar-right {
			width: 80rpx;
		}

		.back-icon {
			font-size: 40rpx;
			color: #FFFFFF;
			font-weight: bold;
		}

		.navbar-title {
			flex: 1;
			text-align: center;
			font-size: 32rpx;
			font-weight: bold;
			color: #FFFFFF;
		}
	}

	/* 课程列表 */
	.course-list {
		padding: 20rpx 30rpx;

		.course-card {
			position: relative;
			margin-bottom: 20rpx;
			background-color: #FFFFFF;
			border-radius: 20rpx;
			overflow: hidden;
			box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);

			.course-image {
				width: 100%;
				height: 360rpx;
				background-color: #F5F5F5;
			}

			.course-info {
				padding: 30rpx;

				.course-name {
					font-size: 32rpx;
					font-weight: bold;
					color: #333333;
					margin-bottom: 15rpx;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
					overflow: hidden;
				}

				.course-desc {
					font-size: 26rpx;
					color: #666666;
					line-height: 1.6;
					margin-bottom: 20rpx;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
					overflow: hidden;
				}

				.course-meta {
					display: flex;
					justify-content: space-between;
					font-size: 24rpx;
					color: #999999;
					margin-bottom: 20rpx;
				}

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

					.course-price {
						color: #E91E63;
						font-weight: bold;

						.price-symbol {
							font-size: 24rpx;
						}

						.price-value {
							font-size: 36rpx;
						}

						.original-price {
							margin-left: 10rpx;
							font-size: 24rpx;
							color: #999999;
							text-decoration: line-through;
						}
					}

					.course-btn {
						padding: 10rpx 30rpx;
						background-color: #E91E63;
						color: #FFFFFF;
						text-align: center;
						border-radius: 30rpx;
						font-size: 24rpx;
					}
				}
			}

			.discount-tag {
				position: absolute;
				top: 20rpx;
				left: 20rpx;
				padding: 8rpx 20rpx;
				background: linear-gradient(135deg, #FF6B6B 0%, #FF9800 100%);
				color: #FFFFFF;
				font-size: 24rpx;
				font-weight: bold;
				border-radius: 10rpx;
			}
		}
	}

	/* 加载更多 */
	.load-more,
	.no-more {
		padding: 30rpx 0;
		text-align: center;

		.load-text,
		.no-more-text {
			font-size: 24rpx;
			color: #999999;
		}
	}
</style>


