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

Vue.use(Router)

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

export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/index/index'),
        name: 'Index',
        meta: { title: '首页', icon: '首页', affix: true }
      }
    ]
  },
  {
    path: '/groupManagement',
    component: Layout,
    redirect: '/groupManagement/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/groupManagement/index'),
        name: 'groupManagement',
        meta: { title: '分组管理', icon: '分组管理', noCache: true }
      }
    ]
  },
  {
    path: '/deviceManagement',
    component: Layout,
    children: [
      {
        path: 'index',
        component: () => import('@/views/deviceManagement/index'),
        name: 'deviceManagement',
        meta: { title: '设备管理', icon: '设备管理', noCache: true }
      }
    ]
  },
  {
    path: '/userManagement',
    component: Layout,
    redirect: '/userManagement/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/userManagement/index'),
        name: 'userManagement',
        meta: { title: '用户管理', icon: '用户管理', noCache: true }
      }
    ]
  },
  {
    path: '/devicewithGroup',
    component: Layout,
    redirect: '/devicewithGroup/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/devicewithGroup/index'),
        name: 'devicewithGroup',
        meta: { title: '设备与组绑定', icon: '设备与组绑定', noCache: true }
      }
    ]
  },
  {
    path: '/deviceWithStatus',
    component: Layout,
    redirect: '/deviceWithStatus/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/deviceWithStatus/index'),
        name: 'deviceWithStatus',
        meta: { title: '设备与身份绑定', icon: '设备与身份绑定', noCache: true }
      }
    ]
  },
  {
    path: '/userWithGroup',
    component: Layout,
    redirect: '/userWithGroup/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/userWithGroup/index'),
        name: 'userWithGroup',
        meta: { title: '用户与组绑定', icon: '用户与组绑定', noCache: true }
      }
    ]
  },
  {
    path: '/accessRecords',
    component: Layout,
    redirect: '/accessRecords/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/accessRecords/index'),
        name: 'accessRecords',
        meta: { title: '通行记录', icon: '通行记录', noCache: true }
      }
    ]
  },
  {
    path: '/user',
    component: Layout,
    redirect: '/user/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/user/index'),
        name: 'user',
        meta: { title: '账号管理', icon: '账号管理', noCache: true }
      }
    ]
  },
  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index'),
        name: 'Profile',
        meta: { title: 'Profile', icon: 'user', noCache: true }
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  // mode: 'hash', // 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
