'use client'; import React, { useState, useRef } from 'react'; import { Item } from '@/lib/db'; import { inventoryApi, buildPhotoUrl } from '@/lib/api'; import ItemPhotoUpload from '@/components/ItemPhotoUpload'; import { DebugRotationPanel } from '@/components/DebugRotationPanel'; import { X, Camera, Trash2, Wrench } from 'lucide-react'; import { toast } from 'react-hot-toast'; interface ItemDetailModalProps { item: Item; onClose: () => void; onPhotoUpdated?: (photo: { thumbnail_url: string; full_url: string; uploaded_at: string }) => void; onItemRefresh?: () => void; backendUrl?: string; } export default function ItemDetailModal({ item, onClose, onPhotoUpdated, onItemRefresh, backendUrl = '', }: ItemDetailModalProps) { const [showPhotoUpload, setShowPhotoUpload] = useState(false); const [showDebugPanel, setShowDebugPanel] = useState(false); const [currentPhoto, setCurrentPhoto] = useState<{ thumbnail_url: string; full_url: string } | null>( item.photo_path && item.photo_thumbnail_path ? { thumbnail_url: item.photo_thumbnail_path, full_url: item.photo_path } : item.image_url ? { thumbnail_url: item.image_url, full_url: item.image_url } : null ); const [isDeleting, setIsDeleting] = useState(false); const photoUploadRef = useRef(null); const handlePhotoUploadSuccess = (photo: { thumbnail_url: string; full_url: string; uploaded_at: string }) => { setCurrentPhoto({ thumbnail_url: photo.thumbnail_url, full_url: photo.full_url }); setShowPhotoUpload(false); onPhotoUpdated?.(photo); onItemRefresh?.(); }; const handlePhotoUploadError = (errorMessage: string) => { toast.error(errorMessage); }; const handleDeletePhoto = async () => { if (!item.id) return; if (!window.confirm('Delete this photo? This cannot be undone.')) { return; } setIsDeleting(true); try { await inventoryApi.deleteItemPhoto(item.id); setCurrentPhoto(null); toast.success('Photo deleted successfully'); onItemRefresh?.(); } catch (error: any) { const errorMsg = error?.message || 'Failed to delete photo'; toast.error(errorMsg); } finally { setIsDeleting(false); } }; return (
{/* Header */}

{item.name}

Asset Technical Protocol

{currentPhoto && ( )}
{/* Content */}
{/* Details Grid */}
{[ { label: 'Category', value: item.category }, { label: 'Item Type', value: item.type || 'N/A' }, { label: 'Available Stock', value: item.quantity }, { label: 'Part Number', value: item.part_number || 'N/A' }, { label: 'Identifier', value: item.barcode, full: true, mono: true } ].map((d, i) => (

{d.label}

{d.value}

))}
{/* Photo Section */}

Visual Signature

{!showPhotoUpload ? ( <> {currentPhoto ? (
{item.name}
) : (

No recorded visual signatures

)} ) : (

Signature Acquisition Protocol

{item.id && ( )}
)}
{/* Debug Rotation Panel */} {showDebugPanel && currentPhoto && ( { try { const parsed = JSON.parse(item.labels_data); const origPath = parsed.image_processing?.original_photo_path; return origPath ? buildPhotoUrl(backendUrl, origPath) : undefined; } catch { return undefined; } })() : undefined } originalCropBounds={item.labels_data ? JSON.parse(item.labels_data).image_processing?.crop_bounds : undefined} originalRotation={item.labels_data ? JSON.parse(item.labels_data).image_processing?.rotation_degrees : undefined} imageProcessing={item.labels_data ? JSON.parse(item.labels_data).image_processing : undefined} onClose={() => setShowDebugPanel(false)} /> )}
); }