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

Vue.use(Router)

// 引入Layout组件
import Layout from '@/layout'

// 常量路由
export const constantRoutes = [
  // 登录页
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

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

  // 首页
  {
    path: '/',
    component: Layout,
    redirect: '/workers',
    children: [
      {
        path: '/workers',
        name: 'Workers',
        component: () => import('@/views/workers/index'),
        meta: { title: '工人管理', icon: 'user' }
      }
    ]
  },

  // 修后评价
  {
    path: '/evaluate',
    component: Layout,
    children: [
      {
        path: '/evaluate',
        name: 'Evaluate',
        component: () => import('@/views/evaluate/index'),
        meta: { title: '修后评价', icon: 'el-icon-document-copy' }
      }
    ]
  },

  // 订单中心
  {
    path: '/center',
    component: Layout,
    redirect: '/center/distribute',
    name: 'Center',
    meta: {
      title: '订单中心',
      icon: 'el-icon-s-help'
    },
    children: [
      {
        path: 'distribute',
        component: () => import('@/views/center/distribute'),
        name: 'Distribute',
        meta: { title: '派发订单', icon: 'edit' }
      },
      {
        path: 'inquire',
        component: () => import('@/views/center/inquire'),
        name: 'Inquire',
        meta: { title: '查询订单', icon: 'list' }
      },
      {
        path: 'sendBack',
        component: () => import('@/views/center/sendBack'),
        name: 'SendBack',
        meta: { title: '查询退回', icon: 'list' }
      }
    ]
  },

  // 公告管理
  {
    path: '/notice',
    component: Layout,
    redirect: '/notice/publish',
    name: 'Notice',
    meta: {
      title: '公告管理',
      icon: 'el-icon-s-order'
    },
    children: [
      {
        path: 'publish',
        component: () => import('@/views/notice/publish'),
        name: 'Publish',
        meta: { title: '发布公告', icon: 'edit' }
      },
      {
        path: 'record',
        component: () => import('@/views/notice/record'),
        name: 'Record',
        meta: { title: '公告记录', icon: 'list' }
      }
    ]
  },

  // 404页面必须放在最后
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // 需要服务支持
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// 详情地址: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // 重置路由
}

export default router
