import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/Login/Login'
import CheckInfo from '@/components/CheckInfo/CheckInfo'
import CheckTemplate from '@/components/CheckTemplate/CheckTemplate'
import ProcessingRecord from '@/components/ProcessingRecord/ProcessingRecord'
import ChannelData from '@/components/ChannelData/ChannelData'
import DetailedData from '@/components/DetailedData/DetailedData'
import UserManage from '@/components/UserManage/UserManage'
import NotFoundComponent from '@/components/NotFoundComponent/NotFoundComponent'
import FieldTableInfo from '@/components/FieldTableInfo/FieldTableInfo'
import DatabaseInfo from '@/components/DatabaseInfo/DatabaseInfo'
import ChannelTableInfo from '@/components/ChannelTableInfo/ChannelTableInfo'
import Reload from '@/components/module/Reload'

Vue.use(Router)
let router = new Router({
  routes: [
    {
      path: '/',
      name: 'Login',
      component: Login,
      meta: {
        pageTitle: '用户登录',
        accessAllowLv: 1
      }
    },
    {
      path: '/checkInfo',
      name: 'CheckInfo',
      component: CheckInfo,
      meta: {
        pageTitle: '检测信息',
        accessAllowLv: 2
      }
    },
    {
      path: '/checkTemplate',
      name: 'CheckTemplate',
      component: CheckTemplate,
      meta: {
        pageTitle: '检测模板',
        accessAllowLv: 2
      }
    },
    {
      path: '/processingRecord',
      name: 'ProcessingRecord',
      component: ProcessingRecord,
      meta: {
        pageTitle: '处理记录',
        accessAllowLv: 2
      }
    },
    {
      path: '/channelData',
      name: 'ChannelData',
      component: ChannelData,
      meta: {
        pageTitle: '渠道数据',
        accessAllowLv: 1
      }
    },
    {
      path: '/detailedData',
      name: 'DetailedData',
      component: DetailedData,
      meta: {
        pageTitle: '详细数据',
        accessAllowLv: 1
      }
    },
    {
      path: '/userManage',
      name: 'UserManage',
      component: UserManage,
      meta: {
        pageTitle: '用户管理',
        accessAllowLv: 3
      }
    },
    {
      path: '/fieldTableInfo',
      name: 'FieldTableInfo',
      component: FieldTableInfo,
      meta: {
        pageTitle: '字段表信息',
        accessAllowLv: 3
      }
    },
    {
      path: '/databaseInfo',
      name: 'DatabaseInfo',
      component: DatabaseInfo,
      meta: {
        pageTitle: '数据库信息',
        accessAllowLv: 3
      }
    },
    {
      path: '/channelTableInfo',
      name: 'ChannelTableInfo',
      component: ChannelTableInfo,
      meta: {
        pageTitle: '渠道表信息',
        accessAllowLv: 3
      }
    },
    {
      path: '/reload',
      name: 'Reload',
      component: Reload,
      meta: {
        pageTitle: 'reload',
        accessAllowLv: 1
      }
    },
    {
      path: '*',
      name: 'NotFoundComponent',
      component: NotFoundComponent,
      meta: {
        pageTitle: '404',
        accessAllowLv: 1
      }
    }
  ]
})

let userInfo = JSON.parse(window.sessionStorage.getItem('userInfo'))

router.afterEach((to, from) => {
  // 登录状态判断
  if (to.path !== '/' && !window.sessionStorage.getItem('userInfo')) {
    // 未登陆 跳转到登录页
    router.push({'path': '/'})
  } else if (to.path === '/' && window.sessionStorage.getItem('userInfo')) {
    // 已登录 不能手动跳转到登录页
    router.go(-1)
  } else if (userInfo && userInfo.lv < to.meta.accessAllowLv) {
    // 访问权限不足
    router.go(-1)
  }
  // 返回页面顶端
  setTimeout(() => {
    window.$('#app>section').scrollTop(0)
  })
  // 修改 title
  document.title = to.meta.pageTitle + ' | 数据管理系统'
})

export default router
