import Vue from "vue";
import VueRouter from "vue-router";
import layout from "@/layout/index.vue";
import { checkLoginStatus, initGuidInfo, doWecomLogin, getQyCode } from '@/utils/wecomLogin.ts';
Vue.use(VueRouter);

const router = new VueRouter({
  mode: process.env.NODE_ENV !== "development" ? "history" : "history",
  base: process.env.BASE_URL,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { x: 0, y: 0 };
    }
  },
  routes: [
    {
      path: "/",
      component: layout,
      redirect: "/AIDesign",
      children: [
        {
          path: "/AIDesign",
          component: () => import("../views/AIDesign/index.vue"),
          meta: { requiresAuth: true } // 标记需要登录的页面
        },
        {
          path: "/login",
          component: () => import("../views/login/index.vue"),
          meta: { title: "登录" }
        },
        {
          path: "/error/:code",
          component: () => import("../views/errorPage/index.vue")
        },
        {
          path: "/error",
          component: () => import("../views/errorPage/weixinerr.vue")
        },
        //设计页
        {
          path: "/AIDesign/design",
          component: () => import("../views/AIDesign/design.vue"),
          meta: { requiresAuth: true } // 标记需要登录的页面
        },
        //结果页
        {
          path: "/AIDesign/result",
          component: () => import("../views/AIDesign/result.vue"),
          meta: { requiresAuth: true } // 标记需要登录的页面
        },
        //历史页
        {
          path: "/AIDesign/history",
          component: () => import("../views/AIDesign/history.vue"),
          meta: { requiresAuth: true } // 标记需要登录的页面
        },
        //内墙-设计页
        {
          path: "/AIDesign/insideDesign",
          component: () => import("../views/AIDesign/insideDesign.vue"),
          meta: { requiresAuth: true } // 标记需要登录的页面
        },
        //一键诊断-生成页
        {
          path: "/AIDesign/diagnose",
          component: () => import("../views/AIDesign/diagnose.vue"),
          meta: { requiresAuth: true } // 标记需要登录的页面
        },
        //一键诊断-历史页
        {
          path: "/AIDesign/diagnoseHistory",
          component: () => import("../views/AIDesign/diagnoseHistory.vue"),
          meta: { requiresAuth: true } // 标记需要登录的页面
        },
        //一键诊断-结果页
        {
          path: "/AIDesign/diagnoseResult",
          component: () => import("../views/AIDesign/diagnoseResult.vue"),
          meta: { requiresAuth: true } // 标记需要登录的页面
        }
      ]
    },
  ]
});
// 全局前置守卫：只对需要登录的页面进行登录校验
router.beforeEach((to, from, next) => {
  // console.log("-------页面链接=", window.location.href)
  if (!to.meta || (to.meta && !to.meta.requiresAuth)) {
    next();
    return;
  }
  try {
    initGuidInfo();
    const isLoggedIn = checkLoginStatus();
    if (isLoggedIn) {
      next();
      return;
    }
    const code = to.query.code as string;
    let finalCode;
    if (Array.isArray(code)) {
      finalCode = code.length > 0 ? code[code.length - 1] : null;
    } else {
      finalCode = code || null;
    }
    if (finalCode) {
      doWecomLogin(finalCode)
        .then(() => {
          next();
          console.log(`登录成功，即将进入 ${to.path}`);
        })
        .catch(() => {
          next({ path: "/error" });
        });
    } else {
      console.log(`访问 ${to.path} 需登录，正在跳转到授权页面`);
      getQyCode();
      // 本地登录-需注释
      // doWecomLogin('')
      //   .then(() => {
      //     console.log(`模拟登录成功，即将进入 ${to.path}`);
      //   })
    }
  } catch (error) {
    console.error("登录校验过程异常:", error);
    next({ path: "/error" });
  }
});

export default router;
