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 UserProfile from './pages/users/Profile';

import ContractTemplateList from './pages/contract-templates/List';
import ContractTemplateCreate from './pages/contract-templates/Create';
import ContractTemplateEdit from './pages/contract-templates/Edit';

import ContractList from './pages/contracts/List';
import ContractCreate from './pages/contracts/Create';
import ContractEdit from './pages/contracts/Edit';

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

let router = new Router({
	mode: 'history',
    base: process.env.MIX_SUB_URL,
    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
        },
        /////////////////////////////////////////////////////
        // Contract Template
        {
            path: '/contract-template/list',
            name: 'Contract Template List',
            meta: {
                auth: true
            },
            component: ContractTemplateList
        },
        {
            path: '/contract-template/create',
            name: 'Contract Template Create',
            meta: {
                auth: true
            },
            component: ContractTemplateCreate
        },
        {
            path: '/contract-template/edit/:hash_id',
            name: 'Contract Template Edit',
            meta: {
                auth: true
            },
            component: ContractTemplateEdit
        },
        /////////////////////////////////////////////////////
        // Contract
        {
            path: '/contract/list',
            name: 'Contract List',
            meta: {
                auth: true
            },
            component: ContractList
        },
        {
            path: '/contract/create',
            name: 'Contract Create',
            meta: {
                auth: true
            },
            component: ContractCreate
        },
        {
            path: '/contract/edit/:hash_id',
            name: 'Contract Edit',
            meta: {
                auth: true
            },
            component: ContractEdit
        },
        /////////////////////////////////////////////////////
        // User
        {
            path: '/user/profile/update',
            name: 'User Profile',
            meta: {
                auth: true
            },
            component: UserProfile
        },
        /////////////////////////////////////////////////////
        // 404
        {
            path: '*',
            name: 'Page 404',
            meta: {
                layout: 'userpages',
                guest: true
            },
            component: PageNotFound
        },
        /////////////////////////////////////////////////////
    ]
});

window.TOKEN = localStorage.getItem('user_jwt');

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

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


export default router;