import { useEffect, useState } from "react"; import "../../styles/ConfirmBuyModal.css"; export default function ConfirmBuyModal({ item, onConfirm, onCancel, allItems = [], onNavigate }) { const [quantity, setQuantity] = useState(item.quantity); const maxQuantity = item.quantity; // Update quantity when item changes (navigation) useEffect(() => { setQuantity(item.quantity); }, [item.id, item.quantity]); // Find current index and check for prev/next const currentIndex = allItems.findIndex(i => i.id === item.id); const hasPrev = currentIndex > 0; const hasNext = currentIndex < allItems.length - 1; const handleIncrement = () => { if (quantity < maxQuantity) { setQuantity(prev => prev + 1); } }; const handleDecrement = () => { if (quantity > 1) { setQuantity(prev => prev - 1); } }; const handleConfirm = () => { onConfirm(quantity); }; const handlePrev = () => { if (hasPrev && onNavigate) { const prevItem = allItems[currentIndex - 1]; onNavigate(prevItem); } }; const handleNext = () => { if (hasNext && onNavigate) { const nextItem = allItems[currentIndex + 1]; onNavigate(nextItem); } }; const imageUrl = item.item_image && item.image_mime_type ? `data:${item.image_mime_type};base64,${item.item_image}` : null; return (