import { createRouter, createWebHashHistory } from "vue-router";
import Dashboard from "@/views/dashboard/Dashboard.vue";
import History from "@/views/history/History.vue";
import Operation from "@/views/operation/Operation.vue";
import GraphManagement from "@/views/graphManagement/GraphManagement.vue";
import ReportManagement from "@/views/reportManagement/ReportManagement.vue";
import AccountManagement from "@/views/accountManagement/AccountManagement.vue";
import AssetManagement from "@/views/AssetManagement/AssetManagement.vue";
import AlertManagement from "@/views/alert/AlertManagement.vue";
import ProductSetting from "@/views/productSetting/ProductSetting.vue";
import Login from "@/views/login/Login.vue";
import useUserInfoStore from "@/stores/useUserInfoStore";
import useGetCookie from "@/hooks/useGetCookie";

import Test from "@/views/Test.vue";

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  // linkActiveClass: "is-active",
  routes: [
    {
      path: "/login",
      name: "login",
      component: Login,
    },
    {
      path: "/dashboard",
      index: true,
      name: "dashboard",
      component: Dashboard,
    },
    {
      path: "/historyData",
      name: "history",
      component: History,
    },
    {
      path: "/reportManagement",
      name: "reportManagement",
      component: ReportManagement,
    },
    {
      path: "/operation",
      name: "operation",
      component: Operation,
    },
    {
      path: "/graphManagement",
      name: "graphManagement",
      component: GraphManagement,
    },
    {
      path: "/accountManagement",
      name: "accountManagement",
      component: AccountManagement,
    },
    {
      path: "/assetManagement",
      name: "assetManagement",
      component: AssetManagement,
    },
    {
      path: "/alert",
      name: "alert",
      component: AlertManagement,
    },
    {
      path: "/productSetting",
      name: "productSetting",
      component: ProductSetting,
    },
    {
      path: "/mytestfile/mjm",
      name: "mytestfile",
      component: Test,
    },
  ],
});

router.beforeEach(async (to, from, next) => {
  console.log("route", to, location, document.cookie);
  // redirect to login page if not logged in and trying to access a restricted page
  const publicPages = ["/login", "/"];
  const authRequired = !publicPages.includes(to.path);
  const auth = useUserInfoStore();
  const token = useGetCookie("JWT-Authorization");

  if (location.pathname.includes("logoutConfirm")) {
    document.cookie = "JWT-Authorization=";
    auth.user.token = "";
    auth.clearAuthPage && auth.clearAuthPage();
    // 清除 cookie token
    next({ path: "/login" });
  }

  if ((authRequired && !token) || to.path === "/") {
    auth.user.token = "";
    auth.clearAuthPage && auth.clearAuthPage();
    next({ path: "/login" });
  } else if (!authRequired) {
    document.cookie = "JWT-Authorization=";
    auth.user.token = "";
    auth.clearAuthPage && auth.clearAuthPage();
  } else {
    auth.user.token = token;
  }
  next();
});

export default router;
