"use client";

import { Suspense, useEffect } from "react";
import { useSearchParams } from "next/navigation";
import Link from "next/link";
import { Header } from "../components";

function PayContent() {
  const searchParams = useSearchParams();

  useEffect(() => {
    if (typeof window !== "undefined" && searchParams.get("test") === "ok") {
      localStorage.setItem("paidType", "vip");
      window.dispatchEvent(new CustomEvent("pay:updated"));
      window.location.href = "/cloudphone";
    }
  }, [searchParams]);

  if (searchParams.get("test") === "ok") {
    return (
      <div className="flex min-h-screen items-center justify-center bg-[var(--background)]">
        <p className="text-[var(--muted-foreground)]">正在跳转...</p>
      </div>
    );
  }

  return (
    <div className="min-h-screen bg-[var(--background)] text-[var(--foreground)]">
      <Header />
      <main className="flex min-h-[80vh] flex-col items-center justify-center px-4">
        <h1 className="animate__animated animate__fadeInDown mb-2 text-2xl font-bold" style={{ animationDelay: "0.1s", animationFillMode: "both" }}>报名云手机实战课</h1>
        <p className="animate__animated animate__fadeInUp mb-6 text-[var(--muted-foreground)]" style={{ animationDelay: "0.2s", animationFillMode: "both" }}>
          加入异度星球 VIP，解锁全部 13 节视频 + 电子书 + 社群答疑
        </p>
        <div className="animate__animated animate__zoomIn mb-8 flex flex-col gap-3 sm:flex-row sm:gap-4" style={{ animationDelay: "0.35s", animationFillMode: "both" }}>
          <Link
            href="/"
            className="inline-flex items-center justify-center gap-2 rounded-full bg-[var(--accent)] px-8 py-4 text-lg font-semibold text-[var(--accent-foreground)] transition hover:opacity-90 sm:px-10"
          >
            前往首页完成支付 →
          </Link>
        </div>
        <p className="mb-4 text-xs text-[var(--muted-foreground)]">
          测试：<Link href="/cloudphone/pay?test=ok" className="text-[var(--accent)] underline">点击模拟支付成功</Link>
        </p>
        <Link href="/cloudphone" className="text-sm text-[var(--muted-foreground)] hover:text-[var(--foreground)]">
          返回课程首页
        </Link>
      </main>
    </div>
  );
}

export default function PayPage() {
  return (
    <Suspense fallback={
      <div className="flex min-h-screen items-center justify-center bg-[var(--background)]">
        <p className="text-[var(--muted-foreground)]">加载中...</p>
      </div>
    }>
      <PayContent />
    </Suspense>
  );
}
