import Vue from "vue";
import VueRouter from "vue-router";
import store from '@/store'
Vue.use(VueRouter);

const routes = [
	{
		path:'/',
		name:'login',
		meta: {
			title: '冷链验证报告管理系统线上版本',
		},
		component: () => import('@/views/login/login.vue')
	},
	{
		path:'/index',
		name:'index',
		meta: {
			title: '首页',
		},
		component: () => import('@/views/layout/index.vue'),
		children: [{
			path: '/home',
			name: 'home',
			icons:'tubiao-',
			component: () => import('@/views/home/home.vue'),
			meta: {
				title: '工作首页',
				authority:0//0全显示，1管理，2用户
			},
		},{
			path: '/CompanyAccount',
			name: 'CompanyAccount',
			icons:'gongsixinxi',
			component: () => import('@/views/CompanyAccount/index.vue'),
			meta: {
				title: '公司账户',
				authority:2//0全显示，1管理，2用户
			},
		},{
			path: '/ManagingUsers',
			name: 'ManagingUsers',
			icons:'icon-',
			component: () => import('@/views/ManagingUsers/index.vue'),
			meta: {
				title: '管理用户',
				authority:1//0全显示，1管理，2用户
			},
		},{
			path: '/companyManagement',
			name: 'companyManagement',
			icons:'fuwugongsi',
			component: () => import('@/views/companyManagement/index.vue'),
			meta: {
				title: '公司管理',
				authority:1//0全显示，1管理，2用户
			},
		},{
			path: '/ValidationTemplate',
			name: 'ValidationTemplate',
			icons:'icon_renwumobanguanli',
			component: () => import('@/views/ValidationTemplate/index.vue'),
			meta: {
				title: '验证模板管理',
				authority:1//0全显示，1管理，2用户
			},
		},{
			path: '/calibrationCertificate',
			name: 'calibrationCertificate',
			icons:'duqumoban',
			component: () => import('@/views/calibrationCertificate/index.vue'),
			meta: {
				title: '校准证书管理',
				authority:1//0全显示，1管理，2用户
			},
		}]
	},{
		path: '*',
		component: () => import('@/components/404.vue'),
	}
]

const router = new VueRouter({
	mode: "history",//hash
	base: process.env.BASE_URL,
	routes,
});

//登录拦截//未登录返回到登录界面
router.beforeEach((to, from, next) => {
	to.meta.title ? document.title = to.meta.title : document.title = '宝智达-冷链验证'
	if(store.state.userInfo.token){
		next();
	}else{
		//如果是登录页面路径，就直接next()
		if (to.path === '/') {
			next();
		} else {
			next('/');
		}
	}
	if(to.name=='login'){//退出登录之后清零
		store.state.tabList = [{
			title:'工作首页',
			path:'/home',
			name:'home'
		}],
		store.commit('setuserInfo',{
			username:store.state.userInfo.username,
			password:store.state.userInfo.password,
			token: '',
			value:'2'
		})
		store.state.includeTabs = []
		console.log('login了')
	}
	if(to.name!=-1 && to.name!='login'){
		store.commit('addTab',{
			title:to.meta.title,
			path:to.path,
			name:to.name
		})
	}
})

export default router;