import { createRouter, createWebHistory } from "vue-router";
/**
 * 说明
 * meta中 
 * keepAlive表示是否要缓存  
 */

const routes=[
  {
    path: "/",
    redirect: "/report/index"
  },
  {
    path: "/report/index",
    name: "ReportIndex",
    component: ()=>import("@/views/report/Index.vue"),
  },
  {
    path: "/report/search",
    name: "ReportSearch",
    component: ()=>import("@/views/report/Search.vue"),
  },
  {
    path: "/report/detail",
    name: "ReportDetail",
    component: ()=>import("@/views/report/Detail.vue"),
  },
  {
    path: "/roadshow/detail",
    name: "RoadshowDetail",
    component: ()=>import("@/views/roadShow/Detail.vue"),
  },
  // verification目录下
  {
    path: "/login",
    name: "Login",
    component: ()=>import("@/views/verification/login.vue"),
  },
  {
    path: "/register",
    name: "Register",
    component: ()=>import("@/views/verification/register.vue"),
  },
  {
    path: "/bindPhoneNo",
    name: "BindPhoneNo",
    component: ()=>import("@/views/verification/bindPhoneNo.vue"),
  },
  {
    path: "/passwordMissing",
    name: "PasswordMissing",
    component: ()=>import("@/views/verification/passwordMissing.vue"),
  },
  {
    path: "/passwordChange",
    name: "PasswordChange",
    component: ()=>import("@/views/verification/passwordChange.vue"),
  },
  {
    path: "/transitionPage",
    name: "TransitionPage",
    component: ()=>import("@/views/verification/transitionPage.vue"),
  },
  {
    path: "/my",
    name: "MobileMyPage",
    component: ()=>import("@/views/verification/mobileMyPage.vue"),
  },
  {
    path: '/:pathMatch(.*)',
    name: 'error',
    component: () => import("@/views/404.vue"),
    meta: { title: '404' },
  }

]

const router=createRouter({
  history:createWebHistory(import.meta.env.VITE_APP_BASE_URL),
  routes,
  scrollBehavior(to, from, savedPosition){
    if (savedPosition&&to.meta.keepAlive) {
      return savedPosition
    } else {
      return new Promise((resolve)=>{
        setTimeout(() => {
          resolve({left:0,top:0})
        }, 0);
      })
    }
  }
})
// 白名单
const whitelist=['/login','/passwordMissing','/register']
// 需要重定向的路由
const shouldRedireact=['/report/index','/report/detail','/roadshow/detail']

router.beforeEach((to, from, next) => {
  // console.log(to.path);
  // 做登录的权限判断
  //英文研报1.1 取消登录判断
  /* if(!localStorage.getItem('yben_token') && !whitelist.includes(to.path)){
    if(shouldRedireact.includes(to.path)) sessionStorage.setItem('login_redirect',to.fullPath)

    next({path:'/login',replace:true})
    return 
  } */
  if(shouldRedireact.includes(to.path)) sessionStorage.setItem('login_redirect',to.fullPath)
  if(to.query.ShareEmail){
    sessionStorage.setItem('shareId',to.query.ShareEmail)
  }
	next();
})


export default router