import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../views/Login.vue';
import base64 from '../utils/base64';

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => err);
}

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    // name: 'login',
		redirect: '/login',
    component: login
  },
	{
		path: '/login',
		name: 'login',
		component: login
	},
	{
		path: '/index',
		name: 'index',
		meta: ['user', 'root'],
		// route level code-splitting
		// this generates a separate chunk (about.[hash].js) for this route
		// which is lazy-loaded when the route is visited.
		component: () => import(/* webpackChunkName: "home" */ '../views/Index.vue'),
    children: [
			{
				path: '/home',
				name: 'home',
				component: () => import(/* webpackChunkName: "home" */ '../views/Home.vue'),
				meta: ['user']
			},
			{
				path: '/taskManage',
				name: 'taskManage',
				meta: ['user'],
				component: () => import(/* webpackChunkName: "home" */ '../views/TaskManage.vue')
			},
			{
				path: '/addTask',
				name: 'addTask',
				meta: ['user'],
				component: () => import(/* webpackChunkName: "home" */ '../views/AddTask.vue')
			},
			{
				path: '/departmentTask',
				name: 'departmentTask',
				meta: ['user'],
				component: () => import(/* webpackChunkName: "home" */ '../views/DepartmentTask.vue')
			},
			{
				path: '/unpublishedTask',
				name: 'unpublishedTask',
				meta: ['user'],
				component: () => import(/* webpackChunkName: "home" */ '../views/UnpublishedTask.vue')
			},
			{
				path: '/viewTask',
				name: 'viewTask',
				meta: ['user'],
				component: () => import(/* webpackChunkName: "home" */ '../views/ViewTask.vue')
			},
			{
				path: '/taskDetails',
				name: 'taskDetails',
				meta: ['user'],
				component: () => import(/* webpackChunkName: "home" */ '../views/TaskDetails.vue')
			},
			{
				path: '/personManage',
				name: 'personManage',
				meta: ['user'],
				component: () => import(/* webpackChunkName: "home" */ '../views/PersonManage.vue')
			},
			{
				path: '/personDetails',
				name: 'personDetails',
				meta: ['user'],
				component: () => import(/* webpackChunkName: "home" */ '../views/PersonDetails.vue')
			},
			{
				path: '/personInfo',
				name: 'personInfo',
				meta: ['user'],
				component: () => import(/* webpackChunkName: "home" */ '../views/PersonInfo.vue')
			},
			{
				path: '/personList',
				name: 'personList',
				meta: ['user'],
				component: () => import(/* webpackChunkName: "home" */ '../views/PersonList.vue')
			},
			{
				path: '/personTask',
				name: 'personTask',
				meta: ['user'],
				component: () => import(/* webpackChunkName: "home" */ '../views/PersonTask.vue')
			},
			// {
			// 	path: '/attendanceManage',
			// 	name: 'attendanceManage',
			// 	component: () => import(/* webpackChunkName: "home" */ '../views/AttendanceManage.vue')
			// },
      {
				path: '/adminSetting',
				name: 'adminSetting',
				meta: ['root'],
				component: () => import(/* webpackChunkName: "home" */ '../views/AdminSetting.vue')
      },
			{
				path: '/paramsSetting',
				name: 'paramsSetting',
				meta: ['root'],
				component: () => import(/* webpackChunkName: "home" */ '../views/ParamsSetting.vue')
			},
			{
				path: '/consumerManage',
				name: 'consumerManage',
				meta: ['root'],
				component: () => import(/* webpackChunkName: "home" */ '../views/ConsumerManage.vue')
			},
			{
				path: '/consumerDetails',
				name: 'consumerDetails',
				meta: ['root'],
				component: () => import(/* webpackChunkName: "home" */ '../views/ConsumerDetails.vue')
			},
			{
				path: '/createConsumer',
				name: 'createConsumer',
				meta: ['root'],
				component: () => import(/* webpackChunkName: "home" */ '../views/CreateConsumer.vue')
			},
			{
				path: '/roleManage',
				name: 'roleManage',
				meta: ['root'],
				component: () => import(/* webpackChunkName: "home" */ '../views/RoleManage.vue')
			},
			{
				path: '/authorityManage',
				name: 'authorityManage',
				meta: ['root'],
				component: () => import(/* webpackChunkName: "home" */ '../views/AuthorityManage.vue')
			},
			{
				path: '/departmentManage',
				name: 'departmentManage',
				meta: ['root'],
				component: () => import(/* webpackChunkName: "home" */ '../views/DepartmentManage.vue')
			}
    ]
	},
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
	if (to.path === '/login') {
		next();
	} else {
		if (localStorage.getItem('login') !== null) {
			let role = JSON.parse(base64.decode(localStorage.getItem('login')));
			if (JSON.stringify(role) === '') {
				next('/');
			} else {
				// console.log(to)
				let data;
				if (role.username !== 'root') {
					data = 'user';
				} else {
					data = 'root';
				}
				for (let i = 0; i < to.meta.length; i++) {
					// console.log(to.meta[i]);
					if (data === to.meta[i]) {
						next();
						break;
					} else if (i === to.meta.length - 1) {
						// next({
						// 	path: '/login'
						// })
						break;
					} else {
						next('/');
						break;
					}
				}
				// if(to.matched.some(item => item.meta === data)) {
				//
				// } else {
				// 	next('/');
				// }
			}
		} else {
			next('/');
		}
	}
});

export default router
