<template>
	<view class="entertainment-page">
		<image class="home_top_bg" src="@/static/images/enter/entertainment_top_bg.png" mode="widthFix"></image>
		<!-- 状态栏占位 -->
		<view class="status-bar" :style="{ height: statusBarHeight + 'px' }"></view>
		<!-- 顶部导航栏 -->
		<u-navbar :bgColor="bgColor" leftIconColor="#FFFFFF" title="娱乐模式" :autoBack="true"> </u-navbar>
		<!-- <view class="nav-bar" :style="{background:bgColor,paddingTop: statusBarHeight+'px'}">
			<view class="nav-title">娱乐模式</view>
		</view> -->
		<!-- 页面内容 -->
		<scroll-view class="page-content" scroll-y :style="'height:' + clientHeight + 'px;'"
			@scrolltolower="reachBottom">
			<!-- 主横幅 -->
			<view class="banner-section">
				<swiper class="banner-swiper" circular :indicator-dots="true" :autoplay="true" :interval="3000"
					:duration="1000" indicator-color="rgba(255,255,255,0.5)" indicator-active-color="#fff">
					<swiper-item v-for="(url,index) in advImgs" :key="index" class="swiper-item"
						@click="handleAdvClick(index)">
						<image style="width: 100%;height: 100%;" :src="url" mode="aspectFill"></image>
					</swiper-item>
				</swiper>
			</view>
			<image v-if="activityShow" @tap="$navTo('/pages_enter/activity/index')" class="img_enter_dance"
				src="/static/images/enter/activity/img_enter_dance.png" mode="">
			</image>
			<!-- 娱乐专区 -->
			<view class="entertainment-zone">
				<view class="section-title">
					<image class="w276 h52" src="@/static/images/enter/entertainment_zone_title.png"></image>
				</view>
				<!-- 前两张图一行并排 -->
				<view class="zone-grid">
					<view class="zone-item " v-for="(item, index) in zoneList.slice(0, 2)" :key="index"
						@click="handleZoneClick(item)">
						<view class="zone-card">
							<image :src="item.icon" mode="widthFix"></image>
						</view>
					</view>
				</view>
				<!-- 后面三张图一行并排 -->
				<view class="zone-grid">
					<view class="zone-item" v-for="(item, index) in zoneList.slice(2)" :key="index + 2"
						:class="[index==0?'es-mr-10':'']" @click="handleZoneClick(item)">
						<view class="zone-card">
							<image :src="item.icon" mode="widthFix"></image>
						</view>
					</view>
				</view>
			</view>

			<!-- 精彩推荐 -->
			<view class="recommend-section">
				<view class="section-title">
					<image class="w276 h52" src="@/static/images/enter/featured_recommendations_title.png"></image>
				</view>
				<mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback"
					:down="downOption" :up="upOption">
					<view class="recommend-grid">
						<view class="recommend-item" v-for="(item, index) in recommendList" :key="index"
							@click="handleRecommendClick(item)">
							<view class="recommend-card">
								<image class="recommend-image" :src="item.image" mode="aspectFill"></image>
								<view class="recommend-info">
									<view class="recommend-stats">
										<text class="play-count">{{ item.playCount }}次播放</text>
										<text class="episode-count">共{{ item.episodeCount }}讲</text>
									</view>
									<view class="recommend-title">{{ item.title }}</view>
								</view>
							</view>
						</view>
					</view>
					<!-- <u-loadmore :status="status" marginTop="30"></u-loadmore> -->
				</mescroll-body>
			</view>

			<!-- 底部安全区域 -->
			<view class="safe-area-bottom" style="height: 100rpx;"></view>
		</scroll-view>
	</view>
</template>

<script>
	import {
		getActivityInfo
	} from '@/api/activity.js';
	import {
		getAdvList
	} from '@/api/adv.js'
	import {
		getCourseList
	} from '@/api/course'

	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin],
		data() {
			return {
				top: 0,
				clientHeight: 0,
				statusBarHeight: 0,
				safeAreaBottom: 0,
				currentNavIndex: 2, // 娱乐页面为当前激活
				advImgs: [],
				zoneList: [{
						id: 1,
						name: '小游戏',
						url: '/pages_enter/games',
						icon: '/static/images/enter/mini_game_img.png',
					}, {
						id: 3,
						name: '戏曲评书',
						url: '/pages_enter/program?cateId=4',
						icon: '/static/images/enter/opera_img.png',
					}, {
						id: 5,
						name: '节目',
						// url: '/pages_enter/program',
						url: '/pages_enter/program?cateId=1',
						icon: '/static/images/enter/program_img2.png',
					},
					{
						id: 2,
						name: '热播剧',
						url: '/pages_enter/program?cateId=3',
						icon: '/static/images/enter/hit_drama_img.png',
					},
					{
						id: 4,
						name: '运动',
						// url: '/pages_enter/sports',
						url: '/pages_enter/program?cateId=2',
						icon: '/static/images/enter/sports_img.png',
					},
					// {
					// 	id: 2,
					// 	name: '短剧',
					// 	url:'/pages_enter/program?cateId=4',
					// 	// url:'/pages_enter/drama',
					// 	icon: '/static/images/enter/short_play_img.png',
					// },
					// {
					// 	id: 3,
					// 	name: '小说',
					// 	// url:'/pages_enter/novel',
					// 	url:'/pages_enter/program?cateId=3',
					// 	icon: '/static/images/enter/novel_img.png',
					// },
				],
				recommendList: [],
				statusBarHeight: '',
				pageNum: 1,
				pageSize: 10,
				totalPage: 1,
				status: 'loadmore',
				downOption: { //下拉刷新
					use: true,
					auto: false // 不自动加载 (mixin已处理第一个tab触发downCallback)
				},
				upOption: {
					onScroll: false,
					use: true, // 是否启用上拉加载; 默认true
					page: {
						pae: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 10, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					textNoMore: "已经到底了",
					empty: {
						icon: 'https://zkzh-2025.oss-cn-beijing.aliyuncs.com/appimgs/cf4a86b913a04341bb44e34bb4d37aa2.png',
						tip: '暂无数据'
					}
				},
				bgColor: 'transparent',
				activityShow: false,
			}
		},
		onPageScroll(e) {
			this.top = e.scrollTop;
			if (e.scrollTop > 30) {
				this.bgColor = '#F4FFF2'
			} else {
				this.bgColor = 'transparent'
			}
		},
		onLoad() {
			const systemInfo = uni.getSystemInfoSync();
			this.statusBarHeight = systemInfo.statusBarHeight;
			this.getAdvList();
			this.getSystemInfo();
			this.getActivityInfoFun();
		},
		onShow() {
			uni.showTabBar();
		},
		// computed: {
		// 	// 计算属性的 getter
		// 	bgColor: function() {
		// 		var top = this.top / 30;
		// 		return 'rgba(239, 254, 248,' + top + ')';
		// 	},
		// },
		onReady() {
			uni.getSystemInfo({
				success: (res) => {
					this.clientHeight = res.windowHeight - uni.upx2px(120) - res.statusBarHeight;
				}
			})
		},
		methods: {
			async getActivityInfoFun() {
				const res = await getActivityInfo({
					activityId: getApp().globalData.activityId
				})
				if (res.code == 200 && res.data) {
					this.activityShow = res.data.status == 1 ? true : false
					getApp().globalData.miniprogamId = res.data.id
					this.activityShow = true
				}
			},
			getAdvList() {
				let that = this;
				let data = {
					advType: 12,
					status: 1
				}
				getAdvList(data).then(res => {
					if (res.code == 200) {
						that.advImgs = [];
						that.advs = [];
						res.data.forEach(function(element) {
							if (element.imageUrl != null && element.imageUrl != "") {
								that.advs.push(element);
								that.advImgs.push(element.imageUrl);
							}
						});

					} else {
						uni.showToast({
							icon: 'none',
							title: "请求失败"
						});
					}
				});
			},
			getSystemInfo() {
				const systemInfo = uni.getSystemInfoSync();
				this.statusBarHeight = systemInfo.statusBarHeight || 0;
				// 计算底部安全区域高度
				if (systemInfo.safeArea) {
					this.safeAreaBottom = systemInfo.screenHeight - systemInfo.safeArea.bottom;
				}
			},
			handlePrizeCollection() {
				uni.showToast({
					title: '有奖征集',
					icon: 'none'
				});
			},
			handleZoneClick(item) {
				if (item.url) {
					uni.navigateTo({
						url: item.url
					});
				} else {
					uni.showToast({
						title: '正在开发中...',
						icon: 'none'
					});
				}
			},
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			/*下拉刷新的回调 */
			downCallback(mescroll) {
				mescroll.resetUpScroll()
			},
			upCallback(page) {
				let params = {
					isTui: 1,
					// isBest: 1,
					pageNum: page.num,
					pageSize: page.size,
					limit: page.size
				};
				let that = this;
				getCourseList(params).then(res => {
					if (res.code === 200) {
						let list = res.data.list.map(item => ({
							id: item.courseId,
							title: item.courseName,
							playCount: item.views,
							episodeCount: item.sectionCount || 0,
							image: item.imgUrl
						}));
						if (page.num == 1) {
							that.recommendList = list
						} else {
							that.recommendList = that.recommendList.concat(list);
						}
						that.mescroll.endBySize(res.data.list.length, res.data.total);
					}
				})
			},
			handleRecommendClick(item) {
				uni.navigateTo({
					url: `/pages/course/info?courseId=${item.id}`
				});
			},
			handleNavClick(item, index) {
				if (this.currentNavIndex === index) {
					return;
				}
				this.currentNavIndex = index;
				if (item.path && item.path !== '/pages_enter/index') {
					uni.switchTab({
						url: item.path
					});
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.entertainment-page {
		width: 100%;
		min-height: 100vh;
		background-color: #fff !important;
		display: flex;
		flex-direction: column;
		position: relative;
		z-index: 2;
	}

	.home_top_bg {
		width: 100%;
		height: auto;
		position: absolute;
		top: 0;
		left: 0;
		z-index: 0;
	}

	.status-bar {
		width: 100%;
	}

	.nav-bar {
		width: 100%;
		height: 88rpx;
		//background-color: #ffffff;
		display: flex;
		align-items: center;
		justify-content: center;
		position: fixed;
		top: 0;
		left: 0;
		z-index: 1000;

		.nav-title {
			font-family: PingFang SC, PingFang SC;
			font-weight: 600;
			font-size: 40rpx;
			color: #222222;
		}
	}

	.page-content {
		flex: 1;
		width: 100%;
	}

	// 主横幅
	.banner-section {
		width: 100%;
		height: 280rpx;
		margin-top: 108rpx;
		padding: 0 30rpx;

		.banner-swiper {
			width: 100%;
			height: 100%;
			border-radius: 24rpx;
			overflow: hidden;
		}

		.banner-item {
			width: 100%;
			height: 100%;
			background: linear-gradient(135deg, #9b59b6 0%, #8e44ad 50%, #7d3c98 100%);
			position: relative;
			display: flex;
			align-items: center;
			justify-content: center;
			overflow: hidden;

			&::before {
				content: '';
				position: absolute;
				top: -50%;
				left: -50%;
				width: 200%;
				height: 200%;
				background: radial-gradient(circle, rgba(255, 255, 255, 0.1) 1px, transparent 1px);
				background-size: 30rpx 30rpx;
				animation: sparkle 3s linear infinite;
			}
		}

		@keyframes sparkle {
			0% {
				transform: rotate(0deg);
			}

			100% {
				transform: rotate(360deg);
			}
		}

		.banner-content {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			position: relative;
			z-index: 2;
		}

		.banner-title {
			font-size: 56rpx;
			font-weight: bold;
			color: #ffffff;
			margin-bottom: 40rpx;
			text-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.2);
		}

		.banner-btn {
			background-color: #ff6b9d;
			color: #ffffff;
			padding: 20rpx 60rpx;
			border-radius: 50rpx;
			font-size: 28rpx;
			font-weight: 500;
		}
	}

	// 娱乐专区
	.entertainment-zone {
		width: 100%;
		padding: 40rpx 30rpx;

		.section-title {
			display: flex;
			align-items: center;
			justify-content: center;
			margin-bottom: 40rpx;

			.sparkle-icon {
				font-size: 32rpx;
				margin: 0 10rpx;
			}

			.title-text {
				font-size: 36rpx;
				font-weight: 600;
				color: #333333;
			}
		}

		.zone-grid {
			display: flex;
			margin-bottom: 20rpx;
			margin-top: 80rpx;

			&:last-child {
				margin-bottom: 0;
				margin-top: 0;
			}
		}

		.zone-item {
			flex: 1
		}

		.zone-card {
			width: 100%;
			display: flex;
			align-items: center;

			image {
				width: 100%;
			}
		}

		.zone-icon-wrapper {
			width: 100rpx;
			height: 100rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			margin-right: 20rpx;

			.zone-icon {
				font-size: 60rpx;
			}
		}

		.zone-info {
			flex: 1;
			display: flex;
			flex-direction: column;
			justify-content: center;
		}

		.zone-name {
			font-size: 32rpx;
			font-weight: 600;
			color: #ffffff;
			margin-bottom: 10rpx;
			display: flex;
			align-items: center;

			.badge {
				width: 32rpx;
				height: 32rpx;
				background-color: #ff4757;
				border-radius: 50%;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 20rpx;
				color: #ffffff;
				margin-left: 10rpx;
			}
		}

		.zone-desc {
			font-size: 24rpx;
			color: rgba(255, 255, 255, 0.9);
		}
	}

	// 精彩推荐
	.recommend-section {
		width: 100%;
		padding: 0rpx 30rpx;
		margin-bottom: 40rpx;

		.section-title {
			display: flex;
			align-items: center;
			justify-content: center;
			margin-bottom: 40rpx;

			.sparkle-icon {
				font-size: 32rpx;
				margin: 0 10rpx;
			}

			.title-text {
				font-size: 36rpx;
				font-weight: 600;
				color: #333333;
			}
		}

		.recommend-grid {
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;
		}

		.recommend-item {
			width: 332rpx;
			margin-bottom: 24rpx;
		}

		.recommend-card {
			width: 100%;
			background-color: #ffffff;
			// border-radius: 16rpx;
			overflow: hidden;
		}

		.recommend-image {
			width: 100%;
			height: 208rpx;
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			background-color: #f0f0f0;
		}

		.recommend-info {
			padding: 20rpx;
		}

		.recommend-title {
			font-size: 28rpx;
			font-weight: 500;
			color: #333333;
			margin-bottom: 12rpx;
			overflow: hidden;
			text-overflow: ellipsis;
			white-space: nowrap;
		}

		.recommend-stats {
			display: flex;
			align-items: center;
			justify-content: space-between;
			font-size: 22rpx;
			color: #999999;

			.play-count {
				margin-right: 10rpx;
			}
		}
	}

	.safe-area-bottom {
		width: 100%;
	}

	// 底部导航栏
	.bottom-nav {
		width: 100%;
		height: 100rpx;
		background-color: #ffffff;
		display: flex;
		align-items: center;
		justify-content: space-around;
		border-top: 1rpx solid #f0f0f0;
		padding-bottom: env(safe-area-inset-bottom);

		.nav-item {
			flex: 1;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			padding: 10rpx 0;
		}

		.nav-icon-wrapper {
			width: 48rpx;
			height: 48rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			margin-bottom: 6rpx;

			.nav-icon {
				font-size: 40rpx;
				line-height: 1;
			}
		}

		.nav-label {
			font-size: 22rpx;
			color: #999999;

			&.active {
				color: #FF233C;
				font-weight: 500;
			}
		}
	}

	.img_enter_dance {
		margin: 24rpx 24rpx 0;
		height: 156rpx;
		width: calc(100vw - 48rpx);
	}
</style>