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

Vue.use(Router)

const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      redirect: 'home'
    },
    {
      path: '/home',
      name: 'apphome',
      components: {
        default: () => import('@com/home/Home'),
        header: () => import('@com/AppHeader'),
      }
    },
    {
      path: '/login',
      component: () => import('@com/loginregister/LoginRegister'),
      children: [
        {
          path: '/',
          component: () => import('@com/loginregister/Login'),
        }
      ]
    },
    {
      path: '/register',
      component: () => import('@com/loginregister/LoginRegister'),
      children: [
        {
          path: '/',
          component: () => import('@com/loginregister/Register'),
        }
      ]
    },
    {
      path: '/forgot',
      component: () => import('@com/loginregister/LoginRegister'),
      children: [
        {
          path: '/',
          component: () => import('@com/loginregister/ForgotPassword'),
        }
      ]
    },
    {
      path: '/person',
      components: {
        default: () => import('@compage/user/User'),
        header: () => import('@com/AppHeader'),
      },
      children: [
        {
          path: '/',
          redirect: 'userinfo'
        },
        {
          path: 'userinfo',
          component: () => import('@compage/UserInfo')
        },
        {
          path: 'addinfo',
          component: () => import('@compage/user/AddInfo')
        },
        {
          path: 'changepassword',
          component: () => import('@compage/ChangePassword'),
        },
        {
          path: 'currentorder',
          component: () => import('@compage/user/CurrentOrder'),
        },
        {
          path: 'addorder',
          component: () => import('@compage/user/AddOrder'),
          meta: {
            keepAlive: true
          }
        },
        {
          path: 'horder',
          component: () => import('@compage/user/HOrder')
        },
        {
          path: 'userorder',
          component: () => import('@compage/user/UserOrder'),
        },
        {
          path: 'mymeg',
          component: () => import('@compage/MyMessage'),
        },
      ]
    },
    {
      path: '/woker',
      components: {
        default: () => import('@compage/woker/Woker'),
        header: () => import('@com/AppHeader'),
      },
      children: [
        {
          path: '/',
          redirect: 'userinfo'
        },
        {
          path: 'userinfo',
          component: () => import('@compage/UserInfo')
        },
        {
          path: 'changepassword',
          component: () => import('@compage/ChangePassword')
        },
        {
          path: 'wa',
          component: () => import('@compage/woker/WokerAccount')
        },
        {
          path: 'wro',
          component: () => import('@compage/woker/WokerReceivedOrder')
        },
        {
          path: 'who',
          component: () => import('@compage/woker/WokerHistoryOrder')
        },
        {
          path: 'wfo',
          component: () => import('@compage/woker/WokerFindOrder')
        },
        {
          path: 'auol',
          component: () => import('@compage/woker/AllUserOrderList')
        },
        {
          path: 'mymeg',
          component: () => import('@compage/MyMessage')
        }
      ]
    },
    {
      path: '/rootlogin',
      component: () => import('@comadmin/AdminLogin')
    },
    {
      path: '/admin',
      component: () => import('@comadmin/AdminPage'),
      children: [
        {
          path: '/',
          redirect: 'au'
        },
        {
          path: 'mail',
          component: () => import('@comadmin/mail/Index')
        },
        {
          path: 'au',
          component: () => import('@comadmin/allperson/AllUser')
        },
        {
          path: 'all',
          component: () => import('@comadmin/allorder/allOrderList')
        },
        {
          path: 'sm',
          component: () => import('@comadmin/message/SendMessage')
        },
        {
          path: 'jua',
          component: () => import('@comadmin/joinus/JoinUsApply')
        },
        {
          path: 'jha',
          component: () => import('@comadmin/joinus/JoinUsHistoryApply')
        },
        {
          path: 'sl',
          component: () => import('@comadmin/SuggestList')
        }
      ]
    },
    {
      path: '/aboutus',
      name: 'AboutUs',
      components: {
        default: () => import('@com/aboutus/AboutUs'),
        header: () => import('@com/AppHeader'),
      }
    },
    {
      path: '/customerservice',
      name: 'CustomerService',
      components: {
        default: () => import('@com/customerservice/CustomerService'),
        header: () => import('@com/AppHeader'),
      }
    },
    {
      path: '/suggest',
      name: 'suggest',
      components: {
        default: () => import('@com/suggest/Suggest'),
        header: () => import('@com/AppHeader'),
      }
    },
    {
      path: '*',
      name: '404',
      component: () => import('@com/error/404')
    }
  ]
})
router.beforeEach((to, from, next) => {
  var identity = sessionStorage.getItem('identity')
  if (!identity) {
    // eslint-disable-next-line eqeqeq
    if (to.path.indexOf('person') == 1 | to.path.indexOf('woker') == 1) {
      return next('/login')
    }
    // eslint-disable-next-line eqeqeq
    if (to.path.indexOf('admin') == 1) {
      return next('/rootlogin')
    }
  } else {
    // eslint-disable-next-line eqeqeq
    if ((!to.path.indexOf('person') == 1 && !identity == 3) || (!to.path.indexOf('woker') == 1 && !identity == 2)) {
      return next('/login')
    }
    // eslint-disable-next-line eqeqeq
    if (!to.path.indexOf('admin') == 1 && !identity == 1) {
      return next('/rootlogin')
    }
  }
  next()
})
export default router
