import React, { useState, useRef } from "react"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faBoxesStacked, faCheckCircle, faCheckToSlot, faClipboardQuestion, faShuffle, } from "@fortawesome/free-solid-svg-icons"; import { ShipmentRequest } from "@/interfaces/interfaces"; interface ShipmentItemsAccordionProps { selectedShipment: ShipmentRequest; buttonStates: { [itemId: number]: any }; setButtonStates: React.Dispatch>; parentModalHandle: () => void; } export default function ShipmentItemsAccordion({ selectedShipment, buttonStates, setButtonStates, parentModalHandle, }: ShipmentItemsAccordionProps) { // Track which accordion item (by id) is open. Default to first item. const [openAccordion, setOpenAccordion] = useState( selectedShipment.items[0]?.id || null ); // Track completed items by their id. const [completedItems, setCompletedItems] = useState<{ [id: number]: boolean }>({}); // Ref to store a hold timer for touch devices. const holdTimerRef = useRef(null); // Modal state const [modalPositionOpen, setModalPositionOpen] = useState(false); const [currentItem, setCurrentItem] = useState(null); const [fromPosition, setFromPosition] = useState(""); const [toPosition, setToPosition] = useState(""); // modalMode: "full" or "select-only" const [modalMode, setModalMode] = useState<"full" | "select-only">("full"); // modalKey: "neniNaPozici" | "doskladnit" | "zmenitPozici" const [modalKey, setModalKey] = useState(null); // Helper to get or initialize the button state for an item const getItemState = (itemId: number) => buttonStates[itemId] || { neniNaPozici: { value: false, details: [] }, doskladnit: { value: false, details: [] }, zmenitPozici: { value: false, details: [] }, }; // Generic update function for button state for a given item and key. const updateButtonState = (itemId: number, key: string, details: any[] = []) => { setButtonStates((prev) => { const currentState = getItemState(itemId); const currentButtonState = currentState[key] || { value: false, details: [] }; const toggledValue = !currentButtonState.value; return { ...prev, [itemId]: { ...currentState, [key]: { value: toggledValue, details: toggledValue ? details : [] }, }, }; }); }; // Open the modal with appropriate mode and key const handleOpenModal = ( item: any, key: "neniNaPozici" | "doskladnit" | "zmenitPozici", mode: "full" | "select-only" ) => { setCurrentItem(item); setModalKey(key); setModalMode(mode); setModalPositionOpen(true); // Optionally notify parent modal // parentModalHandle(); }; // Confirm handler for the modal const handleConfirmModal = () => { if (currentItem && modalKey) { if (modalMode === "full") { updateButtonState(currentItem.id, modalKey, [ { from: fromPosition, to: toPosition }, ]); } else { // select-only: only pass the "from" detail updateButtonState(currentItem.id, modalKey, [{ position: fromPosition }]); } } // Reset modal state setModalPositionOpen(false); setCurrentItem(null); setFromPosition(""); setToPosition(""); setModalMode("full"); setModalKey(null); // Optionally notify parent modal // parentModalHandle(); }; // Toggle the open state of an accordion item. const handleToggle = (id: number) => { if (openAccordion === id) { setOpenAccordion(null); } else { setOpenAccordion(id); } }; // Mark an item as completed. const handleComplete = (id: number) => { setCompletedItems((prev) => ({ ...prev, [id]: true })); if (openAccordion === id) { setOpenAccordion(null); } }; // Touch event handlers for long press. const onTouchStartHandler = (id: number) => { holdTimerRef.current = window.setTimeout(() => { handleComplete(id); }, 1000); }; const onTouchEndHandler = () => { if (holdTimerRef.current) { clearTimeout(holdTimerRef.current); holdTimerRef.current = null; } }; return (
{selectedShipment.items.map((item) => { const itemState = getItemState(item.id); const isOpen = openAccordion === item.id; const isCompleted = completedItems[item.id]; // If an item is not open, it appears "greyed out" const headerOpacity = isOpen ? "opacity-100" : "opacity-50"; const buttonFunctionality = isOpen ? "" : "pointer-events-none"; return (
handleToggle(item.id)} className="peer" />
handleComplete(item.id)} onTouchStart={() => onTouchStartHandler(item.id)} onTouchEnd={onTouchEndHandler} >

{item.quantity}× {item.name}

{item.item_note}

{item.model_number} {(item.price / item.quantity).toFixed(2)} {selectedShipment.currency}
{isCompleted && ( )}
{Object.entries(item.stockData).flatMap(([stockName, stockArray]) => stockArray.map((stock, idx) => (

{stockName}

Location: {stock.location}

Count: {stock.count} ks

)) )}
); })} {/* Modal for changing position */}

Change Position

{modalMode === "full" && (
setToPosition(e.target.value)} placeholder="Enter new position" />
)}
); }