<template>
	<view class="guide-popup" v-if="visible">
		<view class="guide-mask"></view>
		<view class="guide-wrap">
			<view class="guide-card">
				<view v-if="imageLoading" class="guide-loading">
					<u-loading-icon mode="circle" color="#FF233C" size="40"></u-loading-icon>
				</view>
				<template v-else>
					<swiper
						class="guide-swiper"
						:current="guideCurrent"
						@change="onGuideSwiperChange"
						:indicator-dots="false"
						:autoplay="false"
						:circular="false"
					>
						<swiper-item v-for="(item, index) in guideList" :key="'guide-' + index">
							<image class="guide-img" :src="item.value" mode="aspectFit" />
						</swiper-item>
					</swiper>
					<view class="guide-dots" v-if="guideList.length > 1">
						<view
							v-for="(item, index) in guideList"
							:key="'guide-dot-' + index"
							class="guide-dot"
							:class="{ active: guideCurrent === index }"
						></view>
					</view>
				</template>
			</view>
			<view v-if="!imageLoading" class="guide-btn" @tap="onGuideAction">{{ isGuideLast ? '立即体验' : '下一页' }}</view>
			<view v-if="!imageLoading" class="guide-close" @tap="closeGuide">
				<u-icon name="close-circle" color="#fff" size="36"></u-icon>
			</view>
		</view>
	</view>
</template>

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

const GUIDE_VIEWED_KEY = 'store_activity_guide_viewed'

export default {
	name: 'GuidePopup',
	data() {
		const guideViewed = !!uni.getStorageSync(GUIDE_VIEWED_KEY)
		return {
			visible: false,
			guideList: [],
			guideCurrent: 0,
			guideViewed,
			imageLoading: false,
			imagesReady: false
		}
	},
	created() {
		this.syncViewedState()
	},
	computed: {
		isGuideLast() {
			return this.guideList.length > 0 && this.guideCurrent >= this.guideList.length - 1
		}
	},
	methods: {
		isGuideViewedFlag(viewed) {
			return viewed === true || viewed === 1 || viewed === '1' || viewed === 'true'
		},
		markGuideViewed() {
			this.guideViewed = true
			this.visible = false
			this.imageLoading = false
			uni.setStorageSync(GUIDE_VIEWED_KEY, true)
		},
		syncViewedState() {
			if (uni.getStorageSync(GUIDE_VIEWED_KEY) || this.guideViewed) {
				this.markGuideViewed()
			}
		},
		check() {
			if (!this.utils.checkLoginState()) return
			this.syncViewedState()
			if (this.guideViewed) return
			getStoreActivityGuide().then(res => {
				if (res.code != 200) return
				const guideList = res.guideData || []
				const viewed = this.isGuideViewedFlag(res.viewed)
				if (viewed) {
					this.markGuideViewed()
					return
				}
				if (uni.getStorageSync(GUIDE_VIEWED_KEY) || !guideList.length) {
					this.visible = false
					return
				}
				this.guideList = guideList
				const isFirstOpen = !this.visible
				if (isFirstOpen) {
					this.guideCurrent = 0
				}
				this.visible = true
				if (isFirstOpen || !this.imagesReady) {
					this.prepareGuideImages(guideList)
				}
			})
		},
		prepareGuideImages(guideList) {
			this.imageLoading = true
			this.imagesReady = false
			this.preloadGuideImages(guideList).then(() => {
				this.imagesReady = true
				this.imageLoading = false
			})
		},
		preloadGuideImages(guideList) {
			const urls = (guideList || []).map(item => item && item.value).filter(Boolean)
			if (!urls.length) return Promise.resolve()
			return Promise.all(urls.map(url => new Promise(resolve => {
				uni.getImageInfo({
					src: url,
					success: () => resolve(),
					fail: () => resolve()
				})
			})))
		},
		onGuideSwiperChange(e) {
			this.guideCurrent = e.detail.current
		},
		onGuideAction() {
			if (this.isGuideLast) {
				this.closeGuide()
			} else {
				this.guideCurrent++
			}
		},
		closeGuide() {
			if (!this.visible) return
			this.markGuideViewed()
			this.$emit('close')
			// viewStoreActivityGuide().catch(() => {})
		}
	}
}
</script>

<style lang="scss" scoped>
.guide-popup {
	position: fixed;
	top: 0;
	right: 0;
	left: 0;
	bottom: 0;
	z-index: 1001;
	display: flex;
	justify-content: center;
	align-items: center;
}
.guide-mask {
	position: absolute;
	top: 0;
	right: 0;
	bottom: 0;
	left: 0;
	background: rgba(0, 0, 0, 0.55);
}
.guide-wrap {
	position: relative;
	z-index: 1;
	width: 670rpx;
	display: flex;
	flex-direction: column;
	align-items: center;
}
.guide-card {
	width: 670rpx;
	border-radius: 40rpx;
	overflow: hidden;
	position: relative;
}
.guide-loading {
	width: 670rpx;
	height: 880rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	background: rgba(255, 255, 255, 0.96);
}
.guide-swiper {
	width: 670rpx;
	height: 880rpx;
}
.guide-img {
	width: 100%;
	height: 100%;
	display: block;
}
.guide-dots {
	position: absolute;
	left: 0;
	right: 0;
	bottom: 24rpx;
	display: flex;
	justify-content: center;
	align-items: center;
	gap: 12rpx;
}
.guide-dot {
	width: 12rpx;
	height: 12rpx;
	border-radius: 6rpx;
	background: rgba(255, 255, 255, 0.45);
	transition: all 0.2s;
}
.guide-dot.active {
	width: 28rpx;
	background: #fff;
}
.guide-btn {
	margin-top: 30rpx;
	width: 590rpx;
	height: 104rpx;
	background: linear-gradient(135deg, #FF5267 0%, #FF233C 100%);
	border-radius: 52rpx;
	font-family: PingFangSC, PingFang SC;
	font-weight: 600;
	font-size: 40rpx;
	color: #FFFFFF;
	line-height: 104rpx;
	text-align: center;
}
.guide-close {
	width: 60rpx;
	height: 60rpx;
	margin-top: 30rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}
</style>
