import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router);

import Logins from './pages/logins/Index';
import Dashboards from './pages/dashboards/Index';

import UserList from './pages/users/List';
import UserDetail from './pages/users/Detail';

import AdminList from './pages/admins/List';
import AdminCreate from './pages/admins/Create';
import AdminEdit from './pages/admins/Edit';
import AdminProfile from './pages/admins/Profile';

import AdminRoleList from './pages/admin-roles/List';
import AdminRoleCreate from './pages/admin-roles/Create';
import AdminRoleEdit from './pages/admin-roles/Edit';

import PageNotFound from './pages/page-not-founds/Index';

let router = new Router({
	mode: 'history',
    base: process.env.MIX_SUB_URL + 'admin-sys',
    scrollBehavior() {
        return window.scrollTo({ top: 0, behavior: 'smooth' });
    },
    routes: [
        /////////////////////////////////////////////////////
        // Login
        {
            path: '/login',
            name: 'Login',
            meta: {
                layout: 'userpages',
                guest: true
            },
            component: Logins
        },
        /////////////////////////////////////////////////////
        // Dashboard
    	{
            path: '/',
            name: 'Dashboards',
            meta: {
                auth: true
            },
            component: Dashboards
        },
        /////////////////////////////////////////////////////
        // User
        {
            path: '/user/list',
            name: 'User List',
            meta: {
                auth: true
            },
            component: UserList
        },
        {
            path: '/user/detail/:hash_id',
            name: 'User Detail',
            meta: {
                auth: true
            },
            component: UserDetail
        },
        /////////////////////////////////////////////////////
        // Admin
        {
            path: '/admin/list',
            name: 'Admin List',
            meta: {
                auth: true
            },
            component: AdminList
        },
        {
            path: '/admin/create',
            name: 'Admin Create',
            meta: {
                auth: true
            },
            component: AdminCreate
        },
        {
            path: '/admin/edit/:hash_id',
            name: 'Admin Edit',
            meta: {
                auth: true
            },
            component: AdminEdit
        },
        {
            path: '/admin/profile/update',
            name: 'Admin Profile',
            meta: {
                auth: true
            },
            component: AdminProfile
        },
        /////////////////////////////////////////////////////
        // Admin Role
        {
            path: '/admin-role/list',
            name: 'Admin Role List',
            meta: {
                auth: true
            },
            component: AdminRoleList
        },
        {
            path: '/admin-role/create',
            name: 'Admin Role Create',
            meta: {
                auth: true
            },
            component: AdminRoleCreate
        },
        {
            path: '/admin-role/edit/:hash_id',
            name: 'Admin Role Edit',
            meta: {
                auth: true
            },
            component: AdminRoleEdit
        },
        /////////////////////////////////////////////////////
        // 404
        {
            path: '*',
            name: 'Page 404',
            meta: {
                layout: 'userpages',
                guest: true
            },
            component: PageNotFound
        },
        /////////////////////////////////////////////////////
    ]
});

window.ADMIN_DETAIL = localStorage.getItem('admin_detail') != null ? JSON.parse(localStorage.getItem('admin_detail')) : null;
window.TOKEN = localStorage.getItem('admin_jwt');

router.beforeEach((to, from, next) => {
    if(to.matched.some(record => record.meta.auth)) {
        if (localStorage.getItem('admin_jwt') == null) {
            next({ path: 'login' })
        }
        else {
            next();
        }
    } 

    if(to.matched.some(record => record.meta.without_auth)) {
        if (localStorage.getItem('admin_jwt') != null) {
            next({ path: '/' })
        }
        else {
            next();
        }
    } 
    next();
});


export default router;