'use client'; import { useState, useEffect } from 'react'; import { useRouter } from 'next/navigation'; import { ArrowLeft, ChevronRight, Loader2, Camera, Upload, X } from 'lucide-react'; import { useItemCreate } from '@/hooks/useItemCreate'; import ItemPhotoUpload from '@/components/ItemPhotoUpload'; import ManualCropUI from '@/components/ManualCropUI'; import { toast } from 'react-hot-toast'; interface Category { id: number; name: string; } interface ItemType { id: number; name: string; } export default function CreateItemPage() { const router = useRouter(); const { step, formData, setFormData, uploadedPhoto, cropBounds, setCropBounds, isLoading, error, photoError, goToStep, nextStep, prevStep, uploadPhoto, submitItem, reset, } = useItemCreate(); const [categories, setCategories] = useState([]); const [itemTypes, setItemTypes] = useState([]); const [currentUser, setCurrentUser] = useState<{ id: number; username: string } | null>(null); const [useFullPhoto, setUseFullPhoto] = useState(true); const [selectedFile, setSelectedFile] = useState(null); // Load categories and current user on mount useEffect(() => { const loadInitialData = async () => { try { // Get categories from localStorage or API const savedCategories = localStorage.getItem('categories'); const savedUser = localStorage.getItem('currentUser'); if (savedCategories) { setCategories(JSON.parse(savedCategories)); } if (savedUser) { setCurrentUser(JSON.parse(savedUser)); } } catch (err) { // Silently handle initial data load failure - use default empty state } }; loadInitialData(); }, []); const handlePhotoUpload = async (file: File) => { setSelectedFile(file); try { await uploadPhoto(file); toast.success('Photo uploaded successfully'); nextStep(); // Move to preview after upload } catch (err: any) { toast.error(err.message || 'Failed to upload photo'); } }; const handleDetailsSubmit = async () => { try { if (!currentUser) { toast.error('User not authenticated'); return; } await submitItem(currentUser.id); nextStep(); // Move to photo upload after item creation } catch (err: any) { toast.error(err.message || 'Failed to create item'); } }; const handleConfirm = () => { toast.success('Item created successfully'); reset(); router.push('/inventory'); }; const stepIndicator = (stepName: string, stepNum: number) => { const stepOrder: Record = { details: 1, photo: 2, preview: 3, confirm: 4, }; const currentNum = stepOrder[step]; const isActive = currentNum === stepNum; const isCompleted = currentNum > stepNum; return (
{stepNum}
{stepName}
); }; return (
{/* Header */}

Create New Item

Step {step === 'details' ? 1 : step === 'photo' ? 2 : step === 'preview' ? 3 : 4} of 4

{/* Step Indicator */}
{stepIndicator('Details', 1)} {stepIndicator('Photo', 2)} {stepIndicator('Preview', 3)} {stepIndicator('Confirm', 4)}
{/* Content Area */}
{/* Details Step */} {step === 'details' && (

Item Details

{/* Name */}
setFormData({ name: e.target.value })} placeholder="Enter item name" className="w-full px-3 py-2 bg-surface-bright border border-border rounded-none text-white placeholder-slate-500 focus:border-primary focus:outline-none" disabled={isLoading} />
{/* Category */}
{/* Item Type */}
setFormData({ item_type: e.target.value })} placeholder="e.g., Component, Part, Equipment" className="w-full px-3 py-2 bg-surface-bright border border-border rounded-none text-white placeholder-slate-500 focus:border-primary focus:outline-none" disabled={isLoading} />
{/* Quantity */}
setFormData({ quantity: parseInt(e.target.value) || 1 })} className="w-full px-3 py-2 bg-surface-bright border border-border rounded-none text-white focus:border-primary focus:outline-none" disabled={isLoading} />
{/* Part Number (Optional) */}
setFormData({ part_number: e.target.value })} placeholder="e.g., PN-12345" className="w-full px-3 py-2 bg-surface-bright border border-border rounded-none text-white placeholder-slate-500 focus:border-primary focus:outline-none" disabled={isLoading} />
{/* Barcode (Optional) */}
setFormData({ barcode: e.target.value })} placeholder="e.g., 1234567890" className="w-full px-3 py-2 bg-surface-bright border border-border rounded-none text-white placeholder-slate-500 focus:border-primary focus:outline-none" disabled={isLoading} />
{/* Error Message */} {error && (
{error}
)} {/* Action Buttons */}
)} {/* Photo Upload Step */} {step === 'photo' && (

Upload Item Photo

Take a photo or upload an image. You can crop it manually on the next step.

{ setSelectedFile(null); }} onError={(error) => { toast.error(error); }} />
{photoError && (
{photoError}
)} {/* Action Buttons */}
)} {/* Preview Step (Crop) */} {step === 'preview' && uploadedPhoto && (

Crop & Preview

Adjust the crop area or use the full photo. Manual crop handles are visible.

{/* Use Full Photo Toggle */}
{ setUseFullPhoto(e.target.checked); if (e.target.checked) { setCropBounds(null); } }} className="toggle-pill" />
{/* Action Buttons */}
)} {/* Confirm Step */} {step === 'confirm' && (

Confirm & Save

{/* Item Summary */}
Name: {formData.name}
Category: {formData.category}
Type: {formData.item_type}
Quantity: {formData.quantity}
{uploadedPhoto && (
Photo: Uploaded
)}
{/* Photo Thumbnail */} {uploadedPhoto && (

Photo Preview

Item
)} {/* Action Buttons */}
)}
); }