import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'knwlhome',
    component: function (res) {
      require(['@/pages/knwlhome'], res)
    },
    meta: {
      title: '知识库'
    }
  },
  {
    path: '/know-detail',
    name: 'know-detail',
    component: function (res) {
      require(['@/pages/know-detail'], res)
    },
    meta: {
      title: '知识详情'
    }
  },
  {
    path: '/knwldir',
    name: 'knwldir',
    component: function (res) {
      require(['@/pages/knwldir'], res)
    },
    meta: {
      title: '目录'
    }
  },
  {
    path: '/sub-knwldir/:id',
    name: 'sub-knwldir',
    component: function (res) {
      require(['@/pages/sub-knwldir'], res)
    },
    meta: {
      title: '知识目录'
    }
  },
  {
    path: '/sub-knwldir1/:id',
    name: 'sub-knwldir1',
    component: function (res) {
      require(['@/pages/sub-knwldir1'], res)
    },
    meta: {
      title: '知识目录'
    }
  },
  {
    path: '/knwlborrow/:id',
    name: 'knwlborrow',
    component: function (res) {
      require(['@/pages/knwlborrow'], res)
    },
    meta: {
      title: '知识目录'
    }
  },
  {
    path: '/borrow-apply',
    name: 'borrow-apply',
    component: function (res) {
      require(['@/pages/borrow-apply'], res)
    },
    meta: {
      title: '借阅申请'
    }
  },
  {
    path: '/borrow-approve',
    name: 'borrow-approve',
    component: function (res) {
      require(['@/pages/borrow-approve'], res)
    },
    meta: {
      title: '借阅审批'
    }
  },
  {
    path: '/myknwl',
    name: 'myknwl',
    component: function (res) {
      require(['@/pages/myknwl'], res)
    },
    meta: {
      title: '我的'
    }
  },
  {
    path: '/new-knowledge',
    name: 'new-knowledge',
    component: function (res) {
      require(['@/pages/new-knowledge'], res)
    },
    meta: {
      title: '新建知识'
    }
  },
  {
    path: '/release-knowledge',
    name: 'release-knowledge',
    component: function (res) {
      require(['@/pages/release-knowledge'], res)
    },
    meta: {
      title: '发布知识'
    }
  },
  {
    path: '/know-dimension',
    name: 'know-dimension',
    component: function (res) {
      require(['@/pages/know-dimension'], res)
    },
    meta: {
      title: '知识维度'
    }
  },
  {
    path: '/devGetSession',
    name: 'devGetSession',
    component: () => import('../views/DevGetSession.vue')
  }
]

const router = new VueRouter({
  routes
})

/**
 *
 * to 表示将要跳转到的组件 (目标组件)
 * console.log(from); //(源组件)
 * next();
 * next 是一个函数
 * next() 进入下一个组件的钩子函数
 * next(false) 阻止跳转 中断导航
 * next("/login") 进入指定的组件的钩子函数
 */
// 路由守卫
router.beforeEach((to, from, next) => {
  if (production === false && store.state.sessionId == null && to.path != "/devGetSession") {
    //进入一个路由获取session，获取session后再进入主入口
    next("/devGetSession");
  } else {
    //跳转前设置title
    //window.document.title = to.meta.title;
    next();
  }
  //to.matched.some(res=>{res.meta.isLogin}) 能够获取路由配置的参数

});

//跳转后设置scroll为原点
router.afterEach((to, from, next) => {
  window.scrollTo(0, 0);
});
export default router
