import React, { useState, useEffect } from 'react'; import { Icons } from './IconSet'; interface ShopItem { id: string; name: string; description: string; price: number; currency: string; stock: number; type: 'item' | 'service'; materialsRequired?: string; } interface ShopManagementModalProps { isOpen: boolean; onClose: () => void; projectId: string; onUpdate: () => void; } const ShopManagementModal: React.FC = ({ isOpen, onClose, projectId, onUpdate }) => { const [items, setItems] = useState([]); const [loading, setLoading] = useState(false); const [showAddForm, setShowAddForm] = useState(false); const [editingItem, setEditingItem] = useState(null); const [error, setError] = useState(null); // Form state for add/edit const [formData, setFormData] = useState({ name: '', description: '', price: '', currency: 'Gold', stock: '', type: 'item' as 'item' | 'service', materialsRequired: '' }); useEffect(() => { if (isOpen && projectId) { loadShopItems(); } }, [isOpen, projectId]); const loadShopItems = async () => { try { setLoading(true); const response = await fetch(`https://vollidioten.ceraticsoft.de/api/projects/${projectId}/shop`); if (response.ok) { const data = await response.json(); setItems(data); } else { setError('Fehler beim Laden der Shop-Artikel'); } } catch (err) { console.error('Error loading shop items:', err); setError('Netzwerkfehler'); } finally { setLoading(false); } }; const resetForm = () => { setFormData({ name: '', description: '', price: '', currency: 'Gold', stock: '', type: 'item', materialsRequired: '' }); setEditingItem(null); setShowAddForm(false); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!formData.name.trim() || !formData.description.trim() || !formData.price) { setError('Name, Beschreibung und Preis sind erforderlich'); return; } try { setLoading(true); setError(null); const itemData = { name: formData.name.trim(), description: formData.description.trim(), price: parseFloat(formData.price), currency: formData.currency, stock: parseInt(formData.stock) || 0, type: formData.type, materialsRequired: formData.materialsRequired.trim() || undefined }; let response; if (editingItem) { // Update existing item response = await fetch(`https://vollidioten.ceraticsoft.de/api/projects/${projectId}/shop/${editingItem.id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify(itemData) }); } else { // Add new item response = await fetch(`https://vollidioten.ceraticsoft.de/api/projects/${projectId}/shop`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify(itemData) }); } if (response.ok) { await loadShopItems(); resetForm(); onUpdate(); } else { const errorData = await response.json(); setError(errorData.error || 'Fehler beim Speichern'); } } catch (err) { console.error('Error saving shop item:', err); setError('Netzwerkfehler'); } finally { setLoading(false); } }; const handleEdit = (item: ShopItem) => { setFormData({ name: item.name, description: item.description, price: item.price.toString(), currency: item.currency, stock: item.stock.toString(), type: item.type, materialsRequired: item.materialsRequired || '' }); setEditingItem(item); setShowAddForm(true); }; const handleDelete = async (itemId: string) => { if (!confirm('Artikel wirklich löschen?')) return; try { setLoading(true); const response = await fetch(`https://vollidioten.ceraticsoft.de/api/projects/${projectId}/shop/${itemId}`, { method: 'DELETE', credentials: 'include' }); if (response.ok) { await loadShopItems(); onUpdate(); } else { setError('Fehler beim Löschen'); } } catch (err) { console.error('Error deleting shop item:', err); setError('Netzwerkfehler'); } finally { setLoading(false); } }; if (!isOpen) return null; return (

Shop verwalten

{/* Add/Edit Form */} {showAddForm && (

{editingItem ? 'Artikel bearbeiten' : 'Neuen Artikel hinzufügen'}

setFormData({...formData, name: e.target.value})} className="w-full bg-[#0b0b0d] border border-border rounded p-2 text-sm" required />