"use client"; import { Pencil, Plus, Save, Shield, Star, X } from "lucide-react"; import { useState } from "react"; import { CreditCard, CreditCardBack, CreditCardChip, CreditCardCvv, CreditCardExpiry, CreditCardFlipper, CreditCardFront, CreditCardMagStripe, CreditCardName, CreditCardNumber, } from "@/components/kibo-ui/credit-card"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { cn } from "@/lib/utils"; interface PaymentMethod { id: string; bank: "wells-fargo" | "chase"; type: "visa" | "mastercard"; last4: string; expiryMonth: number; expiryYear: number; cardholderName: string; cvv: string; isDefault: boolean; } const DEFAULT_METHODS: PaymentMethod[] = [ { id: "1", bank: "wells-fargo", type: "mastercard", last4: "8888", expiryMonth: 12, expiryYear: 2026, cardholderName: "علی ر. رضایی", cvv: "123", isDefault: true, }, { id: "2", bank: "chase", type: "visa", last4: "4242", expiryMonth: 8, expiryYear: 2025, cardholderName: "علی ر. رضایی", cvv: "456", isDefault: false, }, ]; const BANK_LABELS: Record = { "wells-fargo": "Wells Fargo", chase: "Chase", }; const PAYMENT_TYPE_LABELS: Record = { visa: "Visa", mastercard: "Mastercard", }; const formatCardLast4 = (last4?: string) => { if (!last4) return ""; return Number(last4).toLocaleString("fa-IR", { useGrouping: false }); }; const formatExpiryShort = (month?: number, year?: number) => { if (!month || !year) return ""; const paddedMonth = month.toLocaleString("fa-IR", { minimumIntegerDigits: 2, useGrouping: false, }); const shortYear = (year % 100).toLocaleString("fa-IR", { minimumIntegerDigits: 2, useGrouping: false, }); return `${paddedMonth}/${shortYear}`; }; interface PaymentMethods3Props { methods?: PaymentMethod[]; title?: string; className?: string; } const PaymentMethods3 = ({ methods: initialMethods = DEFAULT_METHODS, title = "انتخاب روش پرداخت", className, }: PaymentMethods3Props) => { const [methods, setMethods] = useState(initialMethods); const [selectedId, setSelectedId] = useState( methods.find((m) => m.isDefault)?.id || methods[0]?.id, ); const [editingId, setEditingId] = useState(null); const [editForm, setEditForm] = useState>({}); const startEditing = (method: PaymentMethod) => { setEditingId(method.id); setEditForm({ ...method }); }; const cancelEditing = () => { setEditingId(null); setEditForm({}); }; const saveEdit = () => { if ( editingId && editForm.cardholderName && editForm.last4 && editForm.expiryMonth && editForm.expiryYear && editForm.cvv ) { setMethods((prev) => prev.map((method) => method.id === editingId ? ({ ...method, ...editForm, } as PaymentMethod) : method, ), ); setEditingId(null); setEditForm({}); } }; const setAsDefault = (id: string) => { setMethods((prev) => prev.map((method) => ({ ...method, isDefault: method.id === id, })), ); }; const getCardBackground = (bank: string): string => { switch (bank) { case "chase": return "bg-[#063573]"; case "wells-fargo": return "bg-gradient-to-br from-[#D71E2B] to-[#8B0000]"; default: return "bg-[#063573]"; } }; const getBankLogo = (bank: string) => { switch (bank) { case "chase": return ( {BANK_LABELS["chase"]} ); case "wells-fargo": return ( {BANK_LABELS["wells-fargo"]} ); default: return null; } }; const getPaymentProviderLogo = (type: string) => { switch (type) { case "visa": return ( {PAYMENT_TYPE_LABELS.visa} ); case "کارت اعتباری": return ( {PAYMENT_TYPE_LABELS.mastercard} ); default: return null; } }; return (
{/* Header */}

{title}

{/* Cards */}
{methods.map((method) => (
{/* Card Details on Left */}
{/* Edit Button - Top Aligned */} {editingId !== method.id && (
)} {/* Card Details Below */}
{/* Selection Indicator */}
{ setSelectedId(method.id); setAsDefault(method.id); }} > {selectedId === method.id && (
)}

{BANK_LABELS[method.bank]}{" "} {PAYMENT_TYPE_LABELS[method.type]}

{method.isDefault && ( پیش‌فرض )}

•••• •••• •••• {formatCardLast4(method.last4)}

{editingId === method.id ? (

ویرایش جزئیات کارت

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()} />
setEditForm({ ...editForm, cvv: e.target.value .replace(/\D/g, "") .slice(0, 4), }) } onClick={(e) => e.stopPropagation()} />
) : (

دارنده کارت

{method.cardholderName}

انقضا

{formatExpiryShort( method.expiryMonth, method.expiryYear, )}

)}
{/* Flipping Card on Right */}
{/* Bank Logo */}
{getBankLogo(method.bank)}
{/* Payment Provider Logo */}
{getPaymentProviderLogo(method.type)}
{/* Cardholder Name */} {method.cardholderName}
{/* Card Number on Back */} •••• •••• •••• {method.last4} {/* Expiry and CVV */}
{method.expiryMonth.toString().padStart(2, "0")}/ {method.expiryYear.toString().slice(-2)} {method.cvv}
))}
{/* Add new card button */} {/* Security note */}
اطلاعات پرداخت شما به‌صورت امن ذخیره می‌شود
); }; export { PaymentMethods3 };