import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import( '../views/Home.vue'),
    redirect:"index",
    meta: {requireAuth: true},
    children:[
      {
        path:"index",
        name:"Index",
        component: () => import( '../views/Index.vue'),
        meta: {requireAuth: true},
      },{
        path:"map",
        name:"Map",
        component: () => import( '../views/Map.vue'),
        meta: {requireAuth: true},
      },{
        path:"data",
        name:"Data",
        redirect:"/data/basedata",
        component: () => import( '../views/DataManagement.vue'),
        meta: {requireAuth: true},
        children:[
          {
            path:"basedata",
            name:"basedata",
            component: () => import("../views/datamanagement/BaseDataManagement.vue"),
            meta: {requireAuth: true},
          },
          {
            path:"layertype",
            name:"LayerType",
            component: () => import("../views/datamanagement/LayerTypeManagement.vue"),
            meta: {requireAuth: true},
          },{
            path:"layer",
            name:"Layer",
            component: () => import("../views/datamanagement/LayerManagement.vue"),
            meta: {requireAuth: true},
          },{
            path:"addlayer",
            name:"addLayer",
            component: () => import("../views/datamanagement/AddLayer.vue"),
            meta: {requireAuth: true},
          }
        ]
      }
    ],
    
  },{
    path:"/login",
    name:"Login",
    component: () => import( '../views/Login.vue'),
    meta: {requireAuth: false},
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // 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')
  }
]



const router = new VueRouter({
  routes
})

// 导航守卫
router.beforeEach((to, from, next) => {
  // 跳转到最顶部
  // window.scroll(0, 0);
  console.log(from);

  if (to.meta.requireAuth == true) {
      let token = localStorage.getItem("X-Token");
      if (token == null || token == "") {
          next({
              path: '/login',
              query: {redirect: to.fullPath}
          })
      } else {
          next();
      }
  } else {
      if(to.path == '/login'){
          let token = localStorage.getItem("X-Token");
          if(token == null || token == ""){
              next();
          }
      }else{
          next();
      }
      
  }

});

export default router
