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

Vue.use(Router)

import PortalLayout from '@/components/PortalLayout'

export const constantRoutes = [
  {
    path: '/portal',
    component: PortalLayout,
    redirect: '/portal/home',
    children: [
      {
        path: 'home',
        name: 'PortalHome',
        component: () => import('@/views/portal/Home'),
        meta: { title: '首页' }
      },
      {
        path: 'about',
        name: 'PortalAbout',
        component: () => import('@/views/portal/AboutSTS'),
        meta: { title: '关于STS' }
      },
      {
        path: 'science-plan',
        name: 'PortalSciencePlan',
        component: () => import('@/views/portal/SciencePlan'),
        meta: { title: '科学计划与国际合作' }
      },
      {
        path: 'progress',
        name: 'PortalProgress',
        component: () => import('@/views/portal/Progress'),
        meta: { title: '计划进展' }
      },
      {
        path: 'achievement',
        name: 'PortalAchievement',
        component: () => import('@/views/portal/Achievement'),
        meta: { title: '科研成果' }
      },
      {
        path: 'application',
        name: 'PortalApplication',
        component: () => import('@/views/portal/Application'),
        meta: { title: '应用与示范' }
      },
      {
        path: 'sdgsat1',
        name: 'PortalSDGSAT1',
        component: () => import('@/views/portal/SDGSAT1'),
        meta: { title: 'SDGSAT-1专区' }
      },
      {
        path: 'sdgsat1/case/detail',
        name: 'PortalSDGSAT1CaseDetail',
        component: () => import('@/views/portal/SDGSAT1CaseDetail'),
        meta: { title: '数据开放与应用 - 案例详情' }
      },
      {
        path: 'test',
        name: 'PortalTest',
        component: () => import('@/views/portal/TestPage'),
        meta: { title: '测试页面' }
      },
      {
        path: 'notice',
        name: 'PortalNotice',
        component: () => import('@/views/portal/Notice'),
        meta: { title: '通知公告' }
      }
    ]
  },
  {
    path: '/',
    redirect: '/portal/home'
  },
  {
    path: '/404',
    component: () => import('@/views/error/404'),
    hidden: true
  }
]

let routerPush = Router.prototype.push
let routerReplace = Router.prototype.replace

Router.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(err => err)
}

Router.prototype.replace = function push(location) {
  return routerReplace.call(this, location).catch(err => err)
}

export default new Router({
  mode: 'history',
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})
