import React, { useState } from 'react'
import { Target, Tags, Dumbbell } from 'lucide-react'
import { api } from '../../utils/api'
function CatalogsTab({ targetGroups, skillCategories, trainingCharacters, loading, error, onUpdate }) {
if (loading) {
return (
)
}
return (
)
}
function CatalogSection({ title, Icon, items, onUpdate, createFn, updateFn, deleteFn, fields }) {
const [creating, setCreating] = useState(false)
const [editing, setEditing] = useState(null)
const [form, setForm] = useState({})
function startCreate() {
const emptyForm = {}
fields.forEach((f) => { emptyForm[f.key] = '' })
setForm(emptyForm)
setCreating(true)
}
function startEdit(item) {
const editForm = {}
fields.forEach((f) => { editForm[f.key] = item[f.key] || '' })
setEditing(item.id)
setForm(editForm)
}
async function handleCreate() {
const required = fields.filter((f) => f.required)
for (const field of required) {
if (!form[field.key]) {
alert(`${field.label} ist erforderlich`)
return
}
}
try {
await createFn(form)
setCreating(false)
setForm({})
onUpdate()
} catch (e) {
alert('Fehler: ' + e.message)
}
}
async function handleUpdate(id) {
try {
await updateFn(id, form)
setEditing(null)
setForm({})
onUpdate()
} catch (e) {
alert('Fehler: ' + e.message)
}
}
async function handleDelete(id, name) {
if (!confirm(`"${name}" wirklich löschen?`)) return
try {
await deleteFn(id)
onUpdate()
} catch (e) {
alert('Fehler: ' + e.message)
}
}
return (
{Icon ? (
) : null}
{title}
{creating && (
Neu erstellen
{fields.map((field) => (
{field.type === 'textarea' ? (
))}
)}
{items.map((item) => (
{editing === item.id ? (
{fields.map((field) => (
{field.type === 'textarea' ? (
))}
) : (
{item.name}
{item.min_age != null && item.max_age != null && (
Alter: {item.min_age}-{item.max_age}
)}
{item.description ? (
{item.description}
) : null}
)}
))}
{items.length === 0 && !creating && (
Noch keine Einträge vorhanden
)}
)
}
export default CatalogsTab