import {
	RouterMount,
	createRouter,
	runtimeQuit
} from 'uni-simple-router';

//配置白名单
let WHiTE_LIST=['login','my-camera','auth','test']

//员工页面,只可以员工权限的用户访问
let STAFF=['auth','editPhone']

//企业页面，只可以企业权限的用户访问
let ENTERPRISE=['staffAuditList',
				'staffAuditDetail',
				'smoke-alarm',
				'gas-alarm',
				'fire-hydrant',
				'changePass'
				]

let first = null;
const router = createRouter({
	platform: process.env.VUE_APP_PLATFORM,  
	APP:{
		animation:{
			animationType:'slide-in-top',
			animationDuration:300
		}
	},
	routerBeforeEach:(to, from, next) => {
		//如果是白名单的页面，直接放行
		if (WHiTE_LIST.includes(to.name)) {
			next();
			return
		}
		//如果是从登陆页进入的认证页，放行；若是从其他页面进入的认证页，则需要验证是否登陆验证
		if (from.name=='login'&&to.name=='auth') {
			next()
			return
		}
		
		//拦截类型，方便提示
		//0：未登陆认证拦截
		//1：允许通行
		//2：权限不足拦截（员工类型访问企业类型的页面）
		//3: 权限不足拦截（企业类型访问员工类型的页面）
		let flag=0
		//登陆类型
		let loginType =uni.getStorageSync("loginType")
		
		if (loginType) {
			if (loginType=='staff'&&ENTERPRISE.includes(to.name)) {
				//员工类型访问企业类型的页面，拦截
				flag=2
			}else if(loginType=='enterprise'&&STAFF.includes(to.name)){
				//企业类型访问员工类型的页面，拦截
				flag=3
			}else{
				//允许通行
				flag=1
			}
		}else{
			flag=0
		}
		switch (flag){
			case 0: //未登录认证
				next({
					name: 'login',
					 params: { fullPath: to.fullPath },
					NAVTYPE: 'replaceAll'
				});
				break;
			case 1://允许通行
				next()
				break;
			case 2: //员工类型访问企业类型的页面
				uni.showModal({
					title: '提示',
					content: '权限不足，员工类型无法访问企业类型的页面',
					showCancel:false,
					success: (res)=>{
						next({
							name: 'index',
							NAVTYPE: 'pushTab'
						});
					}
				});
				break;
			case 3://企业类型访问员工类型的页面
				uni.showModal({
					title: '提示',
					content: '权限不足，企业类型无法访问员工类型的页面',
					showCancel:false,
					success: (res)=>{
						next({
							name: 'index',
							NAVTYPE: 'pushTab'
						});
					}
				});
				break;
			default:
				break;
		}
	},
	routerAfterEach:(to, from) => {
	   console.log('--------routerAfterEach----')
	},
	routerErrorEach:({type,msg})=>{
		console.log({type,msg})
		// #ifdef APP-PLUS
			if(type===3){
				router.$lockStatus=false;
				runtimeQuit();
			}
		// #endif
	},
	routes: [
		...ROUTES,
		{
		  path: '*',
		  redirect:(to)=>{
			  return {name:'404'}
		  }
		},
	]
});
let count=0;
router.beforeEach((to, from, next) => {
	console.log("from:",from)
	console.log("to:",to)
	// if(count==0){
	// 	next({
	// 		path:'/pages/login/login',
	// 		NAVTYPE:'replaceAll'
	// 	})
	// }else{
	// 	next();
	// }
	next();
	count++;
});
router.afterEach((to, from, next) => {
	console.log('afterEach---跳转结束')
});

export {
	router,
	RouterMount
}