import Vue from 'vue'
import Router from 'vue-router'
/* Layout */
import Layout from '@/layout'

Vue.use(Router)

/* Router Modules */

/**
 * 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'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    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: '/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: '/inner-login',
    component: () => import('@/views/login/innerLogin'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: {title: 'dashboard', icon: 'dashboard', affix: 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}
      }
    ]
  }
]

// 用来在noCache=false的情况下进行页面刷新
// beforeRouteEnter(to, from, next) {
//   if (to.params.flush || to.query.flush) {
//     next(vm => {
//       if (!vm.initial) {
//
//       }
//       vm.initial = false
//     })
//   } else {
//     next(vm => {
//       vm.initial = false
//     })
//   }
// },

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [

  {
    path: '/category',
    component: Layout,
    meta: {
      title: '分类管理',
      icon: 'tree-table',
      permission: 'category-management'
    },
    alwaysShow: true,
    redirect: '/category',
    children: [
      {
        path: '',
        component: () => import('@/views/category/index'),
        name: 'CategoryManagement',
        meta: {
          title: '分类设置',
          permission: 'category-management-page'
        }
      }
    ]
  },
  {
    path: '/resource',
    component: Layout,
    meta: {
      title: '资源管理',
      icon: 'zip',
      permission: 'resource-management'
    },
    alwaysShow: true,
    redirect: '/resource',
    children: [
      {
        path: '',
        component: () => import('@/views/resource/index'),
        name: 'ResourceManagement',
        meta: {
          title: '资源设置',
          permission: 'resource-management-page',
          noCache: true
        }
      },
      {
        path: '/add/:resourceId',
        hidden: true,
        component: () => import('@/views/resource/addResource'),
        name: 'AddResourcePage',
        meta: {
          title: '新增资源',
          activeMenu: '/resource',
          permission: 'add-resource-page',
          noCache: true
        }
      },
    ]
  },

  {
    path: '/project',
    component: Layout,
    meta: {
      title: '任务资源分配',
      icon: 'tree',
      permission: 'task-assign-management'
    },
    alwaysShow: true,
    redirect: '/project',
    children: [
      {
        path: '',
        component: () => import('@/views/project/index'),
        name: 'ProjectManagement',
        meta: {
          title: '项目管理',
          permission: 'project-management-page'
        }
      },

      {
        path: 'group',
        hidden: true,
        component: () => import('@/views/project/groupIndex'),
        name: 'ProjectGroupManagement',
        meta: {
          title: '项目组维护',
          activeMenu: '/project',
          permission: 'group-management-page'
        }
      },
      {
        path: 'add',
        hidden: true,
        component: () => import('@/views/project/addProject'),
        name: 'AddProjectPage',
        meta: {
          title: '新增项目',
          activeMenu: '/project',
          permission: 'add-project-page'
        }
      },
      {
        path: 'edit/:projectId',
        hidden: true,
        component: () => import('@/views/project/editProject'),
        name: 'EditProjectPage',
        meta: {
          title: '修改项目',
          activeMenu: '/project',
          permission: 'edit-project-page'
        }
      },
      {
        path: 'task/:projectId',
        hidden: true,
        component: () => import('@/views/project/task'),
        name: 'ProjectTaskPage',
        meta: {
          title: '任务列表',
          activeMenu: '/project',
          permission: 'task-management-page',
          noCache: true
        }
      },
      {
        path: '/project/task/mark/:taskId',
        hidden: true,
        component: () => import('@/views/project/mark'),
        name: 'MarkPage',
        meta: {
          title: '任务标注',
          activeMenu: '/project',
          permission: 'mark-page',
          noCache: true
        }
      },
      {
        path: 'sample-task/:projectId',
        hidden: true,
        component: () => import('@/views/project/sampleTask'),
        name: 'SampleTaskPage',
        meta: {
          title: '抽样任务列表',
          activeMenu: '/project',
          permission: 'sample-task-page',
          noCache: true
        }
      },
      {
        path: '/project/task/sample-mark/:taskId',
        hidden: true,
        component: () => import('@/views/project/sampleMark'),
        name: 'SampleMarkPage',
        meta: {
          title: '任务抽样验证',
          activeMenu: '/project',
          permission: 'sample-mark-page',
          noCache: true
        }
      },
    ]
  },
  {
    path: '/my-task',
    component: Layout,
    meta: {
      title: '我的任务',
      icon: 'skill',
      permission: 'my-task-management'
    },
    alwaysShow: true,
    redirect: '/my-task',
    children: [
      {
        path: '',
        component: () => import('@/views/project/myTask'),
        name: 'MyTaskPage',
        meta: {
          title: '我的任务',
          permission: 'my-task-page',
          noCache: true
        }
      }

    ]
  },
  {
    path: '/mark-result',
    component: Layout,
    meta: {
      title: '标注结果查询',
      icon: 'table',
      permission: 'mark-result-management'
    },
    alwaysShow: true,
    redirect: '/my-task',
    children: [
      {
        path: '',
        component: () => import('@/views/project/markResult'),
        name: 'MarkResultPage',
        meta: {
          title: '标注结果查询',
          permission: 'mark-result-page'
        }
      }

    ]
  },
  {
    path: '/system',
    component: Layout,
    redirect: '/system/user',
    alwaysShow: true, // will always show the root menu
    name: 'System',
    meta: {
      title: 'system',
      icon: 'lock',
      permission: 'system-management'
    },
    children: [
      {
        path: 'user',
        component: () => import('@/views/system/user/index'),
        name: 'UserManagement',
        meta: {
          title: 'userManagement',
          permission: 'user-management'
        }
      },
      {
        path: 'user/add',
        hidden: true,
        component: () => import('@/views/system/user/addUserPage'),
        name: 'AddUserPage',
        meta: {
          title: 'addUserPage',
          activeMenu: '/user/index',
          permission: 'add-user-page'
        }
      },
      {
        path: 'inner-user/add',
        hidden: true,
        component: () => import('@/views/system/user/addInnerUserPage'),
        name: 'AddInnerUserPage',
        meta: {
          title: '新增内部用户',
          activeMenu: '/user/index',
          permission: 'add-inner-user-page'
        }
      },
      {
        path: 'user/edit/:userId',
        hidden: true,
        component: () => import('@/views/system/user/editUserPage'),
        name: 'EditUserPage',
        meta: {
          title: 'editUserPage',
          activeMenu: '/user/index',
          permission: 'edit-user-page'
        }
      },
      {
        path: 'inner-user/edit/:userId',
        hidden: true,
        component: () => import('@/views/system/user/editInnerUserPage'),
        name: 'EditInnerUserPage',
        meta: {
          title: '编辑内部用户',
          activeMenu: '/user/index',
          permission: 'edit-inner-user-page'
        }
      },
      // {
      //   path: 'role',
      //   component: () => import('@/views/system/role/index'),
      //   name: 'RoleManagement',
      //   meta: {
      //     title: 'roleManagement',
      //     permission: 'role-management'
      //   }
      // },
      // {
      //   path: 'perm',
      //   component: () => import('@/views/system/perm/index'),
      //   name: 'PermissionManagement',
      //   meta: {
      //     title: 'permissionManagement',
      //     permission: 'this menu need administrator role'
      //   }
      // },
      // {
      //   path: 'log',
      //   component: () => import('@/views/system/log/index'),
      //   name: 'LogManagement',
      //   meta: {
      //     title: 'logManagement',
      //     permission: 'log-management'
      //   }
      // }
    ]
  },
  /** when your routing map is too long, you can split it into small modules **/

  // 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
