import { useState, useCallback } from "react"; import Box from "@mui/material/Box"; import Typography from "@mui/material/Typography"; import IconButton from "@mui/material/IconButton"; import useMediaQuery from "@mui/material/useMediaQuery"; import { useTheme } from "@mui/material/styles"; import { Modal, Button, TextField, Field } from "../ui"; 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 theme = useTheme(); const isMobile = useMediaQuery(theme.breakpoints.down("sm")); 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? ) : ( {isMobile ? ( {items.map((item, i) => ( Položka {i + 1} removeItem(i)} title="Odstranit" aria-label="Odstranit" > updateItem(i, "description", e.target.value) } /> updateItem(i, "quantity", Number(e.target.value) || 0) } slotProps={{ htmlInput: { step: "0.001" } }} /> updateItem(i, "unit", e.target.value)} /> updateItem(i, "unit_price", Number(e.target.value) || 0) } slotProps={{ htmlInput: { step: "0.01" } }} /> updateItem(i, "vat_rate", Number(e.target.value) || 0) } slotProps={{ htmlInput: { step: "1" } }} /> ))} ) : ( Popis Mn. Jedn. Cena %DPH {items.map((item, i) => ( updateItem(i, "description", e.target.value) } sx={{ minWidth: 200 }} /> updateItem( i, "quantity", Number(e.target.value) || 0, ) } sx={{ width: 90 }} slotProps={{ htmlInput: { step: "0.001" } }} /> updateItem(i, "unit", e.target.value) } sx={{ width: 70 }} /> updateItem( i, "unit_price", Number(e.target.value) || 0, ) } sx={{ width: 110 }} slotProps={{ htmlInput: { step: "0.01" } }} /> updateItem( i, "vat_rate", Number(e.target.value) || 0, ) } sx={{ width: 80 }} slotProps={{ htmlInput: { step: "1" } }} /> removeItem(i)} title="Odstranit" aria-label="Odstranit" > ))} )} )} ); }