<template>
	<view>
		<TitleHeader />
		<AudioControl :top="'230rpx'" audioSrc="https://des.dayunyuanjian.cn/data/2025/09/05/286e6a8d-4433-4d69-b705-74b3f4237667.MP3"  />
		<swiper class="main-swiper" :vertical="true" :current="currentIndex" @change="handleSwiperChange"
			:duration="300" :style="{ height: `calc(100vh - ${titleHeight}px)` }">
			<swiper-item>
				<view class="page-container">
					<template v-if="loadedPages[0]">
						<image v-show="shouldShowContent(0)" class="bg-image" src="https://des.dayunyuanjian.cn/epicSoul/taozi/chapter1/bg1s.png"
							:lazy-load="true" mode="aspectFill"></image>
						<image v-show="shouldShowContent(0)" class="layer-img1"
							:class="{'slide-in-from-left': animationStates[0], 'hidden': !animationStates[0]}"
							src="https://des.dayunyuanjian.cn/epicSoul/taozi/chapter1/bg1-info.png" :lazy-load="true" mode="aspectFill">
						</image>
					</template>
				</view>
			</swiper-item>
			<swiper-item>
				<view class="page-container">
					<template v-if="loadedPages[1]">
						<image v-show="shouldShowContent(1)" class="bg-image" src="https://des.dayunyuanjian.cn/epicSoul/taozi/chapter1/bg2.png"
							:lazy-load="true" mode="aspectFill"></image>
						<image v-show="shouldShowContent(1)" class="layer-img2"
							:class="{'slide-in-from-left': animationStates[1], 'hidden': !animationStates[1]}"
							src="https://des.dayunyuanjian.cn/epicSoul/taozi/chapter1/bg2-info.png" :lazy-load="true" mode="aspectFill">
						</image>
					</template>
				</view>
			</swiper-item>
			<swiper-item>
				<view class="page-container">
					<template v-if="loadedPages[2]">
						<image v-show="shouldShowContent(2)" class="bg-image" src="https://des.dayunyuanjian.cn/epicSoul/taozi/chapter1/bg3.jpg"
							:lazy-load="true" mode="aspectFill"></image>
						<view class="content-layer" v-show="shouldShowContent(2)">
							<image class="layer-img3"
								:class="{'slide-in-from-right': animationStates[2], 'hidden': !animationStates[2]}"
								src="https://des.dayunyuanjian.cn/epicSoul/taozi/chapter1/bg3-info.png" :lazy-load="true" mode="aspectFill"></image>
						</view>
					</template>
				</view>
			</swiper-item>
			<swiper-item>
				<view class="page-container">
					<template v-if="loadedPages[3]">
						<image v-show="shouldShowContent(3)" class="bg-image" src="https://des.dayunyuanjian.cn/epicSoul/taozi/chapter1/bg4.jpg"
							:lazy-load="true" mode="aspectFill"></image>
						<image v-show="shouldShowContent(3)" class="layer-img4" src="https://des.dayunyuanjian.cn/epicSoul/taozi/chapter1/bg4-info.gif"
							:lazy-load="true" mode="aspectFill">
						</image>
					</template>
				</view>
			</swiper-item>
			<swiper-item>
				<view class="page-container">
					<template v-if="loadedPages[4]">
						<image v-show="shouldShowContent(4)" class="bg-image" src="https://des.dayunyuanjian.cn/epicSoul/taozi/chapter1/bg5.png"
							:lazy-load="true" mode="aspectFill"></image>
						<image :class="{'slide-in-from-right': animationStates[4], 'hidden': !animationStates[4]}"
							v-show="shouldShowContent(4)" class="layer-img5" src="https://des.dayunyuanjian.cn/epicSoul/taozi/chapter1/bg5-info.png"
							:lazy-load="true" mode="aspectFill">
						</image>
					</template>
				</view>
			</swiper-item>
			<swiper-item>
				<view class="page-container">
					<template v-if="loadedPages[5]">
						<image v-show="shouldShowContent(5)" class="bg-image" src="https://des.dayunyuanjian.cn/epicSoul/taozi/chapter1/bg6.png"
							:lazy-load="true" mode="aspectFill"></image>
					</template>
				</view>
			</swiper-item>
			<swiper-item>
				<view class="page-container">
					<template v-if="loadedPages[6]">
						<image v-show="shouldShowContent(6)" class="bg-image" src="https://des.dayunyuanjian.cn/epicSoul/taozi/chapter1/bg7.png"
							:lazy-load="true" mode="aspectFill"></image>
						<view @click="goBack" class="back-btn" type="default">
							<image class="back-icon" src="https://des.dayunyuanjian.cn/epicSoul/taozi/back.png" mode="aspectFill"></image>
						</view>
					</template>
				</view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	import AudioControl from '@/components/AudioControl.vue';
	import TitleHeader from '@/components/TitleHeader.vue';
	export default {
		components: {
			TitleHeader,
			AudioControl
		},
		data() {
			return {
				currentIndex: 0,
				loadedPages: {
					0: false,
					1: false,
					2: false,
					3: false,
					4: false,
					5: false,
					6: false
				},
				animationStates: {
					0: false,
					1: false,
					2: false,
					3: false,
					4: false,
					5: false,
					6: false
				},
				preloadBuffer: 1,
				titleHeight: 0
			};
		},
		watch: {
			currentIndex(newIndex) {
				for (let i = Math.max(0, newIndex - this.preloadBuffer); i <= Math.min(9, newIndex + this
					.preloadBuffer); i++) {
					this.loadedPages[i] = true;
				}
			}
		},
		mounted() {
			this.titleHeight = uni.getStorageSync('titleHeight')
			for (let i = 0; i <= Math.min(1 + this.preloadBuffer, 9); i++) {
				this.loadedPages[i] = true;
			}
			setTimeout(() => {
				this.animationStates[this.currentIndex] = true;
			}, 50);
		},
		methods: {
			handleSwiperChange(e) {
				const newIndex = e.detail.current;
				this.currentIndex = newIndex;
				this.animationStates[newIndex] = false;
				setTimeout(() => {
					this.animationStates[newIndex] = true;
				}, 50);
			},
			shouldShowContent(index) {
				return Math.abs(index - this.currentIndex) <= this.preloadBuffer;
			},
			goBack() {
				uni.navigateTo({
					url: '/taozi/home/home?targetIndex=5'
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
	.main-swiper {
		width: 100%;
		height: 100vh;
	}

	.page-container {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		height: 100%;
		position: relative;
		overflow: hidden;
	}

	.bg-image {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		z-index: 1;
	}

	.layer-img1 {
		width: 500rpx;
		z-index: 2;
		margin-right: 180rpx;
		margin-top: 50rpx;
	}

	.layer-img2 {
		z-index: 2;
		position: absolute;
		bottom: 18%;
		left: 0;
		width: 100%;
		height: 350rpx;
	}

	.content-layer {
		margin: 130rpx 0;
		width: 100%;
		height: 100%;
		display: flex;
		justify-content: flex-end;
	}

	.layer-img3 {
		height: 100%;
		width: 280rpx;
		z-index: 2;
		margin-top: 10rpx;
		margin-right: 30rpx;
	}

	.layer-img4 {
		z-index: 2;
		width: 400rpx;
		height: 400rpx;
		margin-top: 400rpx;
		margin-left: 80rpx;
	}

	.layer-img5 {
		z-index: 2;
		position: absolute;
		bottom: 90rpx;
		right: 30rpx;
		width: 320rpx;
		height: 500rpx;
	}

	.back-btn {
		position: absolute;
		top: 50rpx;
		left: 50rpx;
		z-index: 2;
		background-color: rgb(0 0 0 / 0.3);
		border-radius: 50%;
		width: 80rpx;
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.back-icon {
		width: 50rpx;
		height: 50rpx;
	}

	.hidden {
		opacity: 0;
	}

	.slide-in-from-left {
		animation: slideInLeft 1.2s ease-out forwards;
	}

	@keyframes slideInLeft {
		0% {
			opacity: 0;
			transform: translateX(-100px);
		}

		100% {
			opacity: 1;
			transform: translateX(0);
		}
	}

	.slide-in-from-right {
		animation: slideInRight 1.2s ease-out forwards;
	}

	@keyframes slideInRight {
		0% {
			opacity: 0;
			transform: translateX(100px);
		}

		100% {
			opacity: 1;
			transform: translateX(0);
		}
	}
</style>