import { useState, useMemo } from 'react'; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription } from '@/Components/ui/dialog'; import { Input } from '@/Components/ui/input'; import { Badge } from '@/Components/ui/badge'; import { Search, Wrench, Check } from 'lucide-react'; interface EquipmentSpecification { id: number; ulid: string; name: string; } interface Equipment { id: number; ulid: string; name: string; owner_name: string | null; hourly_rate: string; specifications: EquipmentSpecification[]; } interface Props { open: boolean; onOpenChange: (open: boolean) => void; equipments: Equipment[]; onSelectEquipment: (equipment: Equipment) => void; selectedEquipmentUlid?: string; } export default function EquipmentLookupModal({ open, onOpenChange, equipments, onSelectEquipment, selectedEquipmentUlid }: Props) { const [search, setSearch] = useState(''); const [ownerFilter, setOwnerFilter] = useState('all'); const formatCurrency = (v: string | number) => { return new Intl.NumberFormat('en-PH', { style: 'currency', currency: 'PHP' }).format(Number(v)); }; // Dynamically discover all unique owner names in the equipment list for filtering const uniqueOwners = useMemo(() => { const owners = new Set(); equipments.forEach(e => { if (e.owner_name) { owners.add(e.owner_name); } }); return Array.from(owners).sort(); }, [equipments]); const filteredEquipments = useMemo(() => { return equipments.filter(eq => { // Owner filter if (ownerFilter !== 'all') { if (ownerFilter === 'unspecified') { if (eq.owner_name !== null && eq.owner_name !== '') return false; } else { if (eq.owner_name !== ownerFilter) return false; } } // Search filter if (!search) return true; const query = search.toLowerCase(); const matchesName = eq.name.toLowerCase().includes(query); const matchesOwner = eq.owner_name ? eq.owner_name.toLowerCase().includes(query) : false; const matchesSpecs = eq.specifications && eq.specifications.some(spec => spec.name.toLowerCase().includes(query) ); return matchesName || matchesOwner || matchesSpecs; }); }, [equipments, search, ownerFilter]); return ( Select Equipment / Tool Search and select a piece of machinery or equipment to allocate to the project task.
{/* Owner Filter Badges */}
{uniqueOwners.map(owner => ( ))}
{/* Search Input */}
setSearch(e.target.value)} />
{filteredEquipments.length === 0 ? (
No equipment profiles found matching "{search}".
) : (
{filteredEquipments.map(eq => { const isSelected = selectedEquipmentUlid === eq.ulid; return ( onSelectEquipment(eq)} > ); })}
Equipment / Tool Name Owner Specifications Hourly Rate
{eq.name} {isSelected && ( Selected )}
{eq.owner_name ? ( {eq.owner_name} ) : ( Unspecified )}
{eq.specifications && eq.specifications.length > 0 ? ( eq.specifications.map(spec => ( {spec.name} )) ) : ( No specs listed )}
{formatCurrency(eq.hourly_rate)}/hr
)}
); }