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: "/seniorCelebrity",
    name: "seniorCelebrity",
    component: Layout,
    children: [
      {
        path: "order",
        name: "order",
        component: () => import("@/views/order/order.vue"),
        meta: { title: "Dashboard", icon: "dashboard" },
      },
      {
        path: "orderEvaluate",
        name: "orderEvaluate",
        component: () => import("@/views/orderEvaluate/orderEvaluate.vue"),
        meta: { title: "Dashboard", icon: "dashboard" },
      },
      {
        path: "houseState",
        name: "houseState",
        component: () => import("@/views/houseState/houseState.vue"),
        meta: { title: "Dashboard", icon: "dashboard" },
      },
      {
        path: "housePrice",
        name: "housePrice",
        component: () => import("@/views/housePrice/housePrice.vue"),
        meta: { title: "Dashboard", icon: "dashboard" },
      },
      {
        path: "ledger",
        name: "ledger",
        component: () => import("@/views/ledger/ledger.vue"),
        meta: { title: "Dashboard", icon: "dashboard" },
      },
      {
        path: "houseInfo",
        name: "houseInfo",
        component: () => import("@/views/roomInfo/roomInfo.vue"),
        meta: { title: "Dashboard", icon: "dashboard" },
      },
      {
        path: "houseType",
        name: "houseType",
        component: () => import("@/views/roomType/roomType.vue"),
        meta: { title: "Dashboard", icon: "dashboard" },
      },
      {
        path: "houseList",
        name: "houseList",
        component: () => import("@/views/houseList/houseList.vue"),
        meta: { title: "Dashboard", icon: "dashboard" },
      },
      {
        path: "roomFlow",
        name: "roomFlow",
        component: () => import("@/views/roomFlow/roomFlow.vue"),
        meta: { title: "Dashboard", icon: "dashboard" },
      },
      {
        path: "serviceMall",
        name: "serviceMall",
        component: () => import("@/views/serviceMall/serviceMall.vue"),
        meta: {
          title: "服务商城",
        },
      },
    ],
  },
  {
    path: "/houseInfo/houseMap",
    component: () => import("@/views/map/map.vue"),
    meta: { title: "Dashboard", icon: "dashboard" },
  },
];
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`);
    }
  }
});
