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 : {
    perms: ['admin','editor']    control the page perms (you can set multiple perms)
    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 perms can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },
  {
    path: '/changepassword',
    component: Layout,
    redirect: '/changepassword',
    name: 'ChangePW',
    meta: { title: '修改密码', icon: 'tree' },
    hidden:true,
    children: [
      {
        path: '',
        name: 'ChangePassword',
        component: () => import('@/views/system/changepassword'),
        meta: { title: '修改密码', noCache: true, icon: ''},
        hidden: true
      },
    ]
  },

]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user perms
 */
export const asyncRoutes = [
  {
    path: '/crm',
    component: Layout,
    redirect: '/crm/projects',
    name: 'CRM',
    meta: { title: '小程序管理', icon: 'wechat' },
    children: [
      {
        path: 'projects',
        name: 'Projects',
        component: () => import('@/views/crm/index'), // 这里的组件只是为了渲染 router-view
        redirect: '/crm/projects/training',
        meta: { title: '项目管理', icon: 'list' },
        children: [
            {
                path: 'training',
                name: 'ProjectsTraining',
                component: () => import('@/views/crm/project'),
                meta: { title: '学位项目', icon: 'education', type: 'training' }
            },
            {
                path: 'competition',
                name: 'ProjectsCompetition',
                component: () => import('@/views/crm/project'),
                meta: { title: '典礼&论坛', icon: 'trophy', type: 'competition' }
            },
            {
                path: 'grading',
                name: 'ProjectsGrading',
                component: () => import('@/views/crm/project'),
                meta: { title: '校友活动', icon: 'star', type: 'grading' }
            }
        ]
      },
      {
        path: 'teachers',
        name: 'Teachers',
        component: () => import('@/views/crm/teacher'),
        meta: { title: '教学中心', icon: 'user' }
      },
      {
        path: 'coupons',
        name: 'Coupons',
        component: () => import('@/views/crm/coupon'),
        meta: { title: '优惠券管理', icon: 'money' }
      },
      {
        path: 'issued-coupons',
        name: 'IssuedCoupons',
        component: () => import('@/views/crm/issued_coupon'),
        meta: { title: '已发优惠券', icon: 'list' }
      },
      {
        path: 'banners',
        name: 'Banners',
        component: () => import('@/views/crm/banner'),
        meta: { title: '轮播图管理', icon: 'drag' }
      },
      {
        path: 'students',
        name: 'Students',
        component: () => import('@/views/crm/student'),
        meta: { title: '学员管理', icon: 'peoples' }
      },
      {
        path: 'honors',
        name: 'Honors',
        component: () => import('@/views/crm/honor'),
        meta: { title: '学员荣誉管理', icon: 'medal' }
      },
      {
        path: 'showcases',
        name: 'Showcases',
        component: () => import('@/views/crm/showcase'),
        meta: { title: '精彩视频', icon: 'video' }
      },
      {
        path: 'notifications',
        name: 'Notifications',
        component: () => import('@/views/crm/index'),
        redirect: '/crm/notifications/history',
        meta: { title: '小程序通知', icon: 'message' },
        children: [
            {
                path: 'history',
                name: 'NotificationHistory',
                component: () => import('@/views/crm/notification/history'),
                meta: { title: '发送记录' }
            },
            {
                path: 'custom',
                name: 'NotificationCustom',
                component: () => import('@/views/crm/notification/custom'),
                meta: { title: '自定义发送' }
            },
            {
                path: 'project',
                name: 'NotificationProject',
                component: () => import('@/views/crm/notification/project'),
                meta: { title: '按项目发送' }
            },
            {
                path: 'coupon',
                name: 'NotificationCoupon',
                component: () => import('@/views/crm/notification/coupon'),
                meta: { title: '按优惠券发送' }
            }
        ]
      }
    ]
  },
  {
    path: '/system',
    component: Layout,
    redirect: '/system/user',
    name: 'System',
    meta: { title: '系统管理', icon: 'example', perms: ['system_manage'] },
    children: [
      {
        path: 'user',
        name: 'User',
        component: () => import('@/views/system/user.vue'),
        meta: { title: '用户管理', icon: 'user', perms: ['user_manage'] }
      },
      {
        path: 'organization',
        name: 'Organization',
        component: () => import('@/views/system/organization'),
        meta: { title: '部门管理', icon: 'tree', perms: ['org_manage'] }
      },
      {
        path: 'role',
        name: 'Role',
        component: () => import('@/views/system/role'),
        meta: { title: '角色管理', icon: 'lock', perms: ['role_manage'] }
      },
      {
        path: 'position',
        name: 'Postion',
        component: () => import('@/views/system/position'),
        meta: { title: '岗位管理', icon: 'position', perms: ['position_manage'] }
      },
      {
        path: 'dict',
        name: 'Dict',
        component: () => import('@/views/system/dict'),
        meta: { title: '数据字典', icon: 'example', perms: ['dict_manage'] }
      },
      {
        path: 'file',
        name: 'File',
        component: () => import('@/views/system/file'),
        meta: { title: '文件库', icon: 'documentation', perms: ['file_room'] }
      },
      {
        path: 'task',
        name: 'Task',
        component: () => import('@/views/system/task'),
        meta: { title: '定时任务', icon: 'list', perms: ['ptask_manage'] }
      }
    ]
  },
  {
    path: '/monitor',
    component: Layout,
    redirect: '/monitor/service',
    name: 'Monitor',
    meta: { title: '系统监控', icon: 'example', perms: ['monitor_set'] },
    children: [
      {
        path: 'service',
        name: 'service',
        component: () => import('@/views/monitor/service'),
        meta: { title: '服务监控', icon: 'example', perms: ['service_manage'] }
      }
    ]
  },
  {
    path: '/develop',
    component: Layout,
    redirect: '/develop/perm',
    name: 'Develop',
    meta: { title: '开发配置', icon: 'example', perms: ['dev_set'] },
    children: [
      {
        path: 'perm',
        name: 'Perm',
        component: () => import('@/views/system/perm'),
        meta: { title: '权限菜单', icon: 'example', perms: ['perm_manage'] }
      },
      {
        path: 'form-gen-link',
        component: Layout,
        children: [
          {
            path: 'https://jakhuang.github.io/form-generator/',
            meta: { title: '表单设计器', icon: 'link', perms: ['dev_form_gen'] }
          }
        ]
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

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

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
