import { createRouter, createWebHistory } from "vue-router";

import store from "@/store";

import NProgress from "nprogress";
import "nprogress/nprogress.css";

import Home from "../views/Home/index.vue";
import Card from "../views/Card/Index.vue";
import Auth from "../views/Auth/Index.vue";

const routes = [
  {
    path: "/",
    name: "Home",
    component: Home,
  },
  {
    path: "/send",
    name: "Send",
    component: () =>
      import("../views/Send/index.vue"),
  },
  {
    path: "/shop",
    name: "Shop",
    component: () =>
      import("../views/Shop/index.vue"),
  },
  {
    path: "/shop/inputsn",
    name: "InputSN",
    component: () =>
      import("../views/Shop/Inputsn.vue"),
  },
  {
    path: "/member",
    name: "Member",
    component: () =>
      import("../views/Member/index.vue"),
  },
  {
    path: "/card",
    name: "Card",
    component: Card,
    children: [
      {
        path: "/card/edit",
        name: "CardEdit",
        component: () =>
          import("../views/Card/Edit.vue"),
        meta: { keepAlive: true },
      },
      {
        path: "/card/notice",
        name: "CardNotice",
        component: () =>
          import("../views/Card/Notice.vue"),
      },
      {
        path: "/card/video",
        name: "CardVideo",
        component: () =>
          import("../views/Card/Video.vue"),
        meta: { keepAlive: true },
      },
      {
        path: "/card/preview",
        name: "CardPreview",
        component: () =>
          import("@/components/Preview.vue"),
        meta: { keepAlive: true },
      },
    ],
  },
  {
    path: "/auth",
    name: "Auth",
    component: Auth,
    children: [
      {
        path: "/auth/auth",
        name: "Auth",
        component: () => import("../views/Auth/Auth.vue")
      },
      {
        path: "/auth/getauth",
        name: "GetAuth",
        component: () => import("../views/Auth/GetAuth.vue"),
        meta: { keepAlive: true },
      },
      {
        path: "/auth/edit",
        name: "AuthEdit",
        component: () => import("../views/Auth/Edit.vue"),
        meta: { keepAlive: true },
      },
      {
        path: "/auth/preview",
        name: "AuthPreview",
        component: () =>
          import("@/components/Preview.vue"),
        meta: { keepAlive: true },
      },
    ],
  },
  {
    path: "/register",
    name: "Register",
    component: () =>
      import("../views/Register/index.vue"),
    // component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path: "/login",
    name: "Login",
    component: () =>
      import("../views/Login/index.vue"),
  },
  {
    path: "/test",
    name: "Test",
    component: () => import("../views/Test.vue"),
  },
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
});

router.beforeEach((to, from, next) => {
  NProgress.start();

  if (to.path !== "/" && to.path !== "/login" && to.path !== "/register") {
    if (!sessionStorage.getItem("token")) {
      next("/");
    }

    // if(!store.state.user.userInfo){
    //   await store.dispatch('user/getUserInfo')
    // }
  }

  next();
});

router.afterEach(() => {
  NProgress.done();
});

export default router;
