import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
    {
        path: '/login',
        component: () => import('@/views/login'),
        hidden: true
    },

    {
        path: '/404',
        component: () => import('@/views/404'),
        hidden: true
    },

    {
        path: '/',
        component: Layout,
        redirect: '/management',
        meta: { title: '资产中心', icon: 'form' },
        children: [
            {
                path: '/management',
                name: 'Management',
                component: () => import('@/views/assets/assetManagement'),
                meta: { title: '资产管理' }
            },
            {
                path: '/statistics',
                name: 'Statistics',
                component: () => import('@/views/assets/assetStatistic'),
                meta: { title: '资产统计' }
            },
            {
                path: '/detail/:id',
                name: 'Detail',
                component: () => import('@/views/assets/assetDetail'),
                hidden: true,
                meta: { title: '资产详情' }
            },
            {
                path: '/operation/:id',
                name: 'Operation',
                component: () => import('@/views/assets/assetRecord'),
                hidden: true,
                meta: { title: '操作记录' }
            }
        ]
    },

    {
        path: '/member',
        component: Layout,
        redirect: '/crew',
        meta: { title: '人员管理', icon: 'form' },
        children: [
            {
                path: '/crew',
                name: 'CrewList',
                component: () => import('@/views/crew/crewList'),
                meta: { title: '通讯录' }
            },
            {
                path: '/crew/:id',
                name: 'CrewDetail',
                hidden: true,
                component: () => import('@/views/crew/crewDetail'),
                meta: { title: '人员详情' }
            },
            {
                path: '/record',
                name: 'Record',
                hidden: true,
                component: () => import('@/views/crew/record'),
                meta: { title: '操作记录' }
            },
            {
                path: '/score',
                name: 'Score',
                component: () => import('@/views/crew/score'),
                meta: { title: '打分统计' }
            }
        ]
    },

    {
        path: '/task',
        component: Layout,
        redirect: '/taskList',
        meta: { title: '任务管理', icon: 'form' },
        children: [
            {
                path: '/taskList',
                name: 'TaskList',
                component: () => import('@/views/task/taskList'),
                meta: { title: '任务列表' }
            },
            {
                path: '/task/:id',
                name: 'TaskDetail',
                hidden: true,
                component: () => import('@/views/task/taskDetail'),
                meta: { title: '任务详情' }
            }
        ]
    },

    {
        path: '/system',
        component: Layout,
        redirect: '/institution',
        meta: { title: '系统管理', icon: 'form' },
        children: [
            {
                path: '/institution',
                name: 'Institution',
                component: () => import('@/views/system/institution'),
                meta: { title: '制度管理' }
            },
            {
                path: '/institution/:id',
                name: 'InstitutionDetail',
                hidden: true,
                component: () => import('@/views/system/institutionDetail'),
                meta: { title: '制度详情' }
            },
            {
                path: '/permission',
                name: 'Permission',
                component: () => import('@/views/system/permission'),
                meta: { title: '权限设置' }
            },
            {
                path: '/log',
                name: 'Log',
                component: () => import('@/views/system/log'),
                meta: { title: '日志管理' }
            }
        ]
    },

    { path: '*', redirect: '/404', hidden: true }
]

export const staticRoutes = [
    {
        path: '/login',
        component: () => import('@/views/login'),
        hidden: true
    },

    {
        path: '/404',
        component: () => import('@/views/404'),
        hidden: true
    }
]

export const dynamicRoutes = [
    {
        path: '/system',
        component: Layout,
        redirect: '/institution',
        meta: { title: '系统管理', icon: 'form' },
        children: [
            {
                path: '/institution',
                name: 'Institution',
                component: () => import('@/views/system/institution'),
                meta: { title: '制度管理' }
            },
            {
                path: '/institution/:id',
                name: 'InstitutionDetail',
                hidden: true,
                component: () => import('@/views/system/institutionDetail'),
                meta: { title: '制度详情' }
            },
            {
                path: '/permission',
                name: 'Permission',
                component: () => import('@/views/system/permission'),
                meta: { title: '权限设置' }
            },
            {
                path: '/log',
                name: 'Log',
                component: () => import('@/views/system/log'),
                meta: { title: '日志管理' }
            }
        ]
    }
]

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

const router = createRouter()

export function resetRouter() {
    const newRouter = createRouter()
    router.matcher = newRouter.matcher // reset router
}

export default router
