import { getLoginCode } from "cqk-sy-ui";
import { createRouter, createWebHashHistory } from "vue-router";

const home =
  import.meta.env.VITE_hideHome === "1"
    ? {
        path: "/",
        home: "home",
        redirect: "/signIn",
      }
    : { path: "/", name: "home", component: () => import("@/views/Home.vue") };

const routes = [
  home,
  {
    path: "/signIn",
    name: "signIn",
    component: () => import("@/views/SignIn.vue"),
  },
  {
    path: "/main",
    component: () => import("@/views/Main.vue"),
    children: [
      {
        path: "/recycleBin",
        name: "recycleBin",
        component: () => import("@/views/RecycleBin/index.vue"),
      },
      {
        path: "/templateManagement",
        name: "templateManagement",
        component: () => import("@/views/TemplateManagement/index.vue"),
      },
      {
        path: "/tracingManagement",
        name: "tracingManagement",
        component: () => import("@/views/Tracing/tracingManagement/index.vue"),
      },
      // {
      //   path: "/copyrightManagement",
      //   name: "copyrightManagement",
      //   component: (
      //     () => import("@/views/copyrightManagement/index.vue")
      //   ),
      // },
      // {
      //   path: "/draftBox",
      //   name: "draftBox",
      //   component: (
      //     () => import("@/views/draftBox/index.vue")
      //   ),
      // },
      {
        path: "/collectionManagement",
        name: "collectionManagement",
        component: () =>
          import("@/views/Collection/CollectionManagement/index.vue"),
      },
      {
        path: "/albumManagement",
        name: "albumManagement",
        component: () => import("@/views/Collection/AlbumManagement/index.vue"),
      },
      {
        path: "/albumDetail",
        name: "albumDetail",
        component: () => import("@/views/Collection/AlbumDetail/index.vue"),
      },
      {
        path: "/issuedAlbumDetail",
        name: "issuedAlbumDetail",
        component: () =>
          import("@/views/Collection/IssuedBatchesDetail/index.vue"),
      },
      {
        path: "/issuedDerivativesDetail",
        name: "issuedDerivativesDetail",
        component: () =>
          import("@/views/Collection/IssuedBatchesDetail/index.vue"),
      },
      {
        path: "/transferRecords",
        name: "transferRecords",
        component: () => import("@/views/Collection/TransferRecords/index.vue"),
      },

      {
        path: "/initPassword",
        name: "initPassword",
        component: () => import("@/views/userCenter/SetPassword/index.vue"),
      },
      {
        path: "/companyVerify",
        name: "companyVerify",
        component: () => import("@/views/userCenter/CompanyVerify/index.vue"),
      },
      {
        path: "/editPassword",
        name: "editPassword",
        component: () => import("@/views/userCenter/EditPassword/index.vue"),
      },
      {
        path: "/editPhone",
        name: "editPhone",
        component: () => import("@/views/userCenter/EditPhone/index.vue"),
      },
      {
        path: "/personVerify",
        name: "personVerify",
        component: () => import("@/views/userCenter/PersonVerify/index.vue"),
      },
    ],
  },
  // {
  //   path: "/copyrightApplication",
  //   name: "copyrightApplication",
  //   component: (
  //     () => import("@/views/copyrightApplication/index.vue")
  //   ),
  // },
  {
    path: "/choosePackage",
    name: "choosePackage",
    component: () => import("@/views/userCenter/choosePackage/index.vue"),
  },
  {
    path: "/userCenter",
    name: "userCenter",
    component: () => import("@/views/userCenter/index.vue"),
  },
  {
    path: "/editTemplate",
    name: "editTemplate",
    component: () => import("@/views/editTemplate/index.vue"),
  },
  {
    path: "/tracingDetail",
    name: "tracingDetail",
    component: () => import("@/views/Tracing/tracingDetail/index.vue"),
  },
  {
    path: "/passMaker",
    name: "passMaker",
    component: () => import("@/views/Collection/PassMaker/index.vue"),
  },
  {
    path: "/test",
    name: "test",
    component: () => import("@/views/Test/index.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  const urls = ["/", "/signIn"];
  if (to.path === `/signIn` && getLoginCode()) {
    next();
  }
  if (!urls.includes(to.path) && !getLoginCode()) {
    return next({ path: "/signIn" });
  } else {
    next();
  }
});

export { router };
