"use client"; import { useState, useTransition } from "react"; import { useRouter } from "next/navigation"; import { submitCheckout } from "@/lib/api/checkout"; import type { Address, CartGroup, PaymentMethod } from "@/types/api"; /** Exact labels per the OpenAPI PaymentMethodEnum description ("(آنلاین)" added to match the screenshot). */ const PAYMENT_OPTIONS: { value: PaymentMethod; label: string }[] = [ { value: "wallet", label: "کیف پول وینسو" }, { value: "online", label: "درگاه بانکی (آنلاین)" }, { value: "cash_on_delivery", label: "پرداخت در محل" }, ]; export function CheckoutPayment({ group, addresses }: { group: CartGroup; addresses: Address[] }) { const router = useRouter(); const [method, setMethod] = useState("wallet"); const [error, setError] = useState(null); const [isPending, startTransition] = useTransition(); // Pickup flow — no delivery fee, matches the fulfillment screen's "دریافت حضوری" total. const payable = group.items_subtotal; function handlePay() { setError(null); const addressUuid = addresses.find((a) => a.is_default)?.uuid ?? addresses[0]?.uuid; if (!addressUuid) { setError("برای ثبت سفارش ابتدا یک آدرس اضافه کنید."); return; } startTransition(async () => { // submitCheckout processes the whole cart at once (splits into per-store orders // server-side), not just this store — see PageProps note on the confirm page. const result = await submitCheckout({ address_uuid: addressUuid, delivery_type: "express", scheduled_at: null, payment_method: method, notes: "", }); if (result.ok) { router.push(`/order-groups/${result.data.uuid}`); } else { setError(result.message); } }); } return (

روش پرداخت

{PAYMENT_OPTIONS.map((option) => { const selected = method === option.value; return ( ); })}
{error &&

{error}

}
{group.items.length.toLocaleString("fa-IR")} کالا از {group.store.name} {payable.toLocaleString("fa-IR")} تومان
جمع کل {payable.toLocaleString("fa-IR")} تومان
); }