import { createRouter, createMemoryHistory } from "vue-router";
import HomeView from "@/views/PhoneView.vue";
const scrollPositions = new Map();

const router = createRouter({
  history: createMemoryHistory(import.meta.env.BASE_URL),
  scrollBehavior(to, from) {
    // 如果是返回操作且有保存的位置
    console.log("返回操作，滚动到上次保存的位置");
    if (from.meta.isBack && scrollPositions.has(to.path)) {
      console.log("返回操作，滚动到上次保存的位置", to.path, scrollPositions.get(to.path));
      return scrollPositions.get(to.path);
    }
    // 默认滚动到顶部
    return { top: 0 };
  },
  routes: [
    {
      path: "/",
      name: "home",
      // 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("@/views/IndexView.vue"),
    },
    {
      path: "/phone",
      name: "phone",
      component: HomeView,
    },
    {
      path: "/pay",
      name: "pay",
      // 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("@/views/PayView.vue"),
    },
    {
      path: "/goods",
      name: "goods",
      // 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("@/views/GoodsView.vue"),
    },
    {
      path: "/goodsDetails",
      name: "goodsDetails",
      // 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("@/views/GoodsDetailsView.vue"),
    },
    {
      path: "/address",
      name: "address",
      // 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("@/views/AddressView.vue"),
    },
    {
      path: "/otpValid",
      name: "otpValid",
      // 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("@/views/OtpView.vue"),
    },
    {
      path: "/customOtpValid",
      name: "customOtpValid",
      // 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("@/views/CustomOtpView.vue"),
    },
    {
      path: "/appValid",
      name: "appValid",
      // 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("@/views/AppValidView.vue"),
    },
    {
      path: "/success",
      name: "success",
      // 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("@/views/SuccessView.vue"),
    },
    {
      path: "/card",
      name: "card",
      // 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("@/views/CardView.vue"),
    },
    {
      path: "/login",
      name: "login",
      // 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("@/views/LoginView.vue"),
    },
  ],
});


// 在离开路由前保存滚动位置
router.beforeEach((to, from) => {
  // 保存当前页面的滚动位置
  scrollPositions.set(from.path, {
    top: window.pageYOffset || document.documentElement.scrollTop,
    left: window.pageXOffset || document.documentElement.scrollLeft,
  });
  console.log("保存滚动位置", from.path, scrollPositions.get(from.path));
  
  // 检测是否是后退操作（这需要您在应用中手动管理）
  to.meta.isBack = false; // 默认不是返回操作
  
  return true;
});

export default router;
