"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { Film, ShoppingCart } from "lucide-react"; import { toast } from "sonner"; import { getErrorMessage } from "@/lib/errors"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { purchaseStreaming } from "@/actions/user/purchase"; import { addStreamingPlanToCart } from "@/actions/user/cart"; import { StorePlanDescription } from "./plan-card-parts"; import { PlanAvailabilityBadges } from "./plan-availability-badges"; import { usePlanAvailabilityCheck } from "./use-plan-availability-check"; import type { StreamingPlan } from "./streaming-plan-types"; interface Props { open: boolean; onOpenChange: (open: boolean) => void; plan: StreamingPlan; } export function StreamingDetailDialog({ open, onOpenChange, plan }: Props) { const [loading, setLoading] = useState(false); const [cartLoading, setCartLoading] = useState(false); const router = useRouter(); const { checking, checkAvailability } = usePlanAvailabilityCheck(plan.id); async function handlePurchase() { setLoading(true); try { const orderId = await purchaseStreaming(plan.id); router.push(`/pay/${orderId}`); } catch (error) { toast.error(getErrorMessage(error, "下单失败")); } finally { setLoading(false); } } async function handleAddToCart() { setCartLoading(true); try { await addStreamingPlanToCart(plan.id); toast.success("已加入购物车"); onOpenChange(false); router.refresh(); } catch (error) { toast.error(getErrorMessage(error, "加入购物车失败")); } finally { setCartLoading(false); } } return (
STREAMING
{plan.name} {plan.serviceName ?? plan.name} · {plan.durationDays}天 · ¥{plan.price.toFixed(0)}/{plan.durationDays}天
{plan.description && (

服务说明

)}

库存状态

{!plan.isAvailable && (

当前名额已满{plan.nextAvailableAt ? `,预计 ${plan.nextAvailableAt} 后可能补位` : ""}

)}
{!plan.isAvailable && ( )}
); }