<template>
	<view>
		<TitleHeader />
		<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://static.ticket.sz-trip.com/epicSoul/taozi/chapter4/bg1.png"
							:lazy-load="true" mode="aspectFill"></image>
						<view v-show="shouldShowContent(0)" class="content-layer">
							<image class="layer-img"
								:class="{'blur-to-clear': animationStates[0], 'hidden': !animationStates[0]}"
								src="https://static.ticket.sz-trip.com/epicSoul/taozi/chapter4/bg1-infos.png" :lazy-load="true" mode="aspectFill">
							</image>
							<image class="btn-img" @click="gotoPath('/subPackages/techan/detail?id=32')"
								:class="{'blur-to-clear': animationStates[0], 'hidden': !animationStates[0]}"
								src="https://static.ticket.sz-trip.com/epicSoul/taozi/chapter4/btn1.png" :lazy-load="true" mode="widthFix">
							</image>
						</view>
					</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://static.ticket.sz-trip.com/epicSoul/taozi/chapter4/bg2.png"
							:lazy-load="true" mode="aspectFill"></image>
						<view v-show="shouldShowContent(1)" class="content-layer">
							<image class="layer-img"
								:class="{'blur-to-clear': animationStates[1], 'hidden': !animationStates[1]}"
								src="https://static.ticket.sz-trip.com/epicSoul/taozi/chapter4/bg2-infos.png" :lazy-load="true" mode="aspectFill">
							</image>
							<image class="btn-img" style="right: 30rpx;top: 75vh;" @click="gotoPath('/subPackages/techan/detail?id=32')"
								:class="{'blur-to-clear': animationStates[1], 'hidden': !animationStates[1]}"
								src="https://static.ticket.sz-trip.com/epicSoul/taozi/chapter4/btn2.png" :lazy-load="true" mode="widthFix">
							</image>
						</view>
					</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://static.ticket.sz-trip.com/epicSoul/taozi/chapter4/bg3.png"
							:lazy-load="true" mode="aspectFill"></image>
						<view v-show="shouldShowContent(2)" class="content-layer">
							<image class="layer-img"
								:class="{'blur-to-clear': animationStates[2], 'hidden': !animationStates[2]}"
								src="https://static.ticket.sz-trip.com/epicSoul/taozi/chapter4/bg3-infos.png" :lazy-load="true" mode="aspectFill">
							</image>
							<image class="btn-img" style="right: 40vw;top: 65vh;" @click="gotoPath('/subPackages/techan/detail?id=32')"
								:class="{'blur-to-clear': animationStates[1], 'hidden': !animationStates[1]}"
								src="https://static.ticket.sz-trip.com/epicSoul/taozi/chapter4/btn3.png" :lazy-load="true" mode="widthFix">
							</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://static.ticket.sz-trip.com/epicSoul/taozi/chapter4/bg4.png"
							:lazy-load="true" mode="aspectFill"></image>
						<view v-show="shouldShowContent(3)" class="content-layer2">
							<image class="layer-img2"
								:class="{'slide-in-from-left': animationStates[3], 'hidden': !animationStates[3]}"
								src="https://static.ticket.sz-trip.com/epicSoul/taozi/chapter4/bg4-info.png" :lazy-load="true" mode="aspectFill">
							</image>
						</view>
					</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://static.ticket.sz-trip.com/epicSoul/taozi/chapter4/bg5.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://static.ticket.sz-trip.com/epicSoul/taozi/chapter4/bg6.png"
							:lazy-load="true" mode="aspectFill"></image>
						<view v-show="shouldShowContent(5)" class="content-layer3">
							<image class="layer-img3"
								:class="{'slide-in-from-left': animationStates[5], 'hidden': !animationStates[5]}"
								src="https://static.ticket.sz-trip.com/epicSoul/taozi/chapter4/bg6-info.png" :lazy-load="true" mode="aspectFill">
							</image>
						</view>
						<view @click="goBack" class="back-btn" type="default">
							<image class="back-icon" src="https://static.ticket.sz-trip.com/epicSoul/taozi/back.png" mode="aspectFill"></image>
						</view>
					</template>
				</view>
			</swiper-item>
		</swiper>
		<AudioControl :top="'230rpx'" audioSrc="https://des.js-dyyj.com/data/2025/09/05/286e6a8d-4433-4d69-b705-74b3f4237667.MP3"  />
	</view>
</template>

<script>
	import TitleHeader from '@/components/TitleHeader.vue';
	import AudioControl from '@/components/AudioControl.vue';
	export default {
		components: {
			TitleHeader,
			AudioControl
		},
		data() {
			return {
				currentIndex: 0,
				loadedPages: {
					0: false,
					1: false,
					2: false,
					3: false,
					4: false,
					5: false
				},
				animationStates: {
					0: false,
					1: false,
					2: false,
					3: false,
					4: false,
					5: 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=7'
				});
			}
		}
	};
</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;
	}

	.content-layer {
		width: 100%;
		height: 100%;
		position: relative;
		z-index: 2;
	}

	.content-layer2 {
		width: calc(100% - 100rpx);
		height: 100%;
		position: relative;
		z-index: 2;
		display: flex;
		flex-direction: column;
		justify-content: flex-end;
		margin: 0 50rpx 400rpx;
	}

	.content-layer3 {
		width: calc(100% - 100rpx);
		height: 100%;
		z-index: 2;
		display: flex;
		align-items: center;
	}

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

	.layer-img {
		width: 100%;
		height: 100%;
	}

	.layer-img2 {
		width: 100%;
		height: 350rpx;
	}

	.layer-img3 {
		width: 500rpx;
		height: 300rpx;
	}

	.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;
	}

	.blur-to-clear {
		animation: blurToClear .8s ease-out forwards;
	}

	@keyframes blurToClear {
		0% {
			filter: blur(10px);
			opacity: 0.3;
		}

		100% {
			filter: blur(0);
			opacity: 1;
		}
	}

	.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);
		}
	}
	
	.btn-img {
		position: fixed;
		top: 14vh;
		right: 30rpx;
		width: 20vw;
	}
</style>