import { createRouter, createWebHistory, RouteRecordRaw, createWebHashHistory } from 'vue-router'
import { defineAsyncComponent } from 'vue'
import store from "@/store"
import { getCookie, setCookie } from "@/tool/cookie";
const _import = (path: string) => defineAsyncComponent(() => import(`../views/${path}.vue`));
const _import_component = (path : string) => defineAsyncComponent(() => import(`../component/${path}`));
const _import_custom = (path : string) => defineAsyncComponent(() => import(`../views/${path}`));

const routes: Array<RouteRecordRaw> = [
	{
		path: "/",
		// redirect重定向
		// redirect: "/login"
		// redirect: "/upgrade"
		name: "HomeRecommend",
		component: _import('HomeRecommend'),
	},
	{
		path: '/login',
		name: 'login',
		component: _import('LoginPage')
	},
	// {
	// 	path: '/register',
	// 	name: 'register',
	// 	component: _import('RegisterPage')
	// },
	{
		path: '/upgrade',
		name: 'upgrade',
		component: _import('Upgrade'),
	},
	// {
	// 	path: '/home',
	// 	name: 'home',
	// 	component: _import('HomeView')
	// },
	{
		path: '/home',
		name: 'home',
		component: _import('HomeMain'),
		meta:{keepAlive:true},
		children:[
			{
				path: "",
				name:'""',
				redirect: "home/homePage"
			},
			{
				path:'homePage',
				name:'homePage',
				component: _import_custom('HomeView/HomeView.vue'),
					meta:{keepAlive:true},
				},{
				path:'library',
				name:'library',
				component: _import_custom('HomeView/library.vue'),
			},{
				path:'history',
				name:'history',
				component: _import_custom('HomeView/history.vue'),
			},{
				path:'works',
				name:'works',
				component: _import_custom('HomeView/Works.vue'),
			},
		]
	},
	{
		path: '/demo',
		name: 'demo',
		component: _import('Demo')
	},
	// {
	// 	path: '/history',
	// 	name: 'history',
	// 	component: _import('history')
	// },
	// {
	// 	path: '/testClickData',
	// 	name: 'testClickData',
	// 	component: _import('TestClickData')
	// },
	{
		path: '/administrator',
		name: 'administrator',
		component: _import('Administrator'),
		children:[
			{
				path:'allUser',
				name:'allUser',
				component: _import_component('Administrator/allUser.vue'),
			},
			{
				path:'testClickData',
				name:'testClickData',
				component: _import_component('Administrator/TestClickData.vue'),
			},
			{
				path:'trialApproval',
				name:'trialApproval',
				component: _import_component('Administrator/trialApproval.vue'),
			},
		]
	},
	{
		path: '/paySucceed',
		name: 'paySucceed',
		component: _import('paySucceed')
	},
	// {
	// 	path: '/library',
	// 	name: 'library',
	// 	component: _import('library')
	// },
	// {
	// 	path: '/trialApproval',
	// 	name: 'trialApproval',
	// 	component: _import('trialApproval')
	// },
	{
		path: '/setIdentification',
		name: 'setIdentification',
		component: _import('setIdentification')
	},
	{
		path: '/feedbackSurvey',
		name: 'feedbackSurvey',
		component: _import('feedbackSurvey'),
	},
	{
		path: '/feedbackSurveyCN',
		name: 'feedbackSurveyCN',
		component: _import('feedbackSurveyCN'),
	},
	{
		path: '/404',
		name: '404',
		component: _import('404')
	},
]

const router = createRouter({
	history: createWebHistory(process.env.BASE_URL),
	//   history: createWebHashHistory(),
	routes
})
// 刷新保存数据-
let state:any = store.state
window.addEventListener("beforeunload", (e) => {
	sessionStorage.setItem(
		"vuex_setSystemUser",
		JSON.stringify(state.UserHabit.systemUser.value)
	);
});
var vuex_setSystemUser:any = sessionStorage.getItem("vuex_setSystemUser");
if (vuex_setSystemUser == 0 || vuex_setSystemUser == 1) {
	store.commit("setSystemUser", JSON.parse(vuex_setSystemUser));
	sessionStorage.removeItem("vuex_setSystemUser");
}

router.beforeEach((to, from, next) => {
	
	// 	系统维护
    // let upgradeList = ['/feedbackSurvey','/feedbackSurveyCN']//指定页面系统维护也可以访问
	// const toName = to.name === 'upgrade';
	// if(upgradeList.indexOf(to.path) > -1){
	// 	next();
	// }else{
	// 	if (toName) {
	// 		next();
	// 	} else {
	// 		next({ name: 'upgrade' });
	// 	}
	// }
	// return
	// 检查路由是否存在
	// 机房用户
	let userInfo = JSON.parse(getCookie("userInfo") as any);
	let murmurStr: any = localStorage.getItem('murmurStr')
	let getIsMurmur: any = getCookie("isMurmur")
	let token = getCookie("token");
	let isMurmur = JSON.parse(getIsMurmur)
    let routeList = ['/testClickData','/trialApproval']//指定页面需要指定id才能进入
    let userIdList = [88,6,46,31,73,83,87]
    let isSystemUserRouteList = ['/login','/']//指定页面

	let systemUser = state.UserHabit.systemUser
	const routeExists = router.getRoutes().some(({ name }) =>{
		if(name){
			return  name === to.name
		}else{
			return false
		}
	});
	if(systemUser.value == 0){
		if(isSystemUserRouteList.indexOf(to.path) > -1){
			next();
		}else{
			next('/');
		}
		return
	}
	if (routeExists) {
		if (isMurmur && murmurStr && token) {
			const toName = to.name === 'login';
			if (toName) {
				next({ name: '/homePage' });
			} else {
				next();
			}
		} else {
			if (routeList.indexOf(to.path) > -1 ) {
				
				if(userIdList.indexOf(userInfo.userId) > -1){
					next();
				}else{
					next({ name: '/404' });
				}
			}else{
				next();
			}
				// 如果页面存在，正常跳转
		}
	} else {
		// 如果页面不存在，可以跳转到404页面或者其他页面
		next('/404');
	}


});
export default router
