<template>
	<view class="course-detail-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 class="points-info" @click="goToPointsDetail">
					<text class="points-icon">💎</text>
					<text class="points-value">{{ currentPoints }}</text>
				</view>
			</view>
		</view>

		<!-- 课程封面 -->
		<view class="course-cover">
			<image :src="course.cover_image" class="cover-image" mode="aspectFill"></image>
			<view class="cover-mask">
				<view class="points-tag">💎 {{ course.points_price || course.price * 10 }}积分</view>
			</view>
		</view>

		<!-- 课程信息 -->
		<view class="course-content">
			<view class="course-title">{{ course.name }}</view>

			<view class="teacher-info">
				<image :src="course.teacher_avatar || DEFAULT_IMAGES.avatar" class="teacher-avatar" mode="aspectFill"></image>
				<view class="teacher-detail">
					<text class="teacher-name">{{ course.teacher_name }}</text>
					<text class="teacher-desc">{{ course.teacher_desc || '资深情感导师' }}</text>
				</view>
			</view>

			<view class="course-stats">
				<view class="stat-item">
					<text class="stat-icon">👥</text>
					<text class="stat-text">{{ course.student_count || 0 }}人学习</text>
				</view>
				<view class="stat-item">
					<text class="stat-icon">⭐</text>
					<text class="stat-text">{{ course.rating || '5.0' }}分</text>
				</view>
				<view class="stat-item">
					<text class="stat-icon">📚</text>
					<text class="stat-text">{{ course.chapter_count || 0 }}个章节</text>
				</view>
			</view>

			<view class="divider"></view>

			<view class="description-section">
				<view class="section-title">课程介绍</view>
				<view class="description-text">{{ course.description || '暂无介绍' }}</view>
			</view>

			<view class="divider"></view>

			<view class="outline-section">
				<view class="section-title">课程大纲</view>
				<view class="outline-text">{{ course.outline || '敬请期待' }}</view>
			</view>
		</view>

		<!-- 底部兑换按钮 -->
		<view class="bottom-bar">
			<view class="price-info">
				<text class="price-label">兑换所需</text>
				<view class="price-value">
					<text class="points-icon">💎</text>
					<text class="price-num">{{ course.points_price || course.price * 10 }}</text>
					<text class="price-unit">积分</text>
				</view>
			</view>
			<view class="purchase-btn" :class="{ purchased: isPurchased }" @click="handleExchange">
				<text class="btn-text">{{ isPurchased ? '立即学习' : '积分兑换' }}</text>
			</view>
		</view>
	</view>
</template>

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

	export default {
		data() {
			return {
				courseId: null,
				course: {},
				DEFAULT_IMAGES,
				currentPoints: 0,
				makerId: null,
				isPurchased: false
			}
		},

		onLoad(options) {
		
			
			if (options.id) {
				this.courseId = options.id
				this.loadMakerInfo()
			}
		},

		onShow() {
			this.loadCurrentPoints()
		},

		methods: {
			// 加载红娘信息
			async loadMakerInfo() {
				try {
					const userInfo = uni.getStorageSync('userInfo')
					if (userInfo && userInfo.userId) {
						const matchmakerInfo = await api.matchmaker.getByUserId(userInfo.userId)
						if (matchmakerInfo) {
							this.makerId = matchmakerInfo.matchmakerId || matchmakerInfo.matchmaker_id
							this.loadCurrentPoints()
							this.loadCourseDetail()
							this.checkPurchaseStatus()
						}
					}
				} catch (error) {
					
					this.loadCourseDetail()
				}
			},

			// 加载当前积分
			async loadCurrentPoints() {
				if (!this.makerId) return
				try {
					const res = await api.pointsMall.getBalance(this.makerId)
					this.currentPoints = res || 0
				} catch (error) {
				
				}
			},

			// 加载课程详情
			async loadCourseDetail() {
				try {
					// 使用红娘课程独立API
					const data = await api.matchmakerCourse.getDetail(this.courseId)
					if (data) {
						this.course = {
							...data,
							points_price: data.points || (data.price ? data.price * 10 : 100),
							cover_image: data.cover_image || data.coverImage || this.DEFAULT_IMAGES.course,
							teacher_name: data.teacher_name || data.instructor || '专业导师'
						}
					} else {
						this.setMockCourse()
					}
				} catch (error) {
					
					this.setMockCourse()
				}
			},

			// 设置模拟数据
			setMockCourse() {
				this.course = {
					id: this.courseId,
					name: '红娘专业培训课程',
					teacher_name: '资深导师',
					teacher_desc: '10年情感咨询经验',
					rating: 4.9,
					student_count: 128,
					chapter_count: 12,
					points_price: 1990,
					description: '本课程专为红娘设计，涵盖相亲流程、沟通技巧、客户心理分析等核心内容，帮助您成为专业的婚恋顾问。',
					outline: '第一章：红娘职业概述\n第二章：客户需求分析\n第三章：沟通技巧训练\n第四章：相亲活动组织\n第五章：成功案例分享',
					cover_image: this.DEFAULT_IMAGES.course
				}
			},

			// 检查是否已兑换
			async checkPurchaseStatus() {
				if (!this.makerId || !this.courseId) return
				try {
					// 使用红娘课程独立API
					const res = await api.matchmakerCourse.checkExchanged(this.makerId, this.courseId)
					if (res && res.exchanged !== undefined) {
						this.isPurchased = res.exchanged
					}
				} catch (error) {
					
				}
			},

			// 处理兑换
			handleExchange() {
				if (this.isPurchased) {
					uni.showToast({
						title: '学习功能开发中',
						icon: 'none'
					})
					return
				}

				const pointsNeeded = this.course.points_price || this.course.price * 10

				if (this.currentPoints < pointsNeeded) {
					uni.showModal({
						title: '积分不足',
						content: `兑换该课程需要${pointsNeeded}积分，您当前只有${this.currentPoints}积分`,
						showCancel: true,
						cancelText: '取消',
						confirmText: '去赚积分',
						success: (res) => {
							if (res.confirm) {
								uni.navigateTo({
									url: '/pages/matchmaker-workbench/earn-points'
								})
							}
						}
					})
					return
				}

				uni.showModal({
					title: '确认兑换',
					content: `确定使用${pointsNeeded}积分兑换课程"${this.course.name}"吗？`,
					success: async (res) => {
						if (res.confirm) {
							await this.doExchange(pointsNeeded)
						}
					}
				})
			},

			// 执行兑换
			async doExchange(pointsNeeded) {
				uni.showLoading({ title: '兑换中...' })
				try {
					// 使用红娘课程独立API
					const res = await api.matchmakerCourse.exchange({
						makerId: this.makerId,
						courseId: this.courseId,
						points: pointsNeeded
					})

					if (res) {
						uni.hideLoading()
						uni.showToast({
							title: '兑换成功',
							icon: 'success'
						})

						this.isPurchased = true
						this.currentPoints -= pointsNeeded
						this.course.student_count = (this.course.student_count || 0) + 1

						setTimeout(() => {
							uni.showModal({
								title: '兑换成功',
								content: '您已成功兑换此课程，现在可以开始学习了！',
								showCancel: false,
								confirmText: '开始学习',
								success: () => {
									uni.showToast({
										title: '学习功能开发中',
										icon: 'none'
									})
								}
							})
						}, 1500)
					}
				} catch (error) {
					uni.hideLoading()
					
					uni.showToast({
						title: error.msg || '兑换失败',
						icon: 'none'
					})
				}
			},

			// 跳转积分明细
			goToPointsDetail() {
				uni.navigateTo({
					url: '/pages/matchmaker-workbench/points-detail'
				})
			},

			// 返回
			goBack() {
				uni.navigateBack({
					fail: () => {
						uni.navigateTo({
							url: '/pages/matchmaker-workbench/courses'
						})
					}
				})
			}
		}
	}
</script>

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

	/* 自定义导航栏 */
	.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: linear-gradient(135deg, #FCE4EC 0%, #F8BBD0 100%);
		z-index: 999;

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

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

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

		.points-info {
			display: flex;
			align-items: center;
			background: rgba(255, 255, 255, 0.8);
			padding: 8rpx 16rpx;
			border-radius: 30rpx;

			.points-icon {
				font-size: 24rpx;
				margin-right: 6rpx;
			}

			.points-value {
				font-size: 26rpx;
				color: #E91E63;
				font-weight: bold;
			}
		}
	}

	/* 课程封面 */
	.course-cover {
		position: relative;
		width: 100%;
		height: 400rpx;

		.cover-image {
			width: 100%;
			height: 100%;
		}

		.cover-mask {
			position: absolute;
			bottom: 0;
			left: 0;
			right: 0;
			padding: 20rpx;
			background: linear-gradient(transparent, rgba(0, 0, 0, 0.5));

			.points-tag {
				display: inline-block;
				background: linear-gradient(135deg, #E91E63 0%, #9C27B0 100%);
				color: #FFFFFF;
				font-size: 28rpx;
				font-weight: bold;
				padding: 10rpx 24rpx;
				border-radius: 30rpx;
			}
		}
	}

	/* 课程内容 */
	.course-content {
		padding: 30rpx;

		.course-title {
			font-size: 36rpx;
			font-weight: bold;
			color: #333333;
			margin-bottom: 30rpx;
		}

		.teacher-info {
			display: flex;
			align-items: center;
			margin-bottom: 30rpx;

			.teacher-avatar {
				width: 80rpx;
				height: 80rpx;
				border-radius: 50%;
				margin-right: 20rpx;
			}

			.teacher-detail {
				display: flex;
				flex-direction: column;

				.teacher-name {
					font-size: 30rpx;
					font-weight: bold;
					color: #333333;
				}

				.teacher-desc {
					font-size: 24rpx;
					color: #999999;
					margin-top: 6rpx;
				}
			}
		}

		.course-stats {
			display: flex;
			justify-content: space-around;
			padding: 20rpx 0;
			background-color: #FFF5F8;
			border-radius: 16rpx;
			margin-bottom: 30rpx;

			.stat-item {
				display: flex;
				align-items: center;

				.stat-icon {
					font-size: 28rpx;
					margin-right: 8rpx;
				}

				.stat-text {
					font-size: 26rpx;
					color: #666666;
				}
			}
		}

		.divider {
			height: 1rpx;
			background-color: #F0F0F0;
			margin: 30rpx 0;
		}

		.section-title {
			font-size: 32rpx;
			font-weight: bold;
			color: #333333;
			margin-bottom: 20rpx;
		}

		.description-text,
		.outline-text {
			font-size: 28rpx;
			color: #666666;
			line-height: 1.8;
			white-space: pre-wrap;
		}
	}

	/* 底部栏 */
	.bottom-bar {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		height: 120rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 30rpx;
		background-color: #FFFFFF;
		box-shadow: 0 -4rpx 20rpx rgba(0, 0, 0, 0.1);

		.price-info {
			.price-label {
				font-size: 24rpx;
				color: #999999;
			}

			.price-value {
				display: flex;
				align-items: center;
				margin-top: 6rpx;

				.points-icon {
					font-size: 28rpx;
					margin-right: 6rpx;
				}

				.price-num {
					font-size: 40rpx;
					font-weight: bold;
					color: #E91E63;
				}

				.price-unit {
					font-size: 24rpx;
					color: #E91E63;
					margin-left: 6rpx;
				}
			}
		}

		.purchase-btn {
			width: 280rpx;
			height: 80rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			background: linear-gradient(135deg, #E91E63 0%, #9C27B0 100%);
			border-radius: 40rpx;

			&.purchased {
				background: linear-gradient(135deg, #4CAF50 0%, #8BC34A 100%);
			}

			.btn-text {
				font-size: 30rpx;
				font-weight: bold;
				color: #FFFFFF;
			}
		}
	}
</style>
