import { useState, useCallback } from "react"; import FormModal from "./FormModal"; import { useAlert } from "../context/AlertContext"; interface ConfirmationItem { description: string; quantity: number; unit: string; unit_price: number; is_included_in_total: boolean; vat_rate: number; } interface OrderConfirmationModalProps { isOpen: boolean; onClose: () => void; onGenerate: ( lang: string, applyVat: boolean, items?: ConfirmationItem[], ) => Promise; initialItems: ConfirmationItem[]; orderNumber: string; defaultVatRate: number; applyVat: boolean; } export default function OrderConfirmationModal({ isOpen, onClose, onGenerate, initialItems, orderNumber, defaultVatRate, applyVat, }: OrderConfirmationModalProps) { const alert = useAlert(); const [step, setStep] = useState<"choose" | "edit">("choose"); const [lang, setLang] = useState("cs"); const [applyVatState, setApplyVatState] = useState(applyVat); const [items, setItems] = useState(initialItems); const [loading, setLoading] = useState(false); const handleUseExisting = async () => { setLoading(true); try { await onGenerate(lang, applyVatState, undefined); } catch (err) { console.error("Chyba při generování potvrzení:", err); alert.error("Nepodařilo se vygenerovat potvrzení"); } finally { setLoading(false); setStep("choose"); onClose(); } }; const handleEditGenerate = async () => { setLoading(true); try { await onGenerate(lang, applyVatState, items); } catch (err) { console.error("Chyba při generování potvrzení:", err); alert.error("Nepodařilo se vygenerovat potvrzení"); } finally { setLoading(false); setStep("choose"); onClose(); } }; const updateItem = useCallback( ( index: number, field: keyof ConfirmationItem, value: string | number | boolean, ) => { setItems((prev) => { const next = [...prev]; next[index] = { ...next[index], [field]: value }; return next; }); }, [], ); const removeItem = useCallback((index: number) => { setItems((prev) => prev.filter((_, i) => i !== index)); }, []); const addItem = useCallback(() => { setItems((prev) => [ ...prev, { description: "", quantity: 1, unit: "ks", unit_price: 0, is_included_in_total: true, vat_rate: defaultVatRate, }, ]); }, [defaultVatRate]); return ( {step === "choose" ? (

Jak chcete připravit potvrzení objednávky?

) : (
{items.map((item, i) => ( ))}
Popis Mn. Jedn. Cena %DPH
updateItem(i, "description", e.target.value) } className="admin-form-input" style={{ minWidth: "200px" }} /> updateItem(i, "quantity", Number(e.target.value) || 0) } className="admin-form-input" style={{ width: "80px" }} step="0.001" /> updateItem(i, "unit", e.target.value)} className="admin-form-input" style={{ width: "60px" }} /> updateItem( i, "unit_price", Number(e.target.value) || 0, ) } className="admin-form-input" style={{ width: "100px" }} step="0.01" /> updateItem(i, "vat_rate", Number(e.target.value) || 0) } className="admin-form-input" style={{ width: "70px" }} step="1" />
)}
); }