/**
 * @Author: NanKeYang
 * @Description: 路由定义,内部可分模块引用
 * @Data: 2019/12/27 15:39
 */
import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/**
 * 基本路由格式
 * {
        path: '',    // 访问路径
        redirect: '',   //  重定向路由
        component: () => import('@/views/job_ticket/index'),    // 路由引导
        meta: {
            index: 1,   // 动画序列下标
            title: '工作票列表'  // 标题名称 页面通过 $route.meta.title 获取
        },
        children: [] // 子路由，结构同上
    },
 */

const modulesFiles = require.context('./modules', true, /\.js$/)
const constantRoutes = modulesFiles.keys().reduce((modules, modulePath) => {
    const value = modulesFiles(modulePath)
    modules = modules.concat(value.default);
    return modules
}, [])

const createRouter = () => new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({y: 0}),
    routes: constantRoutes,
    // mode: 'history',
    mode: 'hash'
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
    const newRouter = createRouter()
    router.matcher = newRouter.matcher // reset router
}

export default router
