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: "/process",
    name: "process",
    component: Layout,
    children: [
      {
        path: "home",
        name: "home",
        component: () => import("@/views/home/home.vue"),
        meta: { title: "首页", icon: "home" },
      },
      {
        path: "project",
        name: "project",
        component: () => import("@/views/project/project.vue"),
        meta: { title: "项目管理", icon: "project" },
      },
      {
        path: "role",
        name: "role",
        component: () => import("@/views/role/role.vue"),
        meta: { title: "角色管理", icon: "role" },
      },
      {
        path: "user",
        name: "user",
        component: () => import("@/views/user/user.vue"),
        meta: { title: "用户管理", icon: "user" },
      },
      {
        path: "system",
        name: "system",
        component: () => import("@/views/system/system.vue"),
        meta: { title: "系统管理", icon: "system" },
      },
    ],
  },
];

const router = createRouter({
  history: createWebHashHistory("/jgcloudManage"), // 发布地址
  routes: routes,
});
export default router;
router.beforeEach(async (to, from, next) => {
  const token = sessionStorage.getItem("token");
  const userhead = sessionStorage.getItem("userhead");
  let hasRoutes = store.state.user.hasRoutes;
  next()
  // if (token && userhead) {
  //   // next();
  //   if (!hasRoutes) {
  //     console.log("第一次登录也触发");
  //     store.commit("SET_ROUTES_STATE", true);
  //     store.commit("ROLELIST", "");
  //     next({ ...to, replace: true });
  //     // next({ path: "/index" });
  //     // next({ ...to });
  //   } else {
  //     console.log("没刷新页面跳转");
  //   }
  //   next();
  // } else {
  //   if (to.path == "/login") {
  //     next();
  //   } else {
  //     next(`/login`);
  //   }
  // }
});
