import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/pages/login'
import Layout from '@/pages/layout'
import Case from '@/pages/case/list'
import CaseDetails from '@/pages/case/details'
import News from '@/pages/news/list'
import NewsDetails from '@/pages/news/details'
import Media from '@/pages/media'
import Personnel from '@/pages/personnel'
import Home from '@/pages/home'

Vue.use(Router)

let router = new Router({
  routes: [
    {
      path: '/login',
      name: 'login',
      component: Login
    },
    {
      path: '/',
      name: 'layout',
      component: Layout,
      remarks: '管理后台',
      redirect: '/',
      children: [
        {
          path: '',
          name: 'home',
          component: Home,
          remarks: '首页',
          meta: {cache: true}
        },
        {
          path: 'case',
          name: 'caseList',
          remarks: '客户案例',
          component: Case,
          meta: { cache: true }
        },
        {
          path: 'case/:id',
          name: 'caseDetails',
          component: CaseDetails,
          remarks: '案例编辑'
        },
        {
          path: 'news',
          name: 'newsList',
          remarks: '新闻报道',
          component: News,
          meta: { cache: true }
        },
        {
          path: 'news/:id',
          name: 'newsDetails',
          component: NewsDetails,
          remarks: '新闻编辑'
        },
        {
          path: 'media',
          name: 'mediaInfo',
          remarks: '媒体资料',
          component: Media,
          meta: { cache: true }
        },
        {
          path: 'personnel',
          name: 'personnel',
          remarks: '人才招聘',
          component: Personnel,
          meta: { cache: true }
        }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.path !== '/login' && !sessionStorage.getItem('name')) {
    next({ path: '/login' })
  } else {
    next()
  }
})

router.afterEach(() => {
  window.scrollTo(0, 0)
})

export default router
