import Vue from 'vue';
import Router from 'vue-router';
import _import from './_import.js';
Vue.use(Router);

// 页面刷新时，重新赋值token
if (window.sessionStorage.getItem('token')) {
  // eslint-disable-next-line
  store.commit(types.LOGIN, window.sessionStorage.getItem('token'));
}

const errorPage = r => {
  import('views/errorPage/index.vue').then(module => {
    r(module);
  });
};

export const constantRouterMap = [
  {
    path: '/',
    name: '/',
    redirect: '/version'
  },
  {
    path: '/haoban',
    name: '好办运维后台',
    redirect: '/version',
    component: _import('haoban', 'haobanIndex'),
    children: [
      {
        path: '/version',
        name: '版本更新管理',
        component: _import('haoban', 'versionList')
      },
      {
        path: '/addSet',
        name: '添加设置',
        component: _import('haoban', 'addSet')
      },
      {
        path: '/setDetail',
        name: '详情',
        component: _import('haoban', 'setDetail')
      },
      {
        path: '/adList',
        name: '广告列表',
        component: _import('haoban', 'adList')
      },
      {
        path: '/runLog',
        name: '数据监控',
        component: _import('haoban', 'runLog')
      }
    ]
  },

  {
    path: '/401',
    name: '无权访问',
    component: errorPage
  },
  {
    path: '/404',
    name: '不存在',
    component: errorPage
  },
  {
    path: '/500',
    name: 'error500',
    component: errorPage
  },
  { path: '*', redirect: '/404', hidden: true }
];
export default new Router({
  routes: constantRouterMap
  // scrollBehavior: () => ({ y: 0 })

  //使用keep-alive标签后部分安卓机返回缓存页位置不精确问题
  // scrollBehavior(to, from, savedPosition) {
  //   if (savedPosition && to.meta.keepAlive) {
  //     return savedPosition;
  //   }
  //   const layoutRight = document.querySelector('.layout-right');
  //   if (layoutRight) {
  //     layoutRight.scrollTo(0,0);
  //   }
  // }
});
