'use client'; import { useState } from 'react'; import { OrderItem } from '@/lib/types'; import { cn, formatCurrency } from '@/lib/utils'; import { getItemDisplayDescription, getItemDisplayQty } from '@/lib/pocketbase'; import { X, Minus, Plus, Check } from 'lucide-react'; interface CollectModalProps { item: OrderItem | null; isOpen: boolean; onClose: () => void; onConfirm: (item: OrderItem, receivedQty: number, notes: string) => void; } export default function CollectModal({ item, isOpen, onClose, onConfirm, }: CollectModalProps) { const [receivedQty, setReceivedQty] = useState(''); const [notes, setNotes] = useState(''); const [error, setError] = useState(''); if (!isOpen || !item) return null; const expectedQty = getItemDisplayQty(item); const parsedQty = parseFloat(receivedQty) || 0; const isPartial = parsedQty > 0 && parsedQty < expectedQty; const isOverQty = parsedQty > expectedQty; const displayDescription = getItemDisplayDescription(item); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); setError(''); const qty = parseFloat(receivedQty); if (isNaN(qty) || qty <= 0) { setError('Please enter a valid quantity'); return; } onConfirm(item, qty, notes); setReceivedQty(''); setNotes(''); setError(''); }; const adjustQty = (delta: number) => { const current = parseFloat(receivedQty) || 0; const newQty = Math.max(0, current + delta); setReceivedQty(newQty.toString()); setError(''); }; return (
{/* Drag indicator */}
{/* Header */}

Collect Item

{/* Scrollable body — overflow lives on a child so the form can still submit via Enter / native submit semantics. */}
{/* Item info */}

{displayDescription}

{item.sku && (

#{item.sku}

)}
{item.expand?.supplier?.suppliername || 'Unknown'} Expected: {expectedQty} units
{item.selling_price && (

Unit price: {formatCurrency(item.selling_price)}

)}
{/* Quantity */}
{ setReceivedQty(e.target.value); setError(''); }} className="w-20 text-center text-2xl font-bold py-3 border-2 border-[rgba(38,37,30,0.1)] rounded-xl focus:ring-2 focus:ring-[#f54e00]/30 focus:border-[#f54e00] transition-all bg-[#f2f1ed] text-[#26251e]" placeholder="0" autoFocus /> {expectedQty > 0 && ( )}
{isPartial && (

Partial collection: {parsedQty} of {expectedQty} units

)} {isOverQty && (

Over collection: {parsedQty} exceeds expected {expectedQty}

)} {error &&

{error}

}
{/* Notes */}