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 EditCompany from './views/EditCompany.vue';
import RegisterEmployer from './views/RegisterEmployer';
import ManagejobEmployer from './views/managejob.vue';
import ConfigCompany from './views/ConfigCompany.vue';
import AddJob from './views/AddJob.vue';
import Axios from 'axios';

const router = new VueRouter({
  mode: 'history',
  routes: [
    {
      path: '/employer',
      name: 'employerLayout',
      component: EmployerLayout,
      children: [
        {
          path: '',
          name: 'Employer',
          component: Employer,
          meta: {
            loginEmployer: true,
            configProfile: true
          }
        },
        {
          path: 'login',
          name: 'loginEmployer',
          component: LoginEmployer
        },
        {
          path: 'register',
          name: 'registerEmployer',
          component: RegisterEmployer
        },
        {
          path: 'managejob',
          name: 'managejobEmployer',
          component: ManagejobEmployer,
          meta: {
            loginEmployer: true,
            configProfile: true
          }
        },
        {
          path: 'editcompany',
          name: 'editcompany',
          component: EditCompany,
          meta: {
            loginEmployer: true,
            configProfile: true
          }
        },
        {
          path: 'addjob',
          name: 'addjob',
          component: AddJob,
          meta: {
            loginEmployer: true,
            configProfile: true
          }
        },
        {
          path: 'configcompany',
          name: 'configcompany',
          component: ConfigCompany
        },
        {
          path: '*',
          name: '404Employer',
          component: PageNotFound
        }
      ]
    },
    {
      path: '/',
      name: 'appLayout',
      component: AppLayout,
      children: [
        {
          path: '',
          name: 'Home',
          component: Home
        },
        {
          path: 'account',
          name: 'account',
          component: Account
        },
        {
          path: '*',
          name: 'pageNotFound',
          component: PageNotFound,
          meta: {
            requiredUser: true
          }
        }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  iView.LoadingBar.start();
  if(to.matched.some(record => record.meta.requiredUser)) {
    if(!localStorage.getItem('access_token')) {
      next('/employer/login')
    } else {
      next()
    }
  } else if (to.matched.some(record => record.meta.loginEmployer)) {
    if(!localStorage.getItem('access_token_employer')) {
      next('/employer/login')
    } else if (to.matched.some(record => record.meta.configProfile)) {
      axios.get('/api/auth/current', {
        headers: {
          Authorization: `Bearer ${localStorage.getItem('access_token_employer')}`
        }
      })
        .then(res => {
          axios.get(`/api/companys/${res.data.user.id}/check`)
            .then(res => {
              if (res.data.exists) {
                next()
              } else {
                next('/employer/configcompany')
              }
            })
            .catch(err => {next()})
        })
        .catch(err => {next()})
    } else {
      next()
    }
  } else {
    next()
  }
})

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

export default router