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: '/feedback',
		name: 'feedback',
		meta: ['user', 'root', 'hr'],
		component: () => import(/* webpackChunkName: "Feedback" */ '../views/Feedback.vue'),
	},
	{
		path: '/index',
		name: 'index',
		meta: ['user', 'root', 'hr'],
		// 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: '/addTask',
				name: 'addTask',
				meta: ['user'],
				component: () => import(/* webpackChunkName: "addTask" */ '../views/task/AddTask.vue')
			},
			{
				path: '/departmentTask',
				name: 'departmentTask',
				meta: ['user'],
				component: () => import(/* webpackChunkName: "departmentTask" */ '../views/task/DepartmentTask.vue')
			},
			{
				path: '/unpublishedTask',
				name: 'unpublishedTask',
				meta: ['user'],
				component: () => import(/* webpackChunkName: "unpublishedTask" */ '../views/task/UnpublishedTask.vue')
			},
			{
				path: '/viewTask',
				name: 'viewTask',
				meta: ['user'],
				component: () => import(/* webpackChunkName: "viewTask" */ '../views/task/ViewTask.vue')
			},
			{
				path: '/taskDetails',
				name: 'taskDetails',
				meta: ['user'],
				component: () => import(/* webpackChunkName: "taskDetails" */ '../views/task/TaskDetails.vue')
			},
			// {
			// 	path: '/personManage',
			// 	name: 'personManage',
			// 	meta: ['user'],
			// 	component: () => import(/* webpackChunkName: "home" */ '../views/PersonManage.vue')
			// },
			{
				path: '/personDetails',
				name: 'personDetails',
				meta: ['user'],
				component: () => import(/* webpackChunkName: "personDetails" */ '../views/person/PersonDetails.vue')
			},
			{
				path: '/personInfo',
				name: 'personInfo',
				meta: ['user'],
				component: () => import(/* webpackChunkName: "personInfo" */ '../views/person/PersonInfo.vue')
			},
			{
				path: '/personList',
				name: 'personList',
				meta: ['user'],
				component: () => import(/* webpackChunkName: "personList" */ '../views/person/PersonList.vue')
			},
			{
				path: '/attendance',
				name: 'attendance',
				meta: ['user'],
				component: () => import(/* webpackChunkName: "attendance" */ '../views/attendance/AttendanceStatistics.vue')
			},
			{
				path: '/addressBook',
				name: 'addressBook',
				meta: ['user'],
				component: () => import(/* webpackChunkName: "attendance" */ '../views/addressBook/AddressBook.vue')
			},
			// {
			// 	path: '/approval',
			// 	name: 'approval',
			// 	meta: ['user'],
			// 	component: () => import(/* webpackChunkName: "approval" */ '../views/attendance/ApprovalManage.vue')
			// },
			{
				path: '/leaveApproval',
				name: 'leaveApproval',
				meta: ['user'],
				component: () => import(/* webpackChunkName: "approval" */ '../views/attendance/ApprovalManage.vue')
			},
			{
				path: '/overtimeApproval',
				name: 'overtimeApproval',
				meta: ['user'],
				component: () => import(/* webpackChunkName: "approval" */ '../views/attendance/OvertimeApproval.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', 'hr'],
				component: () => import(/* webpackChunkName: "home" */ '../views/root/ParamsSetting.vue')
			},
			{
				path: '/consumerManage',
				name: 'consumerManage',
				meta: ['root', 'hr'],
				component: () => import(/* webpackChunkName: "home" */ '../views/root/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/root/RoleManage.vue')
			},
			{
				path: '/authorityManage',
				name: 'authorityManage',
				meta: ['root'],
				component: () => import(/* webpackChunkName: "home" */ '../views/root/AuthorityManage.vue')
			},
			{
				path: '/departmentManage',
				name: 'departmentManage',
				meta: ['root'],
				component: () => import(/* webpackChunkName: "home" */ '../views/root/DepartmentManage.vue')
			},
			{
				path: '/logManage',
				name: 'logManage',
				meta: ['root'],
				component: () => import(/* webpackChunkName: "home" */ '../views/root/LogManage.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')));
			console.log(role)
			if (JSON.stringify(role) === '') {
				next('/');
			} else {
				// console.log(to)
				let data;
				if (role.username !== 'root' && role.username !== 'HR') {
					data = 'user';
				} else if (role.username === 'HR') {
					data = 'hr';
				} else {
					data = 'root';
				}
				console.log(to, data)
				console.log(to.meta.findIndex(item => item === data))
				if (to.meta.findIndex(item => item === data) > -1) {
					next();
					return false;
				} else {
					next('/');
					localStorage.clear();
					return false;
				}
			}
		} else {
			next('/');
		}
	}
});

export default router
