/**
 * 说明
 * 此文件为路由配置入口文件
 * 路由子模块请按模块拆分到同文件夹下其他文件中
 * meta:{
 * noHead:true pad端没有顶部头部
 * hasBackTop:true 有回到顶部
 * noBack:true pad端没有返回上一家
 * }
 */
import { createRouter, createWebHistory } from "vue-router";
import {useCachedViewsStore} from '@/store/modules/cachedViews'
import {useAuthBtnStore} from '@/store/modules/authBtn'
import {usePublicSettingStore} from '@/store/modules/publicSetting'

import { pptRoutes } from "./ppt";
import {pptENRoutes} from './pptEn'
import {myETARoutes} from './myETA'
import {reportRoutes} from './report'
import {reportEnRoutes} from './reportEn'
import {chartETARoutes} from './chartETA'
import {dataEDBRoutes} from './dataEDB'
import {sheetListRoutes} from './sheetList'
import { storeToRefs } from "pinia";

const routes = [
  	{
		path: "/login",
		name: "Login",
		component: () => import("@/views/Login.vue"),
		meta: { title: "登录" },
  	},
  	
	{
		path:"/",
		name:"Layouts",
		redirect: "/tabbar/home",
		component:()=>import('@/layouts/Index.vue'),
		children:[
			// tabbar模块
			{
				path: "/tabbar",
				name: "Tabbar",
				redirect: "/tabbar/home",
				component: () => import("@/views/tabbar/Index.vue"),
				meta: { title: "移动ETA" },
				children: [
					{
						path: "/tabbar/home",
						name: "Home",
						component: () => import("@/views/tabbar/Home.vue"),
						meta: { title: "移动ETA",noBack:true },
					},
					{
						path: "/tabbar/user",
						name: "User",
						component: () => import("@/views/tabbar/User.vue"),
						meta: { title: "移动ETA",noBack:true },
					},
				],
			},

			// ppt模块
			...pptRoutes,
			//英文ppt模块
			...pptENRoutes,
			//myETA模块
			...myETARoutes,
			// 中文研报模块
			...reportRoutes,
			// 英文研报模块
			...reportEnRoutes,
			//ETA图库模块
			...chartETARoutes,
			//ETA指标库模块
			...dataEDBRoutes,
			//表格模块
			...sheetListRoutes
		]
	},
	
	
	{
		path: "/:pathMatch(.*)",
		name: "error",
		component: () => import("@/views/404.vue"),
		meta: { title: "404" },
	},
];

const router = createRouter({
	history: createWebHistory(import.meta.env.VITE_APP_BASE_URL),
  	routes,
	scrollBehavior(to, from, savedPosition) {
		if (savedPosition && to.meta.keepAlive) {
			return savedPosition;
		} else {
			return new Promise((resolve) => {
				setTimeout(() => {
					resolve({ left: 0, top: 0 });
				}, 0);
			});
		}
	},
});

// 设置keepalive
function setKeeplive(to){
	const cachedViewsStore=useCachedViewsStore()
	if(to.meta.keepAlive){
		let name=to.name
		if(name==='PPTENSearch'){
			name='PPTSearch'
		}
		if(name==='PPTENDetail'){
			name='PPTDetail'
		}
		cachedViewsStore.addCaches(name)
	}
}

router.beforeEach(async(to, from, next) => {
	setKeeplive(to)
    const authBtnStore = useAuthBtnStore()
	const publicSettingStore = usePublicSettingStore()
	if(to.path!='/login'){
		authBtnStore.getAuthList()
		if(Object.keys(publicSettingStore.publicSetting).length===0){
			publicSettingStore.getPublicSetting()
		}
	}
	document.title = to.meta.title;
  	next();
});


export default router;
