import {
	createRouter,
	createWebHistory
} from 'vue-router'
//引入vuex的状态管理store
import store from './store/index.js'
import axios from 'axios'
import {
	ROOTPATH
} from '/comment/httpUrl'

const routerHistory = createWebHistory()
// 检查用户类型
var userType = localStorage.getItem('userType') ? localStorage.getItem('userType') : 1
//定义路由
const routes = [{
	path: '/',
	name: 'home',
	component: () => import("./views/home.vue"), //引入home页面
	redirect: {
		name: userType == 1 ? 'index' : 'indexCompany'
	},
	children: [{
		path: '/index',
		name: 'index',
		component: () => import("./views/index/index.vue"), //引入首页页面
	}, {
		path: '/indexCompany',
		name: 'indexCompany',
		component: () => import("./views/index/indexCompany.vue"), //引入企业端首页页面
	}, {
		path: '/enterpriseList',
		name: 'enterpriseList',
		component: () => import("./views/enterpriseList/index.vue"), //企业页面
	}, {
		path: '/enterpriseInfo',
		name: 'enterpriseInfo',
		component: () => import("./views/enterpriseList/info.vue"), //企业详情
	}, {
		path: '/addResume',
		name: 'addResume',
		component: () => import("./views/resume/addResume.vue"), //引入添加简历页面
	}, {
		path: '/postInfo',
		name: 'postInfo',
		component: () => import("./views/post/postInfo.vue"), //引入添加简历页面
	}, {
		path: '/resumeInfo',
		name: 'resumeInfo',
		component: () => import("./views/resume/resumeInfo.vue"), //引入简历详情页面
	}, {
		path: '/search',
		name: 'search',
		component: () => import("./views/search/index.vue"), //引入搜索页面
	}, {
		path: '/searchCom',
		name: 'searchCom',
		component: () => import("./views/search/searchCom.vue"), //引入简历搜索页面
	}, {
		path: '/message',
		name: 'message',
		component: () => import("./views/message/index.vue"), //引入消息页面
	}, {
		path: '/setting',
		name: 'setting',
		component: () => import("./views/setting/index.vue"), //引入设置页面
		redirect: {
			name: 'agreement'
		},
		children: [{
			path: '/setting/myReams',
			name: 'myReams',
			component: () => import("./views/setting/myReams.vue"), //引入我的团队页面
			redirect: {
				name: 'myTeam'
			},
			children: [{
				path: '/setting/myTeam',
				name: 'myTeam',
				component: () => import("./views/setting/myTeam.vue"), //引入我的团队页面

			}, {
				path: '/setting/myMoney',
				name: 'myMoney',
				component: () => import("./views/setting/myMoney.vue"), //引入我的收益页面

			}, {
				path: '/setting/getMoney',
				name: 'getMoney',
				component: () => import("./views/setting/getMoney.vue"), //引入提现页面

			}]
		}, {
			path: '/setting/shield',
			name: 'shield',
			component: () => import("./views/setting/shield.vue"), //引入屏蔽公司页面
		}, {
			path: '/setting/blacklist',
			name: 'blacklist',
			component: () => import("./views/setting/blacklist.vue"), //引入黑名单页面
		}, {
			path: '/setting/transference',
			name: 'transference',
			component: () => import("./views/setting/transference.vue"), //引入转让企业页面
		}, {
			path: '/setting/agreement',
			name: 'agreement',
			component: () => import("./views/setting/agreement.vue"), //引入协议页面
		}, {
			path: '/setting/agreementU',
			name: 'agreementU',
			component: () => import("./views/setting/agreementU.vue"), //引入协议页面
		}, {
			path: '/setting/agreementG',
			name: 'agreementG',
			component: () => import("./views/setting/agreementG.vue"), //引入协议页面
		}, {
			path: '/setting/help',
			name: 'help',
			component: () => import("./views/setting/help.vue"), //引入帮助页面
		}, {
			path: '/setting/logoff',
			name: 'logoff',
			component: () => import("./views/setting/logOff.vue"), //引入注销账号页面
		}, {
			path: '/setting/upDataPassWord',
			name: 'upDataPassWord',
			component: () => import("./views/setting/upDataPassWord.vue"), //引入修改密码页面
		}],

	}, {
		path: '/interview',
		name: 'interview',
		component: () => import("./views/interview/interview.vue"), //引入面试记录页面
	}, {
		path: '/recordUser',
		name: 'recordUser',
		component: () => import("./views/record/record.vue"), //引入记录页面
	}, {
		path: '/enterprise',
		name: 'enterprise',
		component: () => import("./views/enterprise/enterprise.vue"), //引入企业中心页面
	}, {
		path: '/money',
		name: 'money',
		component: () => import("./views/money/money.vue"), //引入会员页面
	}, {
		path: '/myPostPush',
		name: 'myPostPush',
		component: () => import("./views/myPostPush/myPostPush.vue"), //引入我的招聘页面
	}, {
		path: '/record',
		name: 'record',
		component: () => import("./views/interview/record.vue"), //引入企业页面
	}, {
		path: '/myCollect',
		name: 'myCollect',
		component: () => import("./views/jilu/myCollect.vue"), //引入企业收藏页面
	}, {
		path: '/jilu',
		name: 'jilu',
		component: () => import("./views/jilu/jilu.vue"), //引入简历记录/浏览记录页面
	}],

}, {
	path: '/login',
	name: 'login',
	component: () => import("./views/login/login.vue") //引入登录页面
}, {
	path: '/mobel',
	name: 'mobel',
	component: () => import("./views/mobel/index.vue"), //引入手机访问页面
}]


// 创建路由器
const router = createRouter({
	history: routerHistory,
	routes: routes,
})
//在全局路由的钩子里把页面滚动到最顶部
router.beforeEach((to, from, next) => {
	const userAgent = navigator.userAgent;
	const isMobileRegex =
		/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i;
	let flag = isMobileRegex.test(userAgent);
	if (flag) { //手机访问
		//判断是否开启跳转移动端
		axios.get(ROOTPATH + '/app/common/type/431').then(res => {
			if (res.data.code == 0) {
				if (res.data.data.value == '是') {
					axios.get(ROOTPATH + '/app/common/type/344').then(ree => {
						if (ree.data.code == 0) {
							// replace
							window.location.href = ree.data.data.value;
						} else {
							next()
						}
					})
				} else {
					//判断是否打开二维码页面
					axios.get(ROOTPATH + '/app/common/type/430').then(ree => {
						// alert(ree.data.data.value)
						if (ree.data.code == 0) {
							if (ree.data.data.value == '是') {
								var meta = document.querySelector('meta[name="viewport"]');
								meta.setAttribute('content',
									'width=device-width, initial-scale=1');
								//如果当前已经是mobel页面，那就直接放行，否则回进入无限循环
								if (to.name == 'mobel') {
									next()
								} else { //如果不是那么就跳转到mobel页面
									next({
										name: 'mobel'
									})
								}
							} else {
								next()
							}
						} else {
							next()
						}
					})
				}
			} else {
				next()
			}
		})

	} else { //pc访问
		if (to.name == 'mobel') {
			if (to.name == 'home') {
				next()
			} else { //如果不是那么就跳转到home页面
				next({
					name: 'home'
				})
			}
		} else {
			document.body.scrollTop = 0;
			document.documentElement.scrollTop = 0;
			//兼容ie
			window.scrollTo(0, 0)
			if (to.name !== 'index' && to.name !== 'login' && to.name !=
				'resumeInfo' && to
				.name != 'postInfo' && to.name !== 'enterpriseList') {
				let token = localStorage.getItem('token')
				if (!token || !/\S/.test(token)) {
					next({
						name: 'login'
					})
				} else {
					let userType = localStorage.getItem('userType')
					if (to.name == 'indexCompany') {
						if (userType == 1) {
							next({
								name: 'index'
							})
						} else {
							next();
						}
					} else {
						next();
					}

				}
			} else {
				let userType = localStorage.getItem('userType')
				if (to.name == 'index') {
					if (userType == 2) {
						next({
							name: 'indexCompany'
						})
						// next();
					} else {
						next();
					}
				} else {
					next();
				}
				// next();

			}
		}

	}

})

export default router