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

		<!-- 课程分类标签 -->
		<view class="course-tabs">
			<view class="tab-item" :class="{ active: activeTab === 'all' }" @click="switchTab('all')">全部课程</view>
			<view class="tab-item" :class="{ active: activeTab === 'basic' }" @click="switchTab('basic')">基础课程</view>
			<view class="tab-item" :class="{ active: activeTab === 'advanced' }" @click="switchTab('advanced')">进阶课程</view>
			<view class="tab-item" :class="{ active: activeTab === 'premium' }" @click="switchTab('premium')">精品课程</view>
		</view>

		<!-- 排序选项 -->
		<view class="sort-options">
			<view class="sort-item" :class="{ active: activeSort === 'recommend' }" @click="switchSort('recommend')">推荐排序</view>
			<view class="sort-item" :class="{ active: activeSort === 'latest' }" @click="switchSort('latest')">最新课程</view>
			<view class="sort-item" :class="{ active: activeSort === 'popular' }" @click="switchSort('popular')">热门课程</view>
		</view>

		<!-- 课程列表 -->
		<view class="course-grid">
			<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="purchased-tag" v-if="item.isPurchased">已兑换</view>
				<view class="course-info">
					<view class="course-name">{{ item.name }}</view>
					<view class="course-meta">
						<text class="course-teacher">{{ item.teacher_name }}</text>
						<text class="course-rating">⭐ {{ item.rating || 4.5 }}</text>
					</view>
					<view class="course-footer">
						<view class="course-price">
							<text class="points-icon">💎</text>
							<text class="price-value">{{ item.points_price || item.price * 10 }}</text>
							<text class="price-unit">积分</text>
						</view>
						<text class="course-students">{{ item.student_count || 0 }}人学习</text>
					</view>
					<view class="course-btn" :class="{ purchased: item.isPurchased }" @click.stop="handleExchange(item)">
						{{ item.isPurchased ? '立即学习' : '积分兑换' }}
					</view>
				</view>
			</view>
		</view>

		<!-- 加载更多 -->
		<view class="load-more" v-if="hasMore">
			<text class="load-text">{{ loading ? '加载中...' : '上拉加载更多' }}</text>
		</view>
		<view class="no-more" v-else-if="courseList.length > 0">
			<text class="no-more-text">没有更多了</text>
		</view>
		<view class="empty-state" v-else-if="!loading">
			<text class="empty-text">暂无课程</text>
		</view>
	</view>
</template>

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

	export default {
		data() {
			return {
				courseList: [],
				pageNum: 1,
				pageSize: 10,
				total: 0,
				hasMore: true,
				loading: false,
				DEFAULT_IMAGES,
				activeTab: 'all',
				activeSort: 'recommend',
				currentPoints: 0,
				makerId: null,
				purchasedCourseIds: []
			}
		},

		onLoad() {
			console.log('红娘培训课程页面加载')
			this.loadMakerInfo()
		},

		onShow() {
			// 每次显示页面时刷新积分
			this.loadCurrentPoints()
		},

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

		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
							console.log('红娘ID:', this.makerId)
							this.loadCurrentPoints()
							this.loadPurchasedCourses()
							this.loadCourseList()
						}
					}
				} catch (error) {
					console.error('获取红娘信息失败:', error)
					this.loadCourseList()
				}
			},

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

			// 加载已兑换的课程
			async loadPurchasedCourses() {
				if (!this.makerId) return
				try {
					// 调用红娘课程API获取已兑换的课程ID列表
					const res = await api.matchmakerCourse.getPurchasedList(this.makerId)
					if (res && Array.isArray(res)) {
						this.purchasedCourseIds = res.map(item => item.course_id || item.courseId || item.id)
					}
					console.log('已兑换课程ID:', this.purchasedCourseIds)
				} catch (error) {
					console.error('获取已兑换课程失败:', error)
				}
			},

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

				try {
					console.log('加载红娘培训课程列表...', this.activeTab)
					// 使用红娘课程独立API，根据当前tab传入分类类型
					const response = await api.matchmakerCourse.getList({
						categoryType: this.activeTab
					})

					let courseData = []
					let totalCount = 0

					if (response && response.list) {
						courseData = response.list
						totalCount = response.total || 0
					} else if (Array.isArray(response)) {
						courseData = response
						totalCount = response.length
					}

					if (courseData && courseData.length > 0) {
						const processedData = courseData.map(item => ({
							...item,
							cover_image: item.cover_image || item.coverImage || this.DEFAULT_IMAGES.course,
							teacher_name: item.teacher_name || item.instructor || '专业导师',
							student_count: item.student_count || item.participants || 0,
							points_price: item.points || (item.price ? item.price * 10 : 100),
							isPurchased: this.purchasedCourseIds.includes(item.id)
						}))

						if (this.pageNum === 1) {
							this.courseList = processedData
							this.total = totalCount
						} else {
							this.courseList = [...this.courseList, ...processedData]
						}

						this.hasMore = this.courseList.length < totalCount
					} else {
						this.hasMore = false
						if (this.pageNum === 1) {
							this.courseList = this.getMockCourses()
							this.total = this.courseList.length
						}
					}
				} catch (error) {
					console.error('加载课程列表失败:', error)
					if (this.pageNum === 1) {
						this.courseList = this.getMockCourses()
						this.total = this.courseList.length
					}
					this.hasMore = false
				} finally {
					this.loading = false
				}
			},

			// 模拟数据
			getMockCourses() {
				return [
					{
						id: 1,
						name: '非暴力沟通：让问题更...',
						teacher_name: '王老师',
						rating: 4.9,
						points_price: 2990,
						student_count: 54,
						cover_image: 'https://images.unsplash.com/photo-1506744038136-46273834b3fb?ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60',
						isPurchased: false
					},
					{
						id: 2,
						name: '红娘新手入门：相亲场...',
						teacher_name: '张老师',
						rating: 4.5,
						points_price: 890,
						student_count: 33,
						cover_image: 'https://images.unsplash.com/photo-1535713875002-d1d0cf377fde?ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60',
						isPurchased: false
					},
					{
						id: 3,
						name: '从零做红娘：牵线必备...',
						teacher_name: '高老师',
						rating: 4.9,
						points_price: 1990,
						student_count: 45,
						cover_image: 'https://images.unsplash.com/photo-1516321318423-f06f85e504b3?ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60',
						isPurchased: false
					},
					{
						id: 4,
						name: '新手红娘必学：相亲沟...',
						teacher_name: '魏老师',
						rating: 4.4,
						points_price: 990,
						student_count: 54,
						cover_image: 'https://images.unsplash.com/photo-1502672260266-1c1ef2d93688?ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60',
						isPurchased: false
					}
				]
			},

			// 切换标签页
			switchTab(tab) {
				this.activeTab = tab
				this.pageNum = 1
				this.courseList = []
				this.hasMore = true
				this.loadCourseList()
			},

			// 切换排序
			switchSort(sort) {
				this.activeSort = sort
				if (this.activeSort === 'popular') {
					this.courseList.sort((a, b) => (b.student_count || 0) - (a.student_count || 0))
				} else if (this.activeSort === 'latest') {
					this.courseList.sort((a, b) => b.id - a.id)
				}
			},

			// 处理兑换
			handleExchange(item) {
				if (item.isPurchased) {
					// 已兑换，跳转学习
					this.goToDetail(item.id)
					return
				}

				const pointsNeeded = item.points_price || item.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}积分兑换课程"${item.name}"吗？`,
					success: async (res) => {
						if (res.confirm) {
							await this.doExchange(item, pointsNeeded)
						}
					}
				})
			},

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

					if (res) {
						uni.hideLoading()
						uni.showToast({
							title: '兑换成功',
							icon: 'success'
						})
						
						// 更新状态
						item.isPurchased = true
						this.purchasedCourseIds.push(item.id)
						this.currentPoints -= pointsNeeded
						
						// 跳转到课程详情
						setTimeout(() => {
							this.goToDetail(item.id)
						}, 1500)
					}
				} catch (error) {
					uni.hideLoading()
					console.error('兑换失败:', error)
					uni.showToast({
						title: error.msg || '兑换失败',
						icon: 'none'
					})
				}
			},

			// 跳转到详情
			goToDetail(id) {
				uni.navigateTo({
					url: `/pages/matchmaker-workbench/course-detail?id=${id}`
				})
			},

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

			// 返回
			goBack() {
				uni.navigateBack({
					fail: () => {
						uni.navigateTo({
							url: '/pages/matchmaker-workbench/mine'
						})
					}
				})
			}
		}
	}
</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: 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-tabs {
		display: flex;
		align-items: center;
		justify-content: space-around;
		padding: 20rpx 0;
		background-color: #FFFFFF;
		border-bottom: 1rpx solid #F0F0F0;

		.tab-item {
			padding: 12rpx 24rpx;
			font-size: 28rpx;
			color: #666666;
			border-radius: 30rpx;
			transition: all 0.3s;

			&.active {
				background-color: #9C27B0;
				color: #FFFFFF;
			}
		}
	}

	/* 排序选项 */
	.sort-options {
		display: flex;
		align-items: center;
		justify-content: space-around;
		padding: 20rpx 0;
		background-color: #FFFFFF;
		border-bottom: 1rpx solid #F0F0F0;

		.sort-item {
			font-size: 26rpx;
			color: #666666;
			transition: all 0.3s;

			&.active {
				color: #9C27B0;
				font-weight: bold;
			}
		}
	}

	/* 课程列表 - 两列布局 */
	.course-grid {
		display: grid;
		grid-template-columns: 1fr 1fr;
		gap: 20rpx;
		padding: 20rpx;

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

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

			.purchased-tag {
				position: absolute;
				top: 16rpx;
				right: 16rpx;
				background: linear-gradient(135deg, #4CAF50 0%, #8BC34A 100%);
				color: #FFFFFF;
				font-size: 22rpx;
				padding: 6rpx 16rpx;
				border-radius: 20rpx;
			}

			.course-info {
				padding: 20rpx;

				.course-name {
					font-size: 28rpx;
					font-weight: bold;
					color: #333333;
					margin-bottom: 10rpx;
					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: #666666;
					margin-bottom: 10rpx;
				}

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

					.course-price {
						display: flex;
						align-items: center;
						color: #E91E63;
						font-weight: bold;

						.points-icon {
							font-size: 22rpx;
							margin-right: 4rpx;
						}

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

						.price-unit {
							font-size: 22rpx;
							margin-left: 4rpx;
						}
					}

					.course-students {
						font-size: 22rpx;
						color: #999999;
					}
				}

				.course-btn {
					width: 100%;
					height: 60rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					background-color: #9C27B0;
					color: #FFFFFF;
					border-radius: 30rpx;
					font-size: 26rpx;

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

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

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

	.empty-state {
		padding: 100rpx 0;
		text-align: center;

		.empty-text {
			font-size: 28rpx;
			color: #999999;
		}
	}
</style>
