import Vue from "vue";
import Router from "vue-router";

import Home from "./views/Home";
// modules
import login from "./modules/login/router";
import client from "./modules/client/router";

Vue.use(Router);
/**
 * 权限控制：
 * 1、不同角色构建不同模块。
 * 2、登录成功之后根据角色注册相应模块。
 * 3、不同模块打包不同文件。（webpackChunkName）
 */

let router = new Router({
  routes: [
    {
      path: "/",
      name: "Index",
      redirect: { name: "Login" }
    },
    { ...login },
    {
      path: "/home",
      name: "Home",
      component: Home,
      redirect: { name: "TaskManage" },
      children: [...client]
    }
    // [lazy-loaded] route level code-splitting
    // {
    //   path: "/about",
    //   name: "about",
    //   // this generates a separate chunk (about.[hash].js) for this route
    //   // which is lazy-loaded when the route is visited.
    //   component: () =>
    //     import(/* webpackChunkName: "about" */ "./views/About.vue")
    // }
  ]
});

// route interceptor
router.beforeEach((to, from, next) => {
  const token = Vue.ls.get("token");
  if (to.meta.noRequire) {
    next();
    return;
  }

  if (!token) {
    // 登录失效的处理
    next({ name: "Login" });
    return;
  }

  next();
});

export default router;
