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

const routes = [
  // 宣传页
  {
    path: "/",
    name: "home",
    redirect: 'signIn'
    // 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: "/passManage",
        name: "passManage",
        component: () =>
          import(
            /* webpackChunkName: "categoryManage" */ "@/views/pass/Index.vue"
          ),
        redirect: "/passList",
        children: [
          {
            path: "/passList",
            name: "passList",
            component: () => import("@/views/pass/PassList/index.vue"),
          },
          {
            path: "/transferRecord",
            name: "transferRecord",
            component: () => import("@/views/pass/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: "/passMaker",
    name: "passMaker",
    component: () => import("@/views/pass/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 };
