<template>
	<view class="container">
		<!-- 顶部导航 Tab -->
		<view class="top-nav">
			    <view class="search-cont">
			    	<view class="inner">
			    		<image class="icon-search" src="https://bjzmky-1323137866.cos.ap-chongqing.myqcloud.com/class/search.png" mode=""></image>
			    		<input type="text" v-model="keyword" @input="clearInput" placeholder="请输入关键字搜索内容" placeholder-style="font-size:28rpx;color:#BBBBBB;"/>
						 <view class="close-circle" v-if="showClearIcon"  @click="clearIcon">
							 <u-icon name="close-circle" color="#999" size="22" ></u-icon>
						 </view>
					</view>
					<view class="sousuo" @click="goSearch">搜索</view>
			    </view>
				<!-- <view class="nav-row">
					<view class="nav-all" :class="{ active: activeId === 'all'}" @tap.stop="onSelectAll" @click.stop="onSelectAll">
						<text>全部</text>
					</view>
					<scroll-view scroll-x class="nav-scroll" :show-scrollbar="false">
					<view class="nav-inner">
						<view
							v-for="(item, index) in navList"
							:key="index"
							:class="['nav-item', { active: item.id === activeId }]"
							@tap.stop="onSelectByIndex(index)"
							@click.stop="onSelectByIndex(index)"
						>
							<text>{{ item.categoryName}}</text>
						</view>
					</view>
					</scroll-view>
				</view> -->
		</view>
		<!-- 最近观看 内容 -->
		<scroll-view
			scroll-y
			class="scroll-wrap"
			:show-scrollbar="false"
			lower-threshold="120"
			@scrolltolower="onCourseScrollToLower"
		>
			<!-- 搜索结果 -->
			<template v-if="isSearch">
				<view class="course-skeleton" v-if="courseSectionLoading && courseList.length === 0">
					<view class="sk-course-card" v-for="i in 3" :key="'sk-search-' + i">
						<view class="sk-thumb"></view>
						<view class="sk-info">
							<view class="sk-line sk-line-lg"></view>
							<view class="sk-line sk-line-md"></view>
							<view class="sk-foot">
								<view class="sk-line sk-line-sm"></view>
								<view class="sk-btn"></view>
							</view>
						</view>
					</view>
				</view>
				<view class="course-grid-wrap" v-else-if="courseList.length > 0">
					<view class="course-grid" :class="{ 'list-dimmed': courseRefreshing }">
						<view
							v-for="(course, idx) in courseList"
							:key="course.courseId"
							class="course-card"
							@click="onCourseClick(course)"
						>
							<view class="card-cover">
								<image v-if="course.cover" :src="course.cover" mode="aspectFill" class="cover-img"></image>
							</view>
							<view class="course-info">
								<text class="card-title">{{ course.title }}</text>
								<view class="x-end" style="justify-content: space-between;">
									<view class="course-meta">
										<image src="https://bjzmky-1323137866.cos.ap-chongqing.myqcloud.com/class/renshu.png"></image>
										<text class="meta-count">{{ courseViewsDisplay(course.views)}}</text>
									</view>
									<view class="btn-watch" @click.stop="onCourseClick(course)">立即观看</view>
								</view>
							</view>
						</view>
					</view>
					<view class="refresh-mask" v-if="courseRefreshing">
						<view class="refresh-spinner"></view>
					</view>
				</view>
				<view v-if="courseList.length > 0" class="load-tip">
					<text v-if="courseLoading && !courseRefreshing">加载中...</text>
					<text v-else-if="!courseHasMore">没有更多了</text>
				</view>
			</template>
         
			<!-- 大家都在观看 -->
			<template v-else>
				<view class="recommend-skeleton-wrap" v-if="recommendLoading && recommendList.length === 0">
					<text class="recommend-title">大家都在观看</text>
					<view class="course-skeleton" style="padding: 0;">
						<view class="sk-course-card" v-for="i in 3" :key="'sk-rec-' + i">
							<view class="sk-thumb"></view>
							<view class="sk-info">
								<view class="sk-line sk-line-lg"></view>
								<view class="sk-line sk-line-md"></view>
								<view class="sk-foot">
									<view class="sk-line sk-line-sm"></view>
									<view class="sk-btn"></view>
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class="recommend-section" v-else>
					<text class="recommend-title">大家都在观看</text>
					<view class="course-grid" style="padding: 0;">
						<view
							v-for="(course, idx) in recommendList"
							:key="course.courseId"
							class="course-card"
							@click="onCourseClick(course)"
						>
							<view class="card-cover">
								<image v-if="course.cover" :src="course.cover" mode="aspectFill" class="cover-img"></image>
							</view>
							<view class="card-footer">
								<text class="card-title">{{ course.title }}</text>
								<view class="x-end" style="justify-content: space-between;">
									<view class="course-meta">
										<image src="https://bjzmky-1323137866.cos.ap-chongqing.myqcloud.com/class/renshu.png"></image>
										<text class="meta-count">{{ courseViewsDisplay(course.views) }}</text>
									</view>
									<view class="btn-watch" @click.stop="onCourseClick(course)">立即观看</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</template>
			<template v-if="isSearch && keyword && courseList.length === 0 && !courseSectionLoading && !courseLoading">
				<view class="empty-state">
					<!-- <view class="empty-icon"></view> -->
					<image class="empty-icon" src="https://bjzmky-1323137866.cos.ap-chongqing.myqcloud.com/class/nodata.png"></image>
					<text class="empty-text">没有搜索任何内容，换个词试试</text>
				</view>
			</template>
			<view class="bottom-placeholder"></view>
		</scroll-view>
	</view>
</template>

<script>
import { listPublicCourse } from '@/api/home.js'

export default {
	
	data() {
		return {
			statusBarHeight: uni.getSystemInfoSync().statusBarHeight + 'px',
			keyword: '',
			tabIndex: 0,
			activeId:'all',
			categoryExpand: true,
		    isSearch:false,
			showClearIcon: false,
			categoryIndex: 0,
			categories: ['全部', '歌唱艺术', '太极养生', '防骗指南', '手机摄影', '棋牌益智', '用药指导', '膳食营养', '慢病管理'],
			courseList: [],
			coursePageNum: 1,
			coursePageSize: 10,
			courseLoading: false,
			courseRefreshing: false,
			courseSectionLoading: false,
			courseHasMore: true,
			courseSearchKeyword: '',
			courseRequestId: 0,
			recommendLoading: true,
			recentList: [
				// {
				// 	date: '2026-02-08',
				// 	list: [
				// 		{ title: '歌唱家刘金的《0基础金曲演唱速练课》', progress: 87, cover: '' },
				// 		{ title: '资深编辑邹方斌讲《毛笔书法修心课》', progress: 56, cover: '' }
				// 	]
				// },
				// {
				// 	date: '2026-02-01',
				// 	list: [
				// 		{ title: '张斌《元气八段锦》系列课', progress: 56, cover: '' }
				// 	]
				// }
			],
			navList:[
				// {id: 37,categoryName: "健康食品"},
				// {id: 35,categoryName: "绿色有机"}
			],
			recommendList: [
				// { title: '刘金的《0基础金曲演唱速练课》', views: '9239', cover: '' },
				// { title: '邹方斌讲《毛笔书法修心课》', views: '10.8w', cover: '' },
				// { title: '张斌《元气八段锦》系列课', views: '2.5w', cover: '' },
				// { title: '翔哥精讲摄影课-手机微距拍摄技巧...', views: '100w+', cover: '' }
			]
		}
	},
	onLoad(options) {
		// this.keyword = (options && options.keyword) || ''
		this.getCourseList({})
		this.getNewCourseList()
	},
	methods: {
		courseViewsDisplay(views) {
			const raw = views;
			const n = Number(raw);
			if (!Number.isFinite(n) || n < 0) {
				return "0";
			}
			if (n < 100000) {
				return String(Math.floor(n));
			}
			if (n < 1000000) {
				return (n / 10000).toFixed(1) + "w";
			}
			return "100w+";
		},
		onSelectAll() {
			this.$emit('select', { id: 'all', categoryName: '全部' });
		},
		 clearInput: function(event) {
		            this.keyword = event.detail.value;
		            if (event.detail.value.length > 0) {
		                this.showClearIcon = true;
		            } else {
		                this.showClearIcon = false;
		            }
		        },
		        clearIcon: function() {
		            this.keyword = '';
		            this.showClearIcon = false;
					this.isSearch=false;
		        },
		async getCourseList(options = {}) {
			const loadMore = !!options.loadMore
			if (this.courseLoading && loadMore) return
			if (loadMore && !this.courseHasMore) return
			const requestId = ++this.courseRequestId
			if (!loadMore) {
				this.coursePageNum = 1
				this.courseHasMore = true
				if (options.keyword !== undefined) {
					this.courseSearchKeyword = (options.keyword || '').trim()
				}
				if (this.isSearch) {
					if (this.courseList.length > 0) {
						this.courseRefreshing = true
					} else {
						this.courseSectionLoading = true
					}
				}
			}
			const currentPageNum = this.coursePageNum
			const params = {
				pageNum: currentPageNum,
				pageSize: this.coursePageSize
			}
			if (this.courseSearchKeyword) {
				params.keyword = this.courseSearchKeyword
			}
			this.courseLoading = true
			try {
				const res = await listPublicCourse(params)
				if (requestId !== this.courseRequestId) return
				const list = (res && res.data && res.data.list) || []
				const mapped = list.map(item => ({
					courseId: item.courseId,
					title: item.courseTitle || item.courseName || '',
					views: item.watchUserCount || 0,
					cover: item.imgUrl || ''
				}))
				this.courseList = loadMore ? this.courseList.concat(mapped) : mapped
				const total = Number(res.data && res.data.total)
				if (Number.isFinite(total) && total >= 0) {
					this.courseHasMore = this.courseList.length < total
				} else {
					this.courseHasMore = mapped.length >= this.coursePageSize
				}
				if (mapped.length > 0) {
					this.coursePageNum = currentPageNum + 1
				} else {
					this.courseHasMore = false
				}
			} catch (e) {
				if (requestId !== this.courseRequestId) return
				if (!loadMore) {
					this.courseList = []
				}
				this.courseHasMore = false
			} finally {
				if (requestId !== this.courseRequestId) return
				this.courseLoading = false
				this.courseRefreshing = false
				if (!loadMore && this.isSearch) this.courseSectionLoading = false
			}
		},
		onCourseScrollToLower() {
			if (!this.isSearch) return
			this.getCourseList({ loadMore: true })
		},
		async getNewCourseList() {
			if (!this.recommendList.length) this.recommendLoading = true
			try {
				const params = { pageNum: 1, pageSize: 6 }
				const res = await listPublicCourse(params)
				const list = (res && res.data && res.data.list) || []
				this.recommendList = list.map(item => ({
					courseId: item.courseId,
					title: item.courseTitle || item.courseName || '',
					views: item.watchUserCount || 0,
					cover: item.imgUrl || ''
				}))
			} catch (e) {
				this.recommendList = []
			} finally {
				this.recommendLoading = false
			}
		},
		goSearch() {
			if (!this.keyword.trim()) {
				uni.showToast({
					icon: 'none',
					title: '请输入搜索内容'
				})
				return
			}
			if (this.courseRefreshing || this.courseSectionLoading) return
			this.isSearch = true
			this.getCourseList({ keyword: this.keyword })
		},
		onCourseClick(course) {
			if (course && course.courseId) {
				uni.navigateTo({ url: '/pages_index/courseDetail?courseId=' + course.courseId + '&type=1' })
				return
			}
		}
	}
}
</script>

<style lang="scss" scoped>
	/* 图片箭头旋转类（展开时向上） */
	.rotate-arrow {
	  transform: rotate(180deg);
	}
.container {
	min-height: 100vh;
	background: #F5F5F5;
	display: flex;
	flex-direction: column;
}

/* 顶部 Tab */
.top-nav {
	display: flex;
	flex-direction: column;
	background: #fff;
	/* border-bottom: 1rpx solid #f0f0f0; */
}
.search-cont{
		padding: 24rpx 24rpx 20rpx;
		background-color: #FFFFFF;
		display:flex;
		align-items: center;
		justify-content: space-between;
		.inner{
			box-sizing: border-box;
			flex:1;
			// width: 100%;
			height: 76rpx;
			background: #F7F7F7;
			border-radius: 38rpx;
			display: flex;
			align-items: center;
			padding-left:20rpx;
			margin-right: 24rpx;
			.icon-search{
				width: 44rpx;
				height: 44rpx;
				margin-right: 12rpx;
			}
			input{
				height: 76rpx;
				line-height: 76rpx;
				flex: 1;
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 32rpx;
			}
			.close-circle{
				position: relative;
				z-index:999;
				padding:0 20rpx;
			}
			
		}
		.sousuo{
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			font-size: 36rpx;
			color: #222222;
			line-height: 50rpx;
		}
	}
.nav-row {
	display: flex;
	align-items: center;
	padding: 0 24rpx 24rpx;
	gap: 30rpx;
}
.nav-all {
	flex-shrink: 0;
	font-family: PingFangSC, PingFang SC;
	font-weight: 400;
	font-size: 40rpx;
	color: rgba(0,0,0,0.85);
	line-height: 56rpx;
}
.nav-all.active {
	color: #FF233C;
	font-weight: 600;
}
.nav-scroll {
	flex: 1;
}
.nav-inner {
	display: inline-flex;
	padding:0;
	gap: 30rpx;
}
.nav-item {
	flex-shrink: 0;
	font-family: PingFangSC, PingFang SC;
	font-weight: 400;
	font-size: 40rpx;
	color: rgba(0,0,0,0.85);
	line-height: 56rpx;
}
.nav-item.active {
	color: #FF233C;
	font-weight: 600;
}

/* 滚动区 */
.scroll-wrap {
	flex: 1;
	height: 0;
}

/* 分类标签 */
.category-wrap {
	padding:24rpx 0;
	border-radius: 0rpx 0rpx 20rpx 20rpx;
	background: #fff;
	display: flex;
	align-items: center;
	flex-direction: column;
}
.category-tags {
	display: flex;
	flex-wrap: wrap;
	gap:24rpx;
	flex-direction: row;
	justify-content: center;
}
.category-tags.collapsed {
	max-height: 88rpx;
	overflow: hidden;
}
.tag-item {
	width: 30%;
	padding: 20rpx 0;
	text-align: center;
	border-radius: 20rpx;
	background: #f0f0f0;
}
.tag-item text {
	font-family: PingFangSC, PingFang SC;
	font-weight: 400;
	font-size: 40rpx;
	color: rgba(0,0,0,0.85);
}
.tag-item.active {
	background: linear-gradient( 135deg, #FF5267 0%, #FF233C 100%);
}
.tag-item.active text {
	color: #fff;
}
.expand-btn {
	display: flex;
	align-items: center;
	justify-content: center;
	margin-top: 20rpx;
	width: 166rpx;
	height: 64rpx;
	border-radius: 32rpx;
	border: 2rpx solid #FF233C;
}
.expand-btn text {
	font-family: PingFangSC, PingFang SC;
	font-weight: 400;
	font-size: 32rpx;
	color: #FF233C;
}
.expand-btn .arrow {
	margin-left: 6rpx;
	font-size: 22rpx;
}
.expand-btn image{
	margin-left:10rpx ;
	width: 32rpx;
	height: 32rpx;
}
/* 课程网格 */
.course-grid {
	display: flex;
	flex-wrap: wrap;
	padding:24rpx;
	gap: 24rpx 20rpx;
	flex-direction: column;
}
.course-card {
	display: flex;
	background: #fff;
	border-radius: 20rpx;
	overflow: hidden;
	padding: 20rpx;
}
.course-tag {
	position: absolute;
	left: 0;
	bottom: 0;
	right: 0;
	padding: 8rpx 12rpx;
	background: linear-gradient(transparent, rgba(0,0,0,0.6));
	font-size: 22rpx;
	color: #fff;
}
.course-info {
	flex: 1;
	padding-left: 24rpx;
/* padding: 20rpx 24rpx; */
	display: flex;
	flex-direction: column;
	justify-content: space-between;
	min-width: 0;
}

.course-meta {
	display: flex;
	align-items: center;
	image{
		width: 30rpx;
		height: 30rpx;
	}
	
}
.meta-icon {
	font-size: 24rpx;
	margin-right: 6rpx;
	color: #999;
}
.meta-count {
	margin-left: 10rpx;
	font-family: PingFangSC, PingFang SC;
	font-weight: 400;
	font-size: 32rpx;
	color: #666666;
}
.card-cover {
	position: relative;
	width: 296rpx;
	height: 222rpx;
	border-radius: 20rpx;
	overflow: hidden;
	flex-shrink: 0;
}
.cover-img {
	width: 100%;
	height: 100%;
	background: #BB6D6D;
}
.card-title {
	font-family: PingFangSC, PingFang SC;
	font-weight: 600;
	font-size: 36rpx;
	color: #222222;
	line-height: 50rpx;
	text-align: justify;
	overflow: hidden;
	text-overflow: ellipsis;
	display: -webkit-box;
	-webkit-line-clamp: 2;
	-webkit-box-orient: vertical;
}
.card-footer {
	flex: 1;
		padding-left: 24rpx;
	/* padding: 20rpx 24rpx; */
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		min-width: 0;
}
.card-views {
	font-family: PingFangSC, PingFang SC;
	font-weight: 400;
	font-size: 32rpx;
	color: #666666;
	line-height: 44rpx;
}
.btn-watch {
		width: 168rpx;
		height: 64rpx;
		line-height: 64rpx;
		text-align: center;
		background: linear-gradient( 135deg, #FF5267 0%, #FF233C 100%);
		border-radius: 32rpx;
		font-family: PingFangSC, PingFang SC;
		font-weight: 600;
		font-size: 32rpx;
	color: #FFFFFF;
}

/* 最近观看 - 按日期分组 */
.recent-group {
	padding: 24rpx 24rpx 0;
}
.group-date {
	display: flex;
	align-items: center;
	margin-bottom: 24rpx;
}
.date-icon {
	width: 32rpx;
	height: 32rpx;
	margin-right: 8rpx;
}
.date-text {
	font-family: PingFangSC, PingFang SC;
	font-weight: 600;
	font-size: 40rpx;
	line-height: 56rpx;
	color: #222222;
}
.recent-card {
	display: flex;
	background: #fff;
	border-radius: 20rpx;
	overflow: hidden;
	padding: 20rpx;
	margin-bottom: 24rpx;
}
.recent-card:last-child{
	margin-bottom: 0;
}
.recent-thumb {
	position: relative;
	width: 296rpx;
	height: 222rpx;
	border-radius: 20rpx;
	overflow: hidden;
	flex-shrink: 0;
}
.thumb-img {
	width: 100%;
	height: 100%;
	background: #BB6D6D;
}
.recent-info {
	flex: 1;
	padding-left: 24rpx;
	/* padding: 20rpx 24rpx; */
	display: flex;
	flex-direction: column;
	justify-content: space-between;
	min-width: 0;
}
.recent-title {
	font-family: PingFangSC, PingFang SC;
	font-weight: 600;
	font-size: 36rpx;
	color: #222222;
	line-height: 50rpx;
	text-align: justify;
	overflow: hidden;
	text-overflow: ellipsis;
	display: -webkit-box;
	-webkit-line-clamp: 2;
	-webkit-box-orient: vertical;
}
.recent-progress {
	font-family: PingFangSC, PingFang SC;
	font-weight: 400;
	font-size: 32rpx;
	color: rgba(0,0,0,0.65);
	line-height: 44rpx;
}

/* 空态 */
.empty-state {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	padding: 80rpx 0 48rpx;
	/* background: linear-gradient(180deg, #fff5f5 0%, #fff 100%); */
}
.empty-icon {
	width: 310rpx;
	height: 260rpx;
	margin-bottom: 30rpx;
}
.empty-text {
	font-family: PingFangSC, PingFang SC;
	font-weight: 400;
	font-size: 36rpx;
	color: rgba(0,0,0,0.25);
	line-height: 50rpx;
}

/* 为您精选 */
.recommend-section {
	margin-top: 300rpx;
	padding: 0 24rpx 32rpx;
}
.recommend-title {
	display: block;
	font-family: PingFangSC, PingFang SC;
	font-weight: 600;
	font-size: 40rpx;
	color: #222222;
	line-height: 56rpx;
	padding-bottom: 24rpx;
}

.bottom-placeholder {
	height: 120rpx;
}

.load-tip {
	padding: 24rpx 0 8rpx;
	text-align: center;
	font-size: 28rpx;
	color: #999;
}

@mixin sk-shimmer {
	background: linear-gradient(90deg, #eee 25%, #f5f5f5 50%, #eee 75%);
	background-size: 200% 100%;
	animation: skeleton-shimmer 1.2s ease-in-out infinite;
}

.sk-line {
	height: 28rpx;
	border-radius: 6rpx;
	@include sk-shimmer;
}
.sk-line-lg { width: 90%; }
.sk-line-md { width: 70%; margin-top: 16rpx; }
.sk-line-sm { width: 40%; }

@keyframes skeleton-shimmer {
	0% { background-position: 100% 0; }
	100% { background-position: -100% 0; }
}

.course-skeleton {
	padding: 24rpx;
	display: flex;
	flex-direction: column;
	gap: 24rpx;
}
.sk-course-card {
	display: flex;
	background: #fff;
	border-radius: 20rpx;
	padding: 20rpx;
}
.sk-thumb {
	width: 296rpx;
	height: 222rpx;
	border-radius: 20rpx;
	flex-shrink: 0;
	@include sk-shimmer;
}
.sk-info {
	flex: 1;
	padding-left: 24rpx;
	min-width: 0;
}
.sk-foot {
	display: flex;
	align-items: center;
	justify-content: space-between;
	margin-top: 40rpx;
}
.sk-btn {
	width: 168rpx;
	height: 64rpx;
	border-radius: 32rpx;
	@include sk-shimmer;
}

.recommend-skeleton-wrap {
	margin-top: 300rpx;
	padding: 0 24rpx 32rpx;
}

.course-grid-wrap {
	position: relative;
}
.course-grid.list-dimmed {
	opacity: 0.55;
	transition: opacity 0.2s ease;
}
.refresh-mask {
	position: absolute;
	left: 0;
	right: 0;
	top: 0;
	bottom: 0;
	display: flex;
	align-items: center;
	justify-content: center;
	pointer-events: none;
}
.refresh-spinner {
	width: 48rpx;
	height: 48rpx;
	border: 4rpx solid #e8e8e8;
	border-top-color: #FF233C;
	border-radius: 50%;
	animation: spin 0.8s linear infinite;
}
@keyframes spin {
	to { transform: rotate(360deg); }
}
</style>
