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

Vue.use(VueRouter)

const routes = [
  {
    path: '/',  //页面主路由
    name: 'main',
    component: () => import ('@/views/Main.vue'),
    children: [
      {// 资产库主页
        path: '/workspace',
        name: 'workspace',
        component: () => import('../views/workspace/Workspace')
      },
      {// 用户无任何小组情况下的显示及小组创建
        path: '/cooperationCreate',
        name: 'cooperationCreate',
        component: () => import('../views/cooperation/CooperationCreate')
      },
      {
        path: '/repository',
        name: 'repository',// 模型
        component: () => import('../views/repository/Repository')
      },
      {
        path: '/manage',
        name: '管理中心',
        component: () => import('../views/manage/Manage'),
        children: [
          {
            path: '/workspaceManage',
            name: '资产库管理',
            component: () => import('../views/workspace/WorkspaceManage')
          },
          {
            path: '/cooperationUpdate',
            name: 'PAL小组更新(管理)',
            component: () => import('../views/cooperation/CooperationUpdate')
          },
          {
            path: '/palUser',
            name: 'PAL用户',
            component: () => import('../views/user/User')
          },
          {
            path: '/bpmOrg',
            name: 'BPM组织架构',
            component: () => import('../views/user/BPMOrg')
          },
          {
            path: '/mappingManagement_correlated',
            name: 'mappingManagement_correlated',
            component: () => import('../views/mappingManagement/MappingManagement')
          },
          {
            path: '/mappingManagement_palNotCorrelated',
            name: 'mappingManagement_palNotCorrelated',
            component: () => import('../views/mappingManagement/MappingManagement')
          },
          {
            path: '/mappingManagement_bpmNotCorrelated',
            name: 'mappingManagement_bpmNotCorrelated',
            component: () => import('../views/mappingManagement/MappingManagement')
          },
          {
            path: '/themeStyle',
            component: () => import('../views/portal/ThemeStyle')
          },
          {
            path: '/commonRepository',
            component: () => import('../views/portal/CommonRepository')
          },
          {
            path: '/userGroup',
            component: () => import('../views/portal/UserGroup')
          }
        ]
      }
    ]
  },
  {
    path: '/devGetSession',  //开发时获取session作用
    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
