'use client'; import { useState, useEffect } from 'react'; import { useRouter } from 'next/navigation'; import DashboardSidebar from '@/components/DashboardSidebar'; import { useSession } from 'next-auth/react'; import { FiPackage, FiArrowLeft, FiSave, FiEdit, FiX, FiTrash2, FiMapPin, FiCalendar } from 'react-icons/fi'; import Link from 'next/link'; import { formatDate } from '@/lib/date-utils'; export default function EquipmentDetailPage({ params }: { params: { id: string } }) { const router = useRouter(); const { data: session } = useSession(); const [equipment, setEquipment] = useState(null); const [locations, setLocations] = useState([]); const [projects, setProjects] = useState([]); const [loading, setLoading] = useState(true); const [editing, setEditing] = useState(false); const [saving, setSaving] = useState(false); const [formData, setFormData] = useState(null); useEffect(() => { fetchEquipment(); fetchLocationsAndProjects(); }, [params.id]); const fetchEquipment = async () => { try { const res = await fetch(`/api/inventory/${params.id}`); if (res.ok) { const data = await res.json(); setEquipment(data.equipment); setFormData(data.equipment); } } catch (err) { console.error('Error fetching equipment:', err); } finally { setLoading(false); } }; const fetchLocationsAndProjects = async () => { try { const [locRes, projRes] = await Promise.all([ fetch('/api/locations'), fetch('/api/projects'), ]); if (locRes.ok) { const locData = await locRes.json(); setLocations(locData.locations || []); } if (projRes.ok) { const projData = await projRes.json(); setProjects(projData.projects || []); } } catch (err) { console.error('Error fetching data:', err); } }; const handleSave = async () => { setSaving(true); try { const res = await fetch(`/api/inventory/${params.id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ ...formData, purchaseDate: formData.purchaseDate || null, purchasePrice: formData.purchasePrice ? parseFloat(formData.purchasePrice) : null, minStockLevel: formData.minStockLevel ? parseInt(formData.minStockLevel) : null, currentStock: formData.currentStock ? parseInt(formData.currentStock) : null, locationId: formData.locationId || null, projectId: formData.projectId || null, }), }); if (res.ok) { alert('Gespeichert!'); setEditing(false); fetchEquipment(); } else { alert('Fehler beim Speichern'); } } catch (error) { alert('Fehler beim Speichern'); } finally { setSaving(false); } }; const handleDelete = async () => { if (!confirm('Equipment wirklich löschen?')) return; try { const res = await fetch(`/api/inventory/${params.id}`, { method: 'DELETE', }); if (res.ok) { alert('Equipment gelöscht!'); router.push('/dashboard/inventory'); } else { alert('Fehler beim Löschen'); } } catch (error) { alert('Fehler beim Löschen'); } }; const getStatusColor = (status: string) => { switch (status) { case 'AVAILABLE': return 'bg-green-500/20 text-green-400 border-green-500/50'; case 'IN_USE': return 'bg-blue-500/20 text-blue-400 border-blue-500/50'; case 'MAINTENANCE': return 'bg-yellow-500/20 text-yellow-400 border-yellow-500/50'; case 'DAMAGED': return 'bg-red-500/20 text-red-400 border-red-500/50'; case 'RESERVED': return 'bg-purple-500/20 text-purple-400 border-purple-500/50'; default: return 'bg-gray-500/20 text-gray-400 border-gray-500/50'; } }; const getStatusLabel = (status: string) => { const labels: any = { AVAILABLE: 'Verfügbar', IN_USE: 'Im Einsatz', MAINTENANCE: 'Wartung', DAMAGED: 'Beschädigt', RESERVED: 'Reserviert', }; return labels[status] || status; }; const getTypeLabel = (type: string) => { const labels: any = { PRINTER: 'Drucker', CARPET: 'Roter Teppich', VIP_BARRIER: 'VIP Absperrband', ACCESSORIES_KIT: 'Accessoires-Koffer', PRINTER_PAPER: 'Druckerpapier', TRIPOD: 'Stativ', OTHER: 'Sonstiges', }; return labels[type] || type; }; if (!session) { return null; } if (loading) { return (
Lade...
); } if (!equipment) { return (
Equipment nicht gefunden
); } return (
Zurück zum Inventar

{equipment.name}

{getTypeLabel(equipment.type)}

{!editing ? ( <> ) : ( <> )}

Status

{getStatusLabel(equipment.status)}
{editing && (
)}

Details

{editing ? ( setFormData({ ...formData, name: e.target.value })} className="w-full px-3 py-2 bg-gray-700 border border-gray-600 text-white rounded-lg focus:ring-2 focus:ring-red-500" /> ) : (

{equipment.name}

)}
{editing ? ( ) : (

{getTypeLabel(equipment.type)}

)}
{equipment.brand && (
{editing ? ( setFormData({ ...formData, brand: e.target.value })} className="w-full px-3 py-2 bg-gray-700 border border-gray-600 text-white rounded-lg focus:ring-2 focus:ring-red-500" /> ) : (

{equipment.brand}

)}
)} {equipment.model && (
{editing ? ( setFormData({ ...formData, model: e.target.value })} className="w-full px-3 py-2 bg-gray-700 border border-gray-600 text-white rounded-lg focus:ring-2 focus:ring-red-500" /> ) : (

{equipment.model}

)}
)} {equipment.serialNumber && (
{editing ? ( setFormData({ ...formData, serialNumber: e.target.value })} className="w-full px-3 py-2 bg-gray-700 border border-gray-600 text-white rounded-lg focus:ring-2 focus:ring-red-500" /> ) : (

{equipment.serialNumber}

)}
)}
{editing ? ( setFormData({ ...formData, quantity: parseInt(e.target.value) })} min="1" className="w-full px-3 py-2 bg-gray-700 border border-gray-600 text-white rounded-lg focus:ring-2 focus:ring-red-500" /> ) : (

{equipment.quantity}

)}
{(equipment.currentStock !== null || equipment.minStockLevel !== null || editing) && (

Bestandsverwaltung

{editing ? ( setFormData({ ...formData, currentStock: e.target.value })} className="w-full px-3 py-2 bg-gray-700 border border-gray-600 text-white rounded-lg focus:ring-2 focus:ring-red-500" /> ) : (

{equipment.currentStock || '-'}

)}
{editing ? ( setFormData({ ...formData, minStockLevel: e.target.value })} className="w-full px-3 py-2 bg-gray-700 border border-gray-600 text-white rounded-lg focus:ring-2 focus:ring-red-500" /> ) : (

{equipment.minStockLevel || '-'}

)}
{equipment.currentStock !== null && equipment.minStockLevel !== null && equipment.currentStock < equipment.minStockLevel && (
⚠️ Niedriger Bestand! Nachbestellen erforderlich.
)}
)} {equipment.notes && (

Notizen

{editing ? (