import { createRouter, createWebHistory } from "vue-router";
import store from "@/store";
import { userLogin } from "@/utils/login";

const routes = [
  {
    path: "/:encryptStr?",
    name: "homePage",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/HomePage/index.vue"),
  },
  {
    path: "/videoPlay",
    name: "videoPlay",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/VideoPlay/index.vue"),
  },
  {
    // 直播视频列表
    path: "/playPlan",
    name: "playPlan",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/PlayPlan/index.vue"),
  },
  {
    path: "/reserve",
    name: "reserve",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/Reserve/index.vue"),
  },
  {
    // 栏目列表
    path: "/column",
    name: "column",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/Column/index.vue"),
  },
  {
    // 栏目详情
    path: "/columnDetail",
    name: "columnDetail",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/ColumnDetail/index.vue"),
  },
  {
    path: "/search",
    name: "search",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/Search/index.vue"),
  },
  {
    path: "/my",
    name: "my",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/My/index.vue"),
  },
  {
    // 我浏览的
    path: "/browseList",
    name: "browseList",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/BrowseList/index.vue"),
  },
  {
    // 我的关注
    path: "/myAttention",
    name: "myAttention",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/MyAttention/index.vue"),
  },
  {
    // 投顾详情
    path: "/tg",
    name: "tg",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/Tg/index.vue"),
    meta: {
      title: "投顾主页",
    },
  },
  {
    // 预览问卷
    path: "/previewQuestion",
    name: "previewQuestion",
    component: () =>
      import(
        /* webpackChunkName: "about" */ "../views/PreviewQuestion/index.vue"
      ),
    meta: {
      noLogin: true, // 不需要登录
    },
  },
  {
    // 合集详情
    path: "/serialDetail",
    name: "serialDetail",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/SerialDetail/index.vue"),
  },
  {
    // 课程详情
    path: "/courseDetail",
    name: "courseDetail",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/CourseDetail/index.vue"),
  },
  {
    // 课程详情
    path: "/courseDetail",
    name: "courseDetail",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/CourseDetail/index.vue"),
  },
  {
    // 落地页
    path: "/page",
    name: "page",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/CourseDetail/page.vue"),
  },
  // {
  //   path: "/login",
  //   name: "login",
  //   component: () =>
  //     import(/* webpackChunkName: "about" */ "../views/Login/index.vue"),
  //   meta: {
  //     title: "登录",
  //     noLogin: true, // 不需要登录
  //   },
  // },
  {
    path: "/shotVideoPlay",
    name: "shotVideoPlay",
    component: () =>
      import(
        /* webpackChunkName: "about" */ "../views/ShotVideoPlay/index.vue"
      ),
  },
  {
    path: "/VideoList",
    name: "VideoList",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/VideoList/index.vue"),
    meta: {
      title: "直播列表",
      noLogin: true, // 不需要登录
    },
  },
  {
    path: "/circle",
    name: "Circle",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/Circle/index.vue"),
  },
  {
    path: "/circle/interact",
    name: "CircleInteract",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/Circle/interact.vue"),
  },
  {
    path: "/circle/set",
    name: "CircleSet",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/Circle/set.vue"),
  },
  {
    path: "/:pathMatch(.*)*",
    redirect: "/",
  },
];

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

router.beforeEach(async (to, from, next) => {
  if (to.meta.noLogin || (to.path === "/page" && to.query.preview)) {
    return next();
  }
  if (to.query.refreshToken) {
    let ret = await store.dispatch("Login", {
      token: to.query.token,
      refreshToken: to.query.refreshToken,
    });
    if (ret && ret.code === 0) {
      delete to.query.token;
      delete to.query.refreshToken;
      return next({ path: to.path, query: to.query, replace: true });
    } else {
      userLogin();
    }
  } else if (!store.state.token && !to.meta.noLogin) {
    userLogin();
  } else {
    next();
  }
});

export default router;
