<template>
	<page-meta :page-style="themeColor"></page-meta>
	<view :style="{ backgroundColor: bgColor, minHeight: openBottomNav ? 'calc(100vh - 55px)' : '' }" class="page-img">
		<view class="site-info-box" v-if="$util.isWeiXin() && followOfficialAccount && followOfficialAccount.isShow && wechatQrcode">
			<view class="site-info">
				<view class="img-box" v-if="siteInfo.logo_square">
					<image :src="$util.img(siteInfo.logo_square)" mode="aspectFill"></image>
				</view>
				<view class="info-box" :style="{ color: '#ffffff' }">
					<!-- 新增：多语言 - 站点名称 -->
					<text class="font-size-base">{{ $lang('common.siteName') || siteInfo.site_name }}</text>
					<!-- 新增：多语言 - 公众号欢迎语 -->
					<text>{{ $lang('common.welcomeMsg') || followOfficialAccount.welcomeMsg }}</text>
				</view>
			</view>
			<!-- 新增：多语言 - 关注公众号按钮 -->
			<view class="dite-button" @click="officialAccountsOpen">{{ $lang('common.followOfficialAccount') }}</view>
		</view>

		<diy-index-page v-if="topIndexValue" ref="indexPage" :value="topIndexValue" :bgUrl="bgUrl" :scrollTop="scrollTop" :diyGlobal="diyData.global" class="diy-index-page">
			<template v-slot:components>
				<diy-group ref="diyGroup" v-if="diyData.value" :diyData="diyData" :scrollTop="scrollTop" :haveTopCategory="true" :followOfficialAccount="followOfficialAccount"/>
			</template>
			<template v-slot:default>
				<ns-copyright v-show="isShowCopyRight"/>
			</template>
		</diy-index-page>

		<view v-else class="bg-index" :style="{ backgroundImage: backgroundUrl, paddingTop: paddingTop, marginTop: marginTop }">
			<diy-group ref="diyGroup" v-if="diyData.value" :diyData="diyData" :scrollTop="scrollTop" :followOfficialAccount="followOfficialAccount"/>
			<ns-copyright v-show="isShowCopyRight"/>
		</view>

		<template v-if="adv.advshow != -1">
			<view @touchmove.prevent.stop>
				<uni-popup ref="uniPopupWindow" type="center" class="wap-floating" :maskClick="false">
					 <view class="small-bot">
						<swiper autoplay="true" :circular="true" indicator-active-color="#fff" indicator-color="rgba(255,255,255,0.6)" :indicator-dots="true" interval="3000">
							<swiper-item v-for="(item, index) in adv.list" :key="index">
								<image class="slide-image"  @click="$util.diyRedirectTo(item.link)" height="100%" :src="$util.img(item.imageUrl)" width="100%"></image>
							</swiper-item>
						</swiper>
						<view bindtap="adverclose" class="small-bot-close" @click="closePopupWindow">
							<i class="iconfont icon-round-close" style="color:#fff"></i>
						</view>
					</view>
				</uni-popup>
			</view>
		</template>

		<!-- 底部tabBar -->
		<view class="page-bottom" v-if="openBottomNav">
			<diy-bottom-nav @callback="callback" :name="name"/>
		</view>

		<!-- 关注公众号弹窗 -->
		<view @touchmove.prevent class="official-accounts-inner" v-if="wechatQrcode">
			<uni-popup ref="officialAccountsPopup" type="center">
				<view class="official-accounts-wrap">
					<image class="content" :src="$util.img(wechatQrcode)" mode="aspectFit"></image>
					<!-- 新增：多语言 - 关注公众号提示 -->
					<text class="desc">{{ $lang('common.followMoreInfo') }}</text>
					<text class="close iconfont icon-round-close" @click="officialAccountsClose"></text>
				</view>
			</uni-popup>
		</view>

		<!-- 收藏 -->
		<uni-popup ref="collectPopupWindow" type="top" class="wap-floating wap-floating-collect">
			<view v-if="showTip" class="collectPopupWindow" :style="{ marginTop: (collectTop + statusBarHeight) * 2 + 'rpx' }">
				<image :src="$util.img('public/uniapp/index/collect2.png')" mode="aspectFit"/>
				<!-- 新增：多语言 - 我知道了 -->
				<text @click="closeCollectPopupWindow">{{ $lang('common.iKnow') }}</text>
			</view>
		</uni-popup>

		<!-- 选择门店弹出框 -->
		<view @touchmove.prevent.stop class="choose-store">
			<uni-popup ref="chooseStorePopup" type="center" :maskClick="false" class="choose-store">
				<view class="choose-store-popup">
					<!-- 新增：多语言 - 请确认门店 -->
					<view class="head-wrap" @click="closeChooseStorePopup">{{ $lang('common.confirmStore') }}</view>
					<view class="position-wrap">
						<text class="iconfont icon-dizhi"></text>
						<text class="address">{{ currentPosition }}</text>
						<view class="reposition" @click="reposition" v-if="globalStoreConfig && globalStoreConfig.is_allow_change == 1">
							<text class="iconfont icon-dingwei"></text>
							<!-- 新增：多语言 - 重新定位 -->
							<text>{{ $lang('common.reposition') }}</text>
						</view>
					</view>
					<view class="store-wrap" v-if="nearestStore">
						<!-- 新增：多语言 - 当前门店 -->
						<text class="tag">{{ $lang('common.currentStore') }}</text>
						<view class="store-name">{{ nearestStore.store_name }}</view>
						<view class="address">{{ nearestStore.show_address }}</view>
						<view class="distance" v-if="nearestStore.distance">
							<text class="iconfont icon-dizhi"></text>
							<text>{{ nearestStore.distance > 1 ? nearestStore.distance + 'km' : nearestStore.distance * 1000 + 'm' }}</text>
						</view>
					</view>
					<!-- 新增：多语言 - 确认进入 -->
					<button type="primary" @click="enterStore">{{ $lang('common.confirmEnter') }}</button>
					<view class="other-store" @click="chooseOtherStore" v-if="globalStoreConfig && globalStoreConfig.is_allow_change == 1">
						<!-- 新增：多语言 - 选择其他门店 -->
						<text>{{ $lang('common.chooseOtherStore') }}</text>
						<text class="iconfont icon-right"></text>
					</view>
				</view>
			</uni-popup>
		</view>
		<!-- 悬浮按钮组件（已包含语言切换按钮） -->
		<hover-nav></hover-nav>
		<!-- #ifdef MP-WEIXIN -->
		<privacy-popup ref="privacyPopup"></privacy-popup>
		<!-- #endif -->
        <to-top v-if="showTop" @toTop="scrollToTopNative()"></to-top>
		<ns-login ref="login"></ns-login>

		<!-- 👇 删除：重复的语言切换按钮（hover-nav里已有） -->
	</view>
</template>

<script>
	import uniPopup from '@/components/uni-popup/uni-popup.vue';
	import nsNavbar from '@/components/ns-navbar/ns-navbar.vue';
	import diyJs from '@/common/js/diy.js';
	import indexJs from './public/js/index.js';
    import toTop from '@/components/toTop/toTop.vue';
    import scroll from '@/common/js/scroll-view.js';
	// 👇 保留引入，但无需重复定义切换方法（hover-nav已处理）
	import langUtil from '@/common/js/lang.js';

	export default {
		components: {
			uniPopup,
			nsNavbar,
            toTop
		},
		mixins: [diyJs, scroll, indexJs],
		data() {
			return {
				// 新增：标记是否已监听语言事件
				langListenerAdded: false
			};
		},
		onLoad() {
			// 新增：监听语言切换事件，刷新首页DIY数据
			this.addLangListener();
		},
		onUnload() {
			// 新增：页面销毁时移除监听
			this.removeLangListener();
		},
		methods: {
			// 新增：添加语言监听
			addLangListener() {
				if (!this.langListenerAdded) {
					uni.$on('lang-changed', this.handleLangChange);
					this.langListenerAdded = true;
				}
			},
			// 新增：移除语言监听
			removeLangListener() {
				if (this.langListenerAdded) {
					uni.$off('lang-changed', this.handleLangChange);
					this.langListenerAdded = false;
				}
			},
			// 新增：语言切换后刷新首页数据
			handleLangChange() {
				// 重新请求首页DIY数据，确保内容同步为新语言
				if (this.getDiyInfo) {
					this.getDiyInfo(); // 调用首页获取DIY数据的方法
				}
				// 刷新多语言配置
				this.$langConfig?.refresh();
			},
			// 👇 删除：重复的toggleLang方法（hover-nav里已有）
		}
	};
</script>

<style lang="scss">
	@import '@/common/css/diy.scss';
	@import './public/css/index.scss';
	
	.small-bot {
	    width: 100%;
	    height: 100%;
	    position: fixed;
	    top: 0;
	    bottom: 0;
	    left: 0;
	    right: 0;
	    z-index: 122;
	}
	
	.small-bot swiper {
	    width: 560rpx;
	    height: 784rpx;
	    margin: 120rpx auto 0;
	    border-radius: 10rpx;
	    overflow: hidden;
	}
	
	.small-bot swiper .slide-image {
	    width: 100%;
	    height: 100%;
	}
	
	.small-bot-close {
	    width: 66rpx;
	    height: 66rpx;
	    border-radius: 50%;
	    text-align: center;
	    line-height: 64rpx;
	    font-size: 64rpx;
	    color: #fff;
	    margin: 30rpx auto 0;
	}
	
	.small-bot-close i {
	    font-size: 60rpx;
	}
</style>
<style scoped>
	.swiper /deep/ .uni-swiper-dots-horizontal {
		left: 40%;
		bottom:40px
	}
	.wap-floating>>>.uni-popup__wrapper.uni-custom .uni-popup__wrapper-box {
		background: none !important;
	}

	.choose-store>>>.goodslist-uni-popup-box {
		width: 80%;
	}

	/deep/.diy-index-page .uni-popup .uni-popup__wrapper-box {
		border-radius: 0;
	}

	/deep/ .placeholder {
		height: 0;
	}

	/deep/::-webkit-scrollbar {
		width: 0;
		height: 0;
		background-color: transparent;
		display: none;
	}

	/deep/ .sku-layer .uni-popup__wrapper.uni-custom .uni-popup__wrapper-box {
		max-height: unset !important;
	}
    
    /deep/ .mescroll-totop {
        right: 24rpx !important; 
        bottom: 182rpx !important;
    }

	/* 👇 删除：重复的语言按钮样式（hover-nav里已有） */
</style>