require('./bootstrap.js');
import './styles/theme.css';
require('./helper/autocloseMenu.js');
//window.VueRouter = require('vue-router').default;
//import VueRouter from 'vue-router';
import Util, { methods as utilMethods } from './util.js';
import routes from './routesIndex.js';
import OtpInput from "@bachdgvn/vue-otp-input";

Vue.component("v-otp-input", OtpInput);

Vue.use(VueRouter);
Vue.mixin(Util);
Vue.use(OtpInput);

const router = new VueRouter({
	mode: 'history',
	linkActiveClass: 'active',
	routes
});

router.beforeEach(function (to, from, next) {
	if (to.name === 'Voter Login' && utilMethods.isAdminPortalSession()) {
		var dest = utilMethods.getAdminEntryRoute();
		return next(Object.assign({}, dest, { replace: true }));
	}
	if (!to.path.startsWith('/admin') || to.name === 'Admin Login') {
		return next();
	}
	if (!localStorage.getItem('Access Token')) {
		return next();
	}
	var role = localStorage.getItem('admin_role');
	if (role === '3' && to.name !== 'Kehadiran Calon') {
		return next({ name: 'Kehadiran Calon', replace: true });
	}
	next();
});

const app = new Vue({
	router
}).$mount('#app');
