import Vue from 'vue';
import VueRouter from 'vue-router';
import iView from 'iview';

Vue.use(VueRouter);

import AppLayout from './layouts/AppLayout.vue';
import EmployerLayout from './layouts/EmployerLayout.vue';

import Home from './views/Home.vue';
import Account from './views/Account.vue';
import Employer from './views/Employer.vue';
import LoginEmployer from './views/LoginEmployer.vue';
import PageNotFound from './views/PageNotFound.vue';
import RegisterEmployer from './views/RegisterEmployer';
import ManagejobEmployer from './views/managejob.vue';
import ConfigCompany from './views/ConfigCompany.vue';
import CreateJob from './views/CreateJob.vue';
import EditJob from './views/EditJob.vue';
import DetailJob from './views/DetailJob.vue';
import ShowJob from './views/ShowJob.vue';
import Page404 from './views/404.vue';
import ShowApplicant from './views/showapplicant.vue';

import Axios from 'axios';
import store from './store';

const router = new VueRouter({
  mode: 'history',
  routes: [
    {
      path: '/employer',
      name: 'employerLayout',
      component: EmployerLayout,
      children: [
        {
          path: '',
          name: 'Employer',
          component: Employer,
          meta: {
            loginEmployer: true
          }
        },
        {
          path: 'login',
          name: 'loginEmployer',
          component: LoginEmployer
        },
        {
          path: 'register',
          name: 'registerEmployer',
          component: RegisterEmployer
        },
        {
          path: 'managejob',
          name: 'managejobEmployer',
          component: ManagejobEmployer,
          meta: {
            loginEmployer: true
          }
        },
        {
          path: 'createjob',
          name: 'createjob',
          component: CreateJob,
          meta: {
            loginEmployer: true
          }
        },
        {
          path: 'editjob/:id',
          name: 'editJob',
          component: EditJob,
          meta: {
            loginEmployer: true
          }
        },
        {
          path: 'showapplicant',
          name: 'showapplicant',
          component: ShowApplicant,
          meta: {
            loginEmployer: true
          }
        },
        {
          path: 'configcompany',
          name: 'configcompany',
          component: ConfigCompany,
          meta: {
            loginEmployer: true
          }
        },
        {
          path: '*',
          name: '404Employer',
          component: PageNotFound
        }
      ]
    },
    {
      path: '/',
      name: 'appLayout',
      component: AppLayout,
      children: [
        {
          path: '',
          name: 'Home',
          component: Home
        },
        {
          path: 'showjob',
          name: 'showjob',
          component: ShowJob
        },
        {
          path: 'detailjob/:id',
          name: 'detailJob',
          component: DetailJob
        },
        {
          path: 'account',
          name: 'account',
          component: Account
        },
        {
          path: '*',
          name: 'pageNotFound',
          component: Page404
        }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  iView.LoadingBar.start();
  if(to.matched.some(record => record.meta.requiredUser)) {
    if(!localStorage.getItem('access_token')) {
      next('/')
    } else {
      next()
    }
  } else if(to.matched.some(record => record.meta.loginEmployer)) {
    if(!localStorage.getItem('access_token_employer')) {
      next('/employer/login')
    } else {
      next ()
    }
  } else {
    next()
  }
})

router.afterEach(route => {
  iView.LoadingBar.finish();
})

export default router