"use client"; import { useState } from "react"; import { Edit, Plus, Trash2 } from "lucide-react"; import ConfirmDialog from "@/components/common/ConfirmDialog"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { useToast } from "@/components/common/ToastProvider"; import { api } from "@/lib/api"; import type { InventoryCategory, InventoryLocation, InventorySupplier } from "@/types/inventory"; import { getErrorMessage } from "./inventory-utils"; type MasterKind = "categories" | "locations" | "suppliers"; type MasterEntry = InventoryCategory | InventoryLocation | InventorySupplier; type Props = { title: string; kind: MasterKind; entries: MasterEntry[]; canManage: boolean; onChanged: () => void; }; export default function MasterDataSection({ title, kind, entries, canManage, onChanged }: Props) { const { showToast } = useToast(); const [editing, setEditing] = useState(null); const [deleteEntry, setDeleteEntry] = useState(null); const [name, setName] = useState(""); const [description, setDescription] = useState(""); const [pending, setPending] = useState(false); const [error, setError] = useState(""); function startCreate() { setEditing({ id: 0, name: "", description: null, created_at: "", updated_at: "" } as MasterEntry); setName(""); setDescription(""); setError(""); } function startEdit(entry: MasterEntry) { setEditing(entry); setName(entry.name); setDescription("description" in entry && entry.description ? entry.description : ""); setError(""); } async function save() { if (!editing) { return; } if (!name.trim()) { setError("Bitte einen Namen angeben."); return; } setPending(true); setError(""); const payload = kind === "suppliers" ? { name: name.trim(), notes: description.trim() || null } : { name: name.trim(), description: description.trim() || null }; try { if (editing.id) { await api.put(`/inventory/${kind}/${editing.id}`, payload); } else { await api.post(`/inventory/${kind}`, payload); } showToast({ type: "success", title: `${title} gespeichert` }); setEditing(null); onChanged(); } catch (err) { const message = getErrorMessage(err); setError(message); showToast({ type: "error", title: `${title} konnte nicht gespeichert werden`, description: message }); } finally { setPending(false); } } async function confirmDelete() { if (!deleteEntry) { return; } setPending(true); try { await api.delete(`/inventory/${kind}/${deleteEntry.id}`); showToast({ type: "success", title: `${title} gelöscht`, description: deleteEntry.name }); setDeleteEntry(null); onChanged(); } catch (err) { const message = getErrorMessage(err); showToast({ type: "error", title: `${title} konnte nicht gelöscht werden`, description: message }); } finally { setPending(false); } } return (

{title}

{canManage && }
{entries.length === 0 ? (

Noch keine Einträge vorhanden.

) : entries.map((entry) => (

{entry.name}

{"description" in entry && entry.description &&

{entry.description}

} {"notes" in entry && entry.notes &&

{entry.notes}

}
{canManage && (
)}
))}
{editing && (
setName(event.target.value)} />