"use client"; import { zodResolver } from "@hookform/resolvers/zod"; import { ArrowLeft, Lock } from "lucide-react"; import React from "react"; import { Controller, FormProvider, useForm, useFormContext, } from "react-hook-form"; import z from "zod"; import { Button } from "@/components/ui/button"; import { Field, FieldError, FieldLabel } from "@/components/ui/field"; import { Input } from "@/components/ui/input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Separator } from "@/components/ui/separator"; import { cn } from "@/lib/utils"; interface OrderItem { id: string; name: string; price: number; } interface OrderSummary { companyName: string; items: OrderItem[]; currency: string; } interface Checkout5Props { order: OrderSummary; className?: string; } const formatPrice = (price: number, currency = "IRR") => new Intl.NumberFormat("fa-IR", { style: "currency", currency, }).format(price); const checkoutFormSchema = z.object({ email: z.string().email("ایمیل نامعتبر است"), cardNumber: z.string().min(1, "شماره کارت الزامی است"), expiryDate: z .string() .regex(/^(0[1-9]|1[0-2])\/\d{2}$/, "فرمت نامعتبر (MM/YY)") .refine((value) => { const [mm, yy] = value.split("/").map(Number); const now = new Date(); const currentMonth = now.getMonth() + 1; const currentYear = now.getFullYear() % 100; if (yy < currentYear) return false; if (yy === currentYear && mm < currentMonth) return false; return true; }, "کارت منقضی شده است"), cvc: z.string().min(3, "کد امنیتی الزامی است"), cardholderName: z.string().min(1, "نام روی کارت الزامی است"), country: z.string().min(1, "انتخاب کشور الزامی است"), promoCode: z.string().optional(), }); type CheckoutFormType = z.infer; const COUNTRIES = [ { value: "AU", label: "استرالیا" }, { value: "IR", label: "ایران" }, { value: "TR", label: "ترکیه" }, { value: "IQ", label: "عراق" }, { value: "PK", label: "پاکستان" }, { value: "AFG", label: "افغانستان" }, ]; const DEFAULT_ORDER: OrderSummary = { companyName: "ریو آی", items: [ { id: "pro-subscription", name: "اشتراک حرفه‌ای (سالانه)", price: 199.0, }, ], currency: "IRR", }; const Checkout5 = ({ order = DEFAULT_ORDER, className }: Checkout5Props) => { const form = useForm({ resolver: zodResolver(checkoutFormSchema), defaultValues: { email: "", cardNumber: "", expiryDate: "ماه/سال", cvc: "کد امنیتی", cardholderName: "", country: "IR", promoCode: "", }, }); const onSubmit = (data: CheckoutFormType) => { console.log("Form submitted:", data); }; const subtotal = order.items.reduce((sum, item) => sum + item.price, 0); const total = subtotal; return (
{order.companyName}
{/* Company and Total */}

{order.companyName}

{formatPrice(total, order.currency)}

{/* Order Items */}
{order.items.map((item) => (
{item.name} {formatPrice(item.price, order.currency)}
))}
{/* Subtotal */}
جمع سفارش {formatPrice(subtotal, order.currency)}
{/* Promo Code Button */}
{/* Total */}
مبلغ قابل پرداخت {formatPrice(total, order.currency)}
با کارت بانکی پرداخت کنید

جزئیات کارت

با ثبت سفارش، شرایط استفاده و سیاست حفظ حریم خصوصی را می‌پذیرید. اطلاعات پرداخت شما با استانداردهای امنیتی محافظت و رمزنگاری می‌شود.

پرداخت امن اشتراک شرایط استفاده حریم خصوصی
); }; const PromoCodeField = () => { const form = useFormContext(); const [showInput, setShowInput] = React.useState(false); if (!showInput) { return ( ); } return ( (
)} /> ); }; const EmailField = () => { const form = useFormContext(); return ( ( ایمیل {fieldState.invalid && } )} /> ); }; const PaymentFields = () => { const form = useFormContext(); return (
{/* Card Number */} (
شماره کارت
Visa Mastercard کارت ویژه
{fieldState.invalid && }
)} /> {/* Expiry and CVC Row */}
( { let val = e.target.value; val = val.replace(/[^0-9/]/g, ""); const prev = field.value ?? ""; const isDeleting = val.length < prev.length; if (!isDeleting) { if (val.length === 2 && !val.includes("/")) { val = val + "/"; } } if (val.length > 5) { val = val.slice(0, 5); } field.onChange(val); }} placeholder="MM/YY" dir="rtl" className="rounded-none rounded-bl-md border-r-0" aria-label="تاریخ انقضا" aria-invalid={fieldState.invalid} /> {fieldState.invalid && } )} /> ( {fieldState.invalid && } )} />
{/* Cardholder Name */} ( نام روی کارت {fieldState.invalid && } )} />
); }; const CountryField = () => { const form = useFormContext(); return ( ( کشور صورتحساب {fieldState.invalid && } )} /> ); }; export { Checkout5 };