"use client"; import { payAgency } from "@/pages/finance/lib/api"; import { Button } from "@/shared/ui/button"; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, } from "@/shared/ui/dialog"; import { Input } from "@/shared/ui/input"; import { Label } from "@/shared/ui/label"; import { useMutation } from "@tanstack/react-query"; import { Loader2 } from "lucide-react"; import { useForm } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { toast } from "sonner"; interface PayDialogProps { open: boolean; onClose: () => void; agencyId: number; } interface PayFormValues { amount: string; // formatted value (e.g. "1 200 000") note: string; } // Narxni formatlovchi funksiya function formatPrice(value: number | string): string { const num = Number(value.toString().replace(/\D/g, "")); if (isNaN(num)) return ""; return num.toLocaleString("ru-RU"); } export function PayDialog({ open, onClose, agencyId }: PayDialogProps) { const { t } = useTranslation(); const form = useForm({ defaultValues: { amount: "", note: "", }, }); const { mutate, isPending } = useMutation({ mutationFn: ({ body, }: { body: { travel_agency: number; amount: number; note: string; }; }) => payAgency({ body }), onSuccess: () => { onClose(); }, onError: () => { toast.error(t("Xatolik yuz berdi"), { richColors: true, position: "top-center", }); }, }); const handleAmountChange = (e: React.ChangeEvent) => { const raw = e.target.value.replace(/\D/g, ""); const formatted = formatPrice(raw); form.setValue("amount", formatted); }; const handleSubmit = async (values: PayFormValues) => { const cleanAmount = Number( values.amount.replace(/\s/g, "").replace(/,/g, ""), ); mutate({ body: { amount: cleanAmount, note: values.note, travel_agency: agencyId, }, }); }; return ( {t("Qolgan summani to‘lash")}
{/* Summani kiritish */}
{form.formState.errors.amount && (

{t("Summani to‘g‘ri kiriting")}

)}
{/* Izoh */}
{/* Tugmalar */}
); }