'use client'; import React, { useState, useRef } from 'react'; import { Item } from '@/lib/db'; import { inventoryApi } from '@/lib/api'; import ItemPhotoUpload from '@/components/ItemPhotoUpload'; import { X, Camera, Trash2 } 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; } export default function ItemDetailModal({ item, onClose, onPhotoUpdated, onItemRefresh, }: ItemDetailModalProps) { const [showPhotoUpload, setShowPhotoUpload] = useState(false); const [currentPhoto, setCurrentPhoto] = useState<{ thumbnail_url: string; full_url: string } | null>( 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}

{/* Content */}
{/* Item Details */}

Category

{item.category}

Type

{item.type || 'N/A'}

Quantity

{item.quantity}

Part Number

{item.part_number || 'N/A'}

{item.barcode && (

Barcode

{item.barcode}

)}
{/* Photo Section */}

Photo

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

No photo uploaded

)} ) : (

Upload New Photo

{item.id && ( )}
)}
); }