// src/router/index.ts
import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
import Layout from "@/components/Layout.vue";
import { Components } from "ant-design-vue/es/date-picker/generatePicker";

const routes: RouteRecordRaw[] = [
  {
    path: "/",
    // component: HomeView  // 这个页面独立，不使用Layout
    redirect: "/layout/home",
  },
  {
    path: "/login",
    name: "Login",
    component: () => import("@/views/login/index.vue"),
  },
  {
    path: "/404",
    name: "404",
    component: () => import("@/views/404.vue"),
  },
  {
    path: "/:pathMatch(.*)*",
    name: "NotFound",
    component: () => import("@/views/404.vue"),
  },
  {
    path: "/document",
    name: "document",
    component: () => import("@/views/document/index.vue"),
    redirect: "/document/introdution",
    children: [
      {
        path: "introdution",
        name: "introdution",
        component: () => import("@/views/document/introdution.vue"),
      },
      {
        path: "start",
        name: "start",
        component: () => import("@/views/document/start.vue"),
      },
      {
        path: "video",
        name: "video",
        component: () => import("@/views/document/video.vue"),
      },
      {
        path: "study",
        name: "study",
        component: () => import("@/views/document/study.vue"),
      },
      {
        path: "select",
        name: "select",
        component: () => import("@/views/document/select.vue"),
      },
      {
        path: "ceshi",
        name: "ceshi",
        component: () => import("@/views/document/ceshi.vue"),
      },
      {
        path: "summary",
        name: "summary",
        component: () => import("@/views/document/summary.vue"),
      },
      {
        path: "jupyterLab",
        name: "jupyterLab",
        component: () => import("@/views/document/jupyterLab.vue"),
      },
      {
        path: "SSH",
        name: "SSH",
        component: () => import("@/views/document/ssh.vue"),
      },
      {
        path: "member",
        name: "member",
        component: () => import("@/views/document/member.vue"),
      },
    ],
  },
  {
    path: "/layout",
    name: "Layout",
    component: () => import("@/views/layout/index.vue"),
    children: [
      {
        path: "home",
        name: "LayoutHome",
        component: () => import("@/views/home/index.vue"),
      },
      {
        path: "market",
        name: "LayoutMarket",
        component: () => import("@/views/market/index.vue"),
      },
      {
        path: "create",
        name: "Create",
        component: () => import("@/views/instanceCreate/index.vue"),
      },
      {
        path: "admin",
        name: "Admin",
        component: () => import("@/views/admin/index.vue"),
        children: [
          {
            path: "",
            redirect: "home",
          },
          {
            path: "home",
            name: "AdminHome",
            component: () => import("@/views/admin/home/index.vue"),
          },
          {
            path: "instance",
            name: "Instance",
            component: () => import("@/views/admin/instance/index.vue"),
          },
          {
            path: "instanceCreate",
            name: "InstanceCreate",
            component: () => import("@/views/admin/instanceCreate/index.vue"),
          },
          // {
          //   path: "fileStore",
          //   name: "FileStore",
          //   component: () => import("@/views/admin/fileStore/index.vue"),
          // },
          {
            path: "history",
            name: "History",
            component: () => import("@/views/admin/account/history/index.vue"),
          },
          {
            path: "security",
            name: "Security",
            component: () => import("@/views/admin/account/security/index.vue"),
          },
          {
            path: "realnameAuth",
            name: "RealnameAuth",
            component: () =>
              import("@/views/admin/account/realnameAuth/index.vue"),
          },
          {
            path: "enterRealAuth",
            name: "EnterRealAuth",
            component: () =>
              import("@/views/admin/account/enterRealAuth/index.vue"),
          },
          {
            path: "image",
            name: "Image",
            component: () => import("@/views/admin/image/index.vue"),
          },
          {
            path: "contract",
            name: "contract",
            component: () =>
              import("@/views/admin/account/cost/contract/index.vue"),
          },
          {
            path: "costDetail",
            name: "costDetail",
            component: () =>
              import("@/views/admin/account/cost/costDetail/index.vue"),
          },
          {
            path: "coupon",
            name: "coupon",
            component: () =>
              import("@/views/admin/account/cost/coupon/index.vue"),
          },
          {
            path: "flow",
            name: "flow",
            component: () =>
              import("@/views/admin/account/cost/flow/index.vue"),
          },
          {
            path: "invoice",
            name: "invoice",
            component: () =>
              import("@/views/admin/account/cost/invoice/index.vue"),
          },

          {
            path: "myOrder",
            name: "myOrder",
            component: () =>
              import("@/views/admin/account/cost/myOrder/index.vue"),
          },
          {
            path: "voucher",
            name: "voucher",
            component: () =>
              import("@/views/admin/account/cost/voucher/index.vue"),
          },
          {
            path: "growthValue",
            name: "growthValue",
            component: () =>
              import("@/views/admin/growthValue/index.vue"),
          },
        ],
      },
    ],
  },

  // {
  //   path: '/fileStore',
  //   name: 'FileStore',
  //   component: Layout,  // 使用Layout包装FileStore
  //   children: [
  //     {
  //       path: '',  // 空路径，访问 /fileStore 时显示FileStore组件
  //       name: 'FileStoreContent',
  //       component: FileStore
  //     },
  //     {
  //       path: '/accountSecurity',
  //       name: 'AccountSecurity',
  //       component: AccountSecurity
  //     },
  //      {
  //       path: '/accountHistory',
  //       name: 'AccountHistory',
  //       component: AccountHistory
  //     }
  //   ]
  // },
  // // 可以继续添加其他使用Layout的路由
  // {
  //   path: '/container',
  //   name: 'Container',
  //   component: Layout,
  //   children: [
  //     {
  //       path: '',
  //       name: 'ContainerContent',
  //       component: () => import('@/views/controlPanel/container/index.vue')
  //     }
  //   ]
  // }
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
});
// ====== 添加全局前置守卫 ======
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem("token"); // 或从 pinia/vuex 获取
  const isLoginPage = to.path === "/login";
  if (!token && !isLoginPage) {
    // 没有 token 且不是去登录页 → 跳转登录
    next({ path: "/login" });
  } else if (token && isLoginPage) {
    // 已登录却访问登录页 → 跳转首页（可选）
    next({ path: "/layout/home" });
  } else {
    // 正常访问
    next();
  }
});
export default router;
