<!-- 首页，支持店铺装修 -->
<template>
	<view v-if="template">

		<s-layout title="首页" navbar="normal" tools="search" tabbar="/pages/index/index" :bgStyle="template.page"
			:navbarStyle="template.navigationBar" navbarbackgroundColor="transparent" onShareAppMessage
			@search="(e) => { console.log(e,'eeeeeeeeeeee') }" headerBtns='headerBtns'
			:backgroundColor="stateData.backgroundColor" opacityBgUi='ll' :navBg="stateData.navBg">
			<!-- <view class="new-bg" :style="{backgroundImage:'url('+ stateData.backgroundImage +')'}"> -->
			<view class="new-bg" >
				<image class="newbg-img" :src="stateData.backgroundImage" :style="{width:'150%',height:'150%'}"></image>
			</view>

			<s-block v-for="(item, index) in template.components" :key="index" :styles="item.property.style">
				<s-block-item :type="item.id" :data="item.property" :styles="item.property.style" :fl="true" />
			</s-block>
		</s-layout>
	</view>
</template>

<script setup>
	import {
		computed,
		ref,
		nextTick,
		onMounted,
		watchEffect,
		reactive,
		watch
	} from 'vue';
	import {
		onLoad,
		onPageScroll,
		onPullDownRefresh
	} from '@dcloudio/uni-app';
	import sheep from '@/sheep';
	const app = sheep.$store('app');


	import $share from '@/sheep/platform/share';
	// 隐藏原生tabBar
	uni.hideTabBar();



	const template = computed(() => sheep.$store('app').template?.home);
	// 在此处拦截改变一下首页轮播图 此处先写死后期复活 放到启动函数里
	// (async function() {
	// console.log('原代码首页定制化数据',template)
	// let {
	// 	data
	// } = await index2Api.decorate();
	// console.log('首页导航配置化过高无法兼容',JSON.parse(data[1].value))
	// 改变首页底部数据 但是没有通过数组id获取商品数据接口
	// let {
	// 	data: datas
	// } = await index2Api.spids();
	// template.value.data[9].data.goodsIds = datas.list.map(item => item.id);
	// template.value.data[0].data.list = JSON.parse(data[0].value).map(item => {
	// 	return {
	// 		src: item.picUrl,
	// 		url: item.url,
	// 		title: item.name,
	// 		type: "image"
	// 	}
	// })
	// }())
	const stateData = reactive({
		imgindexArr: [],
		backgroundImage: '',
		backgroundColor: 'transparent',
		navBg: true
	});

	watch(
		() => template.value,
		(newTemplate) => {
			if (newTemplate?.components?.[2]?.property?.items) {
				console.log('Template is now available:', newTemplate);
				stateData.imgindexArr = newTemplate.components[2].property.items;
			} else {
				console.warn('Template format is invalid or missing necessary data.');
			}
		}, {
			immediate: true,
			once: true
		}
	);

	// 响应式订阅 Vuex（或 Pinia）状态变化
	app.$subscribe((mutation, state) => {
		// console.log('State changed:', state.indexSwiperCurr, '图片数组:', stateData.imgindexArr);

		const currentIndex = state.indexSwiperCurr || 0;
		const currentItem = stateData.imgindexArr?.[currentIndex];

		if (currentItem?.imgUrl) {
			stateData.backgroundImage = currentItem.imgUrl;
		} else {
			console.warn('当前索引的图片数据无效:', currentIndex);
			stateData.backgroundImage = ''; // fallback 或保留旧图
		}
	});

	onLoad((options) => {
		// #ifdef MP
		// 小程序识别二维码
		if (options.scene) {
			const sceneParams = decodeURIComponent(options.scene).split('=');
			console.log('sceneParams=>', sceneParams);
			options[sceneParams[0]] = sceneParams[1];
		}
		// #endif

		// 预览模板
		if (options.templateId) {
			sheep.$store('app').init(options.templateId);
		}

		// 解析分享信息
		if (options.spm) {
			$share.decryptSpm(options.spm);
		}

		// 进入指定页面(完整页面路径)
		if (options.page) {
			sheep.$router.go(decodeURIComponent(options.page));
		}
	});

	// 下拉刷新
	onPullDownRefresh(() => {
		sheep.$store('app').init();
		setTimeout(function() {
			uni.stopPullDownRefresh();
		}, 800);
	});



	onPageScroll((e) => {
		if (e.scrollTop > 0) {
			console.log('不在顶部')
			stateData.backgroundColor = 'white'
		} else {
			console.log('在顶部')
			stateData.backgroundColor = 'transparent'
		}
	});
</script>

<style lang="scss">
	.new-bg {
		// background: #e93422;
		filter: blur(16px) opacity(.8);
		background-size: auto;
		background-position: bottom;
		width: 100vw;
		overflow:hidden;
		// transform: scale(1.1);
		height: 550rpx;
		position: absolute;
		top: -190rpx;
		transition: opacity .5s ease;
		.newbg-img{
			position: absolute;
			bottom: 50rpx;
			
		}
	}
	// .new-bg::after{
	// content:"";
	// display: block;
	// width: 100%;
	// height: 400rpx;
	// background:rgba(17,17,17,.2);
	// z-index: 1;
	// }
</style>