"use client"; import { CreditCard, MoreVertical, Pencil, Plus, Save, Trash2, X, } from "lucide-react"; import { useState } from "react"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Card, CardContent } from "@/components/ui/card"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { cn } from "@/lib/utils"; interface PaymentMethod { id: string; type: "visa" | "mastercard" | "amex" | "paypal"; last4?: string; expiryMonth?: number; expiryYear?: number; email?: string; cardholderName?: string; isDefault: boolean; } const DEFAULT_METHODS: PaymentMethod[] = [ { id: "1", type: "visa", last4: "4242", expiryMonth: 12, expiryYear: 2026, cardholderName: "علی رضایی", isDefault: true, }, { id: "2", type: "mastercard", last4: "8888", expiryMonth: 8, expiryYear: 2025, cardholderName: "علی رضایی", isDefault: false, }, { id: "3", type: "paypal", email: "user@example.com", isDefault: false, }, ]; const PAYMENT_LOGOS: Record = { visa: "/block-media/shadcnblocks/block/ecommerce/payment-methods/visa.svg", mastercard: "/block-media/shadcnblocks/block/ecommerce/payment-methods/mastercard.svg", amex: "/block-media/shadcnblocks/block/ecommerce/payment-methods/amex.svg", paypal: "/block-media/shadcnblocks/block/ecommerce/payment-methods/paypal.svg", }; const PAYMENT_TYPE_LABELS: Record = { visa: "Visa", mastercard: "Mastercard", amex: "American Express", paypal: "PayPal", }; const formatCardLast4 = (last4?: string) => { if (!last4) return ""; return Number(last4).toLocaleString("fa-IR", { useGrouping: false }); }; const formatExpiry = (month?: number, year?: number) => { if (!month || !year) return ""; return `انقضا: ${month.toLocaleString("fa-IR")}/${year.toLocaleString("fa-IR")}`; }; interface PaymentMethods2Props { methods?: PaymentMethod[]; className?: string; } const PaymentMethods2 = ({ methods: initialMethods = DEFAULT_METHODS, className, }: PaymentMethods2Props) => { const [methods, setMethods] = useState(initialMethods); const [selectedId, setSelectedId] = useState( methods.find((m) => m.isDefault)?.id || methods[0]?.id, ); const [editingId, setEditingId] = useState(null); const [isAddingNew, setIsAddingNew] = useState(false); const [editForm, setEditForm] = useState>({}); const startEditing = (method: PaymentMethod) => { setEditingId(method.id); setEditForm({ ...method }); }; const cancelEditing = () => { setEditingId(null); setEditForm({}); }; const saveEdit = () => { if ( editingId && editForm.type && (editForm.type === "paypal" ? editForm.email : editForm.last4 && editForm.expiryMonth && editForm.expiryYear) ) { setMethods((prev) => prev.map((method) => method.id === editingId ? ({ ...method, ...editForm, } as PaymentMethod) : method, ), ); setEditingId(null); setEditForm({}); } }; const startAddingNew = () => { setIsAddingNew(true); setEditForm({ type: "visa", last4: "", expiryMonth: undefined, expiryYear: undefined, email: undefined, cardholderName: "", isDefault: methods.length === 0, }); }; const cancelAddingNew = () => { setIsAddingNew(false); setEditForm({}); }; const saveNewMethod = () => { if ( editForm.type && (editForm.type === "paypal" ? editForm.email : editForm.last4 && editForm.expiryMonth && editForm.expiryYear) ) { const newMethod: PaymentMethod = { id: Date.now().toString(), type: editForm.type as "visa" | "mastercard" | "amex" | "paypal", last4: editForm.last4, expiryMonth: editForm.expiryMonth, expiryYear: editForm.expiryYear, email: editForm.email, cardholderName: editForm.cardholderName, isDefault: methods.length === 0, }; setMethods((prev) => [...prev, newMethod]); setIsAddingNew(false); setEditForm({}); if (methods.length === 0) { setSelectedId(newMethod.id); } } }; const deleteMethod = (id: string) => { setMethods((prev) => prev.filter((method) => method.id !== id)); if (selectedId === id && methods.length > 1) { setSelectedId(methods.find((m) => m.id !== id)?.id || ""); } if (editingId === id) { setEditingId(null); setEditForm({}); } }; const setAsDefault = (id: string) => { setMethods((prev) => prev.map((method) => ({ ...method, isDefault: method.id === id, })), ); }; return (
{/* Header */}

روش‌های پرداخت

روش‌های پرداخت ذخیره‌شده خود را مدیریت کنید

{/* Payment methods grid */}
{methods.map((method) => ( editingId !== method.id && setSelectedId(method.id) } > {editingId === method.id ? (

ویرایش روش پرداخت

{editForm.type === "paypal" ? (
setEditForm({ ...editForm, email: e.target.value, }) } onClick={(e) => e.stopPropagation()} />
) : ( <>
setEditForm({ ...editForm, cardholderName: e.target.value, }) } onClick={(e) => e.stopPropagation()} />
setEditForm({ ...editForm, last4: e.target.value .replace(/\D/g, "") .slice(0, 4), }) } onClick={(e) => e.stopPropagation()} />
setEditForm({ ...editForm, expiryMonth: parseInt(e.target.value) || undefined, }) } onClick={(e) => e.stopPropagation()} />
setEditForm({ ...editForm, expiryYear: parseInt(e.target.value) || undefined, }) } onClick={(e) => e.stopPropagation()} />
)}
) : (
{ e.stopPropagation(); startEditing(method); }} > ویرایش {!method.isDefault && ( { e.stopPropagation(); setAsDefault(method.id); }} > تنظیم به‌عنوان پیش‌فرض )} { e.stopPropagation(); deleteMethod(method.id); }} > حذف
{/* Payment Logo */}
{PAYMENT_LOGOS[method.type] ? ( {PAYMENT_TYPE_LABELS[method.type]} ) : ( )}
{/* Details */}
{method.type === "paypal" ? PAYMENT_TYPE_LABELS.paypal : `${PAYMENT_TYPE_LABELS[method.type]} •••• ${formatCardLast4(method.last4)}`} {method.isDefault && ( پیش‌فرض )}

{method.type === "paypal" ? method.email : method.cardholderName ? `${method.cardholderName} • ${formatExpiry(method.expiryMonth, method.expiryYear)}` : formatExpiry( method.expiryMonth, method.expiryYear, )}

)}
))} {isAddingNew && (

افزودن روش پرداخت جدید

{editForm.type === "paypal" ? (
setEditForm({ ...editForm, email: e.target.value, }) } />
) : ( <>
setEditForm({ ...editForm, cardholderName: e.target.value, }) } />
setEditForm({ ...editForm, last4: e.target.value .replace(/\D/g, "") .slice(0, 4), }) } />
setEditForm({ ...editForm, expiryMonth: parseInt(e.target.value) || undefined, }) } />
setEditForm({ ...editForm, expiryYear: parseInt(e.target.value) || undefined, }) } />
)}
)}
{/* Add payment method button */} {!isAddingNew && ( )} {methods.length === 0 && (

روش پرداختی ذخیره نشده

برای تسویه‌حساب سریع‌تر، یک روش پرداخت اضافه کنید

)}
); }; export { PaymentMethods2 };