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

const routes = [
  // 宣传页
  {
    path: "/",
    name: "home",
    component: () => import("./views/Home.vue"),
  },
  // 登录
  {
    path: "/signIn",
    name: "signIn",
    component: () => import("./views/SignIn.vue"),
  },
  {
    path: "/main",
    component: () => import(/* webpackChunkName: "main" */ "./views/Main.vue"),
    children: [
      {
        path: "/categoryManage",
        name: "categoryManage",
        component: () =>
          import(
            /* webpackChunkName: "categoryManage" */ "@/views/category/Index.vue"
          ),
      },
      {
        path: "/recycleBin",
        name: "recycleBin",
        component: () =>
          import(
            /* webpackChunkName: 'recycleBin' */ "@/views/category/recycleBin.vue"
          ),
      },
      {
        path: "/templateManagement",
        name: "templateManagement",
        component: () =>
          import(
            /* webpackChunkName: 'templateManagement' */ "@/views/category/templateManagement.vue"
          ),
      },
      {
        path: "/tracingManagement",
        name: "tracingManagement",
        component: () => import("@/views/category/tracingManagement.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: "/transferRecords",
        name: "transferRecords",
        component: () => import("@/views/Collection/TransferRecords/index.vue"),
      },
      // 隐藏老版本划转记录和通证列表
      // {
      //   path: "/passManage",
      //   name: "passManage",
      //   component: () =>
      //     import(
      //       /* webpackChunkName: "categoryManage" */ "@/views/Collection/Index.vue"
      //     ),
      //   redirect: "/passList",
      //   children: [
      //     {
      //       path: "/passList",
      //       name: "passList",
      //       component: () => import("@/views/Collection/PassList/index.vue"),
      //     },
      //     {
      //       path: "/transferRecord",
      //       name: "transferRecord",
      //       component: () =>
      //         import("@/views/Collection/TransferRecord/index.vue"),
      //     },
      //   ],
      // },
      // // 个人中心
      {
        path: "/userCenter",
        name: "userCenter",
        component: () =>
          import(
            /* webpackChunkName: "userCenter" */ "@/views/userCenter/index.vue"
          ),
      },
      {
        path: "/choosePackage",
        name: "choosePackage",
        component: () =>
          import(
            /* webpackChunkName: "companyVerify" */ "@/views/userCenter/choosePackage/index.vue"
          ),
      },
      {
        path: "/initPassword",
        name: "initPassword",
        component: () =>
          import(
            /* webpackChunkName: "initPassword" */ "@/views/userCenter/SetPassword/index.vue"
          ),
      },
      {
        path: "/companyVerify",
        name: "companyVerify",
        component: () =>
          import(
            /* webpackChunkName: "companyVerify" */ "@/views/userCenter/CompanyVerify/index.vue"
          ),
      },
      {
        path: "/editPassword",
        name: "editPassword",
        component: () =>
          import(
            /* webpackChunkName: "editPassword" */ "@/views/userCenter/EditPassword/index.vue"
          ),
      },
      {
        path: "/editPhone",
        name: "editPhone",
        component: () =>
          import(
            /* webpackChunkName: "editPhone" */ "@/views/userCenter/EditPhone/index.vue"
          ),
      },
      {
        path: "/personVerify",
        name: "personVerify",
        component: () =>
          import(
            /* webpackChunkName: "personVerify" */ "@/views/userCenter/PersonVerify/index.vue"
          ),
      },
      {
        path: "/editTemplate",
        name: "editTemplate",
        component: () =>
          import(
            /* webpackChunkName: "editTemplate" */ "@/views/editTemplate/index.vue"
          ),
      },
    ],
  },
  {
    path: "/copyrightApplication",
    name: "copyrightApplication",
    component: () => import("@/views/copyrightApplication/index.vue"),
  },
  {
    path: "/passMaker",
    name: "passMaker",
    component: () => import("@/views/Collection/PassMaker/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 };
