import {
  createRouter,
  createWebHistory,
  createWebHashHistory,
} from "vue-router";
import Layout from "@/layout/index.vue";
import store from "@/store";
import { ElMessage, ElMessageBox } from "element-plus";
const routes = [
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
  },
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/wanzai",
    name: "wanzai",
    component: Layout,
    children: [
      {
        path: "home",
        name: "home",
        meta: { isAuth: true, title: "系统首页" },
        component: () => import("@/views/home/home.vue"),
      },
      {
        path: "user",
        name: "user",
        meta: { isAuth: true, title: "用户管理" },
        component: () => import("@/views/user/user.vue"),
      },
      {
        path: "identity",
        name: "identity",
        meta: { isAuth: true, title: "身份管理" },
        component: () => import("@/views/identity/identity.vue"),
      },
      {
        path: "apply",
        name: "apply",
        meta: { isAuth: true, title: "应用管理" },
        component: () => import("@/views/apply/apply.vue"),
      },
      {
        path: "energy",
        name: "energy",
        meta: { isAuth: true, title: "能耗管理" },
        component: () => import("@/views/energy/energy.vue"),
      },
      {
        path: "safety",
        name: "safety",
        meta: { isAuth: true, title: "校园安全" },
        component: () => import("@/views/safety/safety.vue"),
      },
      {
        path: "caller",
        name: "caller",
        meta: { isAuth: true, title: "访客预约" },
        component: () => import("@/views/caller/caller.vue"),
      },
      {
        path: "info",
        name: "info",
        meta: { isAuth: true, title: "基本信息" },
        component: () => import("@/views/info/info.vue"),
      },
      {
        path: "screenShot",
        name: "screenShot",
        meta: { isAuth: true, title: "截屏风险" },
        component: () => import("@/views/screenShot/screenShot.vue"),
      },
      {
        path: "roles",
        name: "roles",
        meta: { isAuth: true, title: "权限管理" },
        component: () => import("@/views/roles/roles.vue"),
      },
    ],
  },
];
const router = createRouter({
  // history: createWebHashHistory("/business"), // 线上地址
  history: createWebHashHistory("/business"), // 测试环境发布地址
  // history: createWebHashHistory("/hotelReservation/pc3/"), // 测试环境发布地址
  routes: routes,
});
export default router;
router.beforeEach(async (to, from, next) => {
  // let hasRoutes = store.state.user.hasRoutes;
  // if (sessionStorage.getItem("token")) {
  //   if (!hasRoutes || to.path == "/login") {
  //     console.log("第一次登录也触发");
  //     store.commit("SET_ROUTES_STATE", true);
  //     let res = await store.dispatch("login", {
  //       adminName: sessionStorage.getItem("username"),
  //       password: sessionStorage.getItem("name"),
  //     });
  //     console.log(res, "路由判断登录");
  //     if (res == "success") {
  //       if (store.state.user.level == 2) {
  //         let modules = import.meta.glob("../views/**/*.vue");
  //         let arr = {
  //           path: "account",
  //           name: "account",
  //           component: modules[`../views/account/account.vue`],
  //         };
  //         router.addRoute("seniorCelebrity", arr);
  //       }
  //       next({ ...to, replace: true });
  //     } else {
  //       sessionStorage.removeItem("token");
  //       sessionStorage.removeItem("name");
  //       ElMessage({
  //         type: "error",
  //         showClose: true,
  //         message: res.data.data.message,
  //         center: true,
  //       });
  //     }
  //     //----------解决
  //     // next({ path: "/index" }); //----------解决
  //   }
  //   next(); //放行
  // } else {
  //   if (to.path == "/login") {
  //     next();
  //   } else {
  //     next(`/login`);
  //   }
  // }
  next();
});
