"use client"; import { useState, useTransition } from "react"; import { useRouter } from "next/navigation"; import { toast } from "sonner"; import { createAdminRechargeCards } from "@/actions/admin/recharge-cards"; import { BooleanToggle } from "@/components/ui/boolean-toggle"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { getErrorMessage } from "@/lib/errors"; interface PlanOption { id: string; name: string; type: "PROXY" | "STREAMING"; remainingCount: number | null; } export function RechargeCardForm({ plans }: { plans: PlanOption[] }) { const router = useRouter(); const [type, setType] = useState<"BALANCE" | "PLAN">("BALANCE"); const [planId, setPlanId] = useState(plans[0]?.id ?? ""); const [hasExpiry, setHasExpiry] = useState(false); const [pending, startTransition] = useTransition(); const selectedPlan = plans.find((plan) => plan.id === planId) ?? null; const planSoldOut = type === "PLAN" && selectedPlan?.remainingCount === 0; return (
{ event.preventDefault(); const form = event.currentTarget; const formData = new FormData(form); formData.set("type", type); if (type === "PLAN") formData.set("planId", planId); startTransition(async () => { try { await createAdminRechargeCards(formData); toast.success("充值卡已生成"); form.reset(); setHasExpiry(false); router.refresh(); } catch (error) { toast.error(getErrorMessage(error, "生成充值卡失败")); } }); }} >

生成充值卡

余额卡充值钱包,套餐卡兑换后直接激活套餐。

setType(value ? "PLAN" : "BALANCE")} trueLabel="套餐卡" falseLabel="余额卡" ariaLabel="充值卡类型" className="w-36" />
{type === "BALANCE" ? (
) : (
{selectedPlan && (

{selectedPlan.remainingCount == null ? "库存不限" : `当前可生成 ${selectedPlan.remainingCount} 张`}

)}
)}
{hasExpiry && (
)} {!hasExpiry && ( )} {type === "PLAN" && plans.length === 0 && (
暂无可用套餐,请先上架套餐。
)}
); }