import { createRouter,createWebHistory } from 'vue-router';

//all routes
const appAllRoutes = [];
const importAllRoutes = (r) => {
  for(let key in r) {
    appAllRoutes.push(...r[key].default)
  }
}
importAllRoutes(import.meta.glob('./modules/*.js',{ eager: true }))


const routes = [
  ...appAllRoutes,

  {
    name: 'login',
    path: '/login',
    component: () => import("@/views/Login.vue"),
    meta: { 
      title: "登录"
    },
  },
  {
    name: '404',
    path: '/404',
    component: () => import("@/views/404.vue"),
    meta: { 
      title: "404"
    },
  },
  {
    path: '/:pathMatch(.*)',
    redirect: {
      path: '/404'
    },
  },
]

export const router = createRouter({
	history: createWebHistory(import.meta.env.VITE_APP_BASE_URL),
  routes
});

router.beforeEach(async(to, from, next) => {

  let auth = localStorage.getItem("auth") || false;
  if (to.path != "/login" && to.path!='/temppage' &&to.path!='/fogetpassword' && !auth) {
    console.log(1111111)
    window.location.href =  window.location.origin + '/login';
    return false;
  }

  if (to.path) {
    //百度统计
    if (window._hmt) {
      window._hmt.push(["_trackPageview", "/#" + to.fullPath]);
    }
    //获取权限按钮
    // to.path != "/login"&&to.path!='/temppage'&&await store.dispatch('getPermissionButtons')
    next();
  } else {
    next({ path: "/404" });
  }
});

router.afterEach((to, from, next) => {
  window.scrollTo(0, 0);
});



export function initRouter(app) {
  app.use(router)
}