<template>
	<el-config-provider :size="getGlobalComponentSize" :locale="zhCn" :message="messageConfig" :button="buttonConfig">
		<router-view v-show="setLockScreen" />
		<LockScreen v-if="themeConfig.isLockScreen" />
		<Setings ref="setingsRef" v-show="setLockScreen" />
		<CloseFull v-if="!themeConfig.isLockScreen" />
	</el-config-provider>
</template>

<script lang="ts" name="app" setup>
import { computed, ref, onBeforeMount, onMounted, onUnmounted, nextTick, watch, reactive, defineAsyncComponent } from 'vue';
import { useRoute } from 'vue-router';
import zhCn from 'element-plus/es/locale/lang/zh-cn';
import { storeToRefs } from 'pinia';
import { useTagsViewRoutes } from '/@/stores/tagsViewRoutes';
import { useThemeConfig } from '/@/stores/themeConfig';
import other from '/@/utils/other';
import checkUpdate from '/@/utils/checkUpdate';
import mittBus from '/@/utils/mitt';
import { Session, Local } from '/@/utils/storage';
import setIntroduction from '/@/utils/setIconfont';
import { loginPageInfo } from '/@/api/login';
import { getImageUrl } from '/@/utils/tools';
// 引入组件
const LockScreen = defineAsyncComponent(() => import('/@/layout/lockScreen/index.vue'));
const Setings = defineAsyncComponent(() => import('/@/layout/navBars/breadcrumb/setings.vue'));
const CloseFull = defineAsyncComponent(() => import('/@/layout/navBars/breadcrumb/closeFull.vue'));

const setingsRef = ref();
const route = useRoute();
const stores = useTagsViewRoutes();
const storesThemeConfig = useThemeConfig();
const { themeConfig } = storeToRefs(storesThemeConfig);

// 设置锁屏时组件显示隐藏
const setLockScreen = computed(() => {
	// 防止锁屏后，刷新出现不相关界面
	// https://gitee.com/lyt-top/vue-next-admin/issues/I6AF8P
	return themeConfig.value.isLockScreen ? themeConfig.value.lockScreenTime > 1 : themeConfig.value.lockScreenTime >= 0;
});

// 可同时显示的消息最大数量
const messageConfig = reactive({
	max: 3,
});
// 自动在两个中文字符之间插入空格
const buttonConfig = reactive({
	autoInsertSpace: false,
});
// 获取全局组件大小
const getGlobalComponentSize = computed(() => {
	return other.globalComponentSize();
});
// 布局配置弹窗打开
const openSetingsDrawer = () => {
	setingsRef.value.openDrawer();
};
// 设置初始化，防止刷新时恢复默认
onBeforeMount(() => {
	// 设置批量第三方 icon 图标
	setIntroduction.cssCdn();
	// 设置批量第三方 js
	setIntroduction.jsCdn();
});
// 页面加载时
onMounted(() => {
	nextTick(async () => {
		// 获取登录页的背景图和系统名称等
		const res: any = await loginPageInfo();
		themeConfig.value.globalTitle = res.result.sysName; // 标题名称
		themeConfig.value.loginImage = res.result.loginImage ? `url${res.result.loginImage}` : `url(${getImageUrl('login/bg.png')})`; // 登录页背景图
		Local.set('themeConfig', themeConfig.value);
		// 开发环境不提示
		if (import.meta.env.VITE_MODE_NAME != 'development') {
			// 监听是否更新
			checkUpdate();
		}
		// 监听布局配置弹窗点击打开
		mittBus.on('openSetingsDrawer', () => {
			openSetingsDrawer();
		});

		// 获取缓存中的布局配置
		if (Local.get('themeConfig')) {
			storesThemeConfig.setThemeConfig(Local.get('themeConfig'));
			document.documentElement.style.cssText = Local.get('themeConfigStyle');
		}
		// 获取缓存中的全屏配置
		if (Session.get('isTagsViewCurrenFull')) {
			stores.setCurrenFullscreen(Session.get('isTagsViewCurrenFull'));
		}
		// 解决火狐拖动打开新窗口
		document.body.ondrop = (event) => {
			event.preventDefault();
			event.stopPropagation();
		};
	});
});
// 页面销毁时，关闭监听布局配置/i18n监听
onUnmounted(() => {
	mittBus.off('openSetingsDrawer', () => {});
});
// 监听路由的变化，设置网站标题
watch(
	() => route.path,
	() => {
		other.useTitle();
	},
	{
		deep: true,
	}
);
</script>
