<script setup>
import { ref, onMounted, nextTick, watch, onBeforeUnmount, getCurrentInstance } from 'vue';
import { useI18n } from "vue-i18n";
const { locale } = useI18n();

import {
	onLoad,
	onShow,
	onLaunch
} from '@dcloudio/uni-app'
import {
	updateRoute
} from "@/hooks/useRoute";
import useGlobalStore from "@/stores/use-global-store";
import { useAppUpdate } from '@/hooks/useAppUpdate'
import liveChat from '@/utils/liveChat';
const { checkUpdate } = useAppUpdate()
const globalStore = useGlobalStore()
onLoad((options) => {
	updateRoute();
	checkUpdate()
})
onShow((options) => {
	updateRoute();
	checkUpdate()
})
onLaunch((options) => {
	updateRoute();
	checkUpdate()
})
const adjustLiveChatPosition = () => {
	// #ifdef H5
	const footerBody = document.getElementById('footer-body');
	if (footerBody) {
		liveChat.adjustPosition(footerBody.offsetHeight);
	}
	// #endif
}
watch(locale, () => {
	console.log(locale.value, 23123)
	const currentPath = route.path;
	menu.value.forEach((item, index) => {
		if (item.children) {
			const isActive = item.children.some(child => child.path.includes(currentPath));
			menu.value[index].isOpenMenu = isActive;
			if (isActive) {
				nextTick(() => {
					updateSubmenuHeight(index);
				});
			}
		}
	});
}, { immediate: true })
onBeforeUnmount(() => {
	// #ifdef H5
	if (typeof window !== 'undefined') {
		window.removeEventListener('resize', adjustLiveChatPosition)
	}
	// #endif
})
onMounted(() => {
	const sysInfo = uni.getSystemInfoSync();
	globalStore.setBarHeight(sysInfo.statusBarHeight);

	// ---------- 新增 H5 端专属初始化 ----------
	// 仅在 H5 端执行（通过环境判断）
	// #ifdef H5
	if (typeof window !== 'undefined') {
		const instance = getCurrentInstance()
		if (instance) {
			window.vm = instance.proxy
		}

		// 调用一次调整位置
		adjustLiveChatPosition()

		// 监听窗口大小变化，动态调整
		window.addEventListener('resize', adjustLiveChatPosition)
	}
	// #endif
});
</script>

<style>
/*每个页面公共css */
</style>

<style lang="scss">
/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
@import "uview-plus/index.scss";
@import "@/static/scss/global/global.scss";
@import "/static/scss/style.scss";

/* 让整个项目文字都能选中 */
* {
	-webkit-user-select: text !important;
	user-select: text !important;
}

/* 修复滚动层无法选中 */
view,
text,
div,
span {
	-webkit-user-select: text !important;
	user-select: text !important;
}

:deep(.u-toolbar__wrapper__confirm) {
	font-size: 20px !important;
}

:deep(.u-toolbar__wrapper__cancel) {
	font-size: 20px !important;
}

.page {
	/* padding: 31px 31px 110px 31px; */
	box-sizing: border-box;
	/* background: var(--main-bg); */
}

html {
	font-size: 32px !important;
}

uni-page-body {
	height: 100%;
}
</style>
