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


const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/main',
      name: 'main',
      redirect: '/login',
      component: () => import('../views/main.vue'),
      meta: {
        name: 'main',
        title: '首页',
      }
    },
    {
      path: "/",
      name: 'home',
      component: () => import('../views/index.vue'),
      children: [
        {
          path: "/merchantManagement/homePage",
          name: "merchantBackHomePage",
          meta: {
            title: '用户列表',
          },
          component: () => import("../views/homePage/index.vue")
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/login.vue'),
      meta: {
        name: 'login',
        title: '登录',
      }
    },



    {
      path: "/:catchAll(.*)",
      name: 'noPage',
      component: () => import('../views/404.vue'),
      meta: {
        name: 'noPage',
        title: '无页面',
      }
    }
  ]
})
router.beforeEach(async (to, from, next) => {
  // 路由守卫
  console.log('to', to);
  console.log('from', from);
  const whiteList = [
    // 设置白名单
    // "/404",
    "/login"
  ];
  var isLogin // 登录状态判断
  if (localStorage.getItem("LOGIN_DATA") || localStorage.getItem("DAOTIAN_token")) {
    isLogin = true;
  }
  console.log(isLogin);
  if (isLogin) {
    if (to.path == '/login') {
      next("/merchantManagement/homePage");
    }
    next();
  } else {
    if (whiteList.some((obj) => obj === to.path)) {
      console.log('/login', to);
      // if (to.path == '/login') {
      // next('/login');
      next();
      // }
    } else {
      next("/login");
    }
  }
});
export default router
