/*
 * @Author       : yuanrunwei
 * @Date         : 2020-05-18 12:05:37
 * @LastEditors: XuTongZhang
 * @LastEditTime: 2020-08-03 15:09:54
 * @FilePath     : \ambre-admin-noob\src\router\index.js
 */
import Vue from 'vue'
import VueRouter from 'vue-router'
// import cookiesDeploy from '../common/utils/cookiesDeploy'
Vue.use(VueRouter)
// let list

const routes = [
  {
    path: '/home',
    name: 'home',
    component: () => import('@/views/index')
  },
  {
    path: '/candidateAll',
    name: 'candidateAll',
    component: () => import('@/views/candidateManage/candidateAll')
  },
  {
    path: '/candidateList',
    name: 'candidateList',
    component: () => import('@/views/candidateManage/candidateList')
  },
  {
    path: '/resumeList',
    name: 'resumeList',
    component: () => import('@/views/candidateManage/resumeList')
  },
  {
    path: '/mailManage',
    name: 'mailManage',
    component: () => import('@/views/candidateManage/mailManage')
  },
  {
    path: '/statistics',
    name: 'statistics',
    component: () => import('@/views/candidateManage/statistics')
  },
  {
    path: '/basicInfo',
    name: 'basicInfo',
    component: () => import('@/views/companyManage/basicInfo')
  },
  {
    path: '/positionInfo',
    name: 'positionInfo',
    component: () => import('@/views/companyManage/positionInfo')
  },
  {
    path: '/passwordSettings',
    name: 'passwordSettings',
    component: () => import('@/views/companyManage/passwordSettings')
  },
  {
    path: '/procedureManage',
    name: 'procedureManage',
    component: () => import('@/views/companyManage/procedureManage')
  },
  {
    path: '/videoManage',
    name: 'videoManage',
    component: () => import('@/views/companyManage/videoManage')
  },
  {
    path: '/sceneManage',
    name: 'sceneManage',
    component: () => import('@/views/companyManage/sceneManage')
  },
  {
    path: '/formManage',
    name: 'formManage',
    component: () => import('@/views/companyManage/formManage')
  },
  {
    path: '/templateManage',
    name: 'templateManage',
    component: () => import('@/views/companyManage/templateManage')
  },
  // error
  {
    path: '*',
    name: 'error',
    component: resolve => require(['@/views/error'], resolve)
  },
  // login
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login.vue')
  },
  // redirect
  {
    path: '/',
    redirect: '/home'
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
const whiteList = ['/login'] // 不重定向白名单
const info = JSON.parse(sessionStorage.getItem('info'))
router.beforeEach((to, from, next) => {
  if (info && info.id && localStorage.getItem('menuId')) {
    if (to.path === '/login') {
      next({ path: '/' })
    } else {
      // if (!to.meta.id) next()
      // else if (localStorage.getItem('menuId')) {
      //   list = localStorage.getItem('menuId').split(',').map(item => item - 0)
      //   // console.log(to, list)
      //   // console.log(list.includes(to.meta.id))
      //   list.includes(to.meta.id) ? next() : next({
      //     path: routes[routes.findIndex(item => list.includes(item.meta.id))].path
      //   })
      next()
      // }
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next({
        name: 'login'
      })
    }
  }
})

export default router
