"use client"; import { useState, useTransition } from "react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { submitCheckout } from "@/lib/api/checkout"; import { Button } from "@/components/ui/button"; import type { Address, Cart, DeliveryType, PaymentMethod } from "@/types/api"; const PAYMENT_LABELS_FA: Record = { wallet: "کیف پول وینوفای", online: "درگاه بانکی", cash_on_delivery: "پرداخت در محل", }; export function CheckoutForm({ cart, addresses }: { cart: Cart; addresses: Address[] }) { const router = useRouter(); const [addressUuid, setAddressUuid] = useState(addresses.find((a) => a.is_default)?.uuid ?? addresses[0]?.uuid ?? ""); const [deliveryType, setDeliveryType] = useState("express"); const [scheduledAt, setScheduledAt] = useState(""); const [paymentMethod, setPaymentMethod] = useState("cash_on_delivery"); const [notes, setNotes] = useState(""); const [error, setError] = useState(null); const [isPending, startTransition] = useTransition(); function handleSubmit(e: React.FormEvent) { e.preventDefault(); setError(null); if (!addressUuid) { setError("لطفاً یک آدرس انتخاب کنید."); return; } if (deliveryType === "scheduled" && !scheduledAt) { setError("لطفاً زمان ارسال را مشخص کنید."); return; } startTransition(async () => { const result = await submitCheckout({ address_uuid: addressUuid, delivery_type: deliveryType, scheduled_at: deliveryType === "scheduled" ? new Date(scheduledAt).toISOString() : null, payment_method: paymentMethod, notes, }); if (result.ok) { router.push(`/order-groups/${result.data.uuid}`); } else { setError(result.message); } }); } if (addresses.length === 0) { return (

برای ثبت سفارش ابتدا یک آدرس اضافه کنید.

); } return (

آدرس تحویل

{addresses.map((address) => ( ))} مدیریت آدرس‌ها

زمان ارسال

{deliveryType === "scheduled" && ( setScheduledAt(e.target.value)} className="h-11 rounded-xl border border-border bg-background px-3 text-sm" /> )}

روش پرداخت

{(Object.keys(PAYMENT_LABELS_FA) as PaymentMethod[]).map((method) => ( ))}

توضیحات (اختیاری)