import { Box, Typography, Button, Paper, TextField, FormControlLabel, Checkbox, Select, MenuItem, FormControl, InputLabel, Divider, CircularProgress } from "@mui/material"; import { useState, useEffect } from "react"; import type { Kennzahl } from "../types/kpi"; import { typeDisplayMapping } from "../types/kpi"; interface KPIFormProps { mode: 'add' | 'edit'; initialData?: Kennzahl | null; onSave: (data: Partial) => Promise; onCancel: () => void; loading?: boolean; } const emptyKPI: Partial = { name: '', description: '', mandatory: false, type: 'string', translation: '', example: '', active: true }; export function KPIForm({ mode, initialData, onSave, onCancel, loading = false }: KPIFormProps) { const [formData, setFormData] = useState>(emptyKPI); const [isSaving, setIsSaving] = useState(false); useEffect(() => { if (mode === 'edit' && initialData) { setFormData(initialData); } else { setFormData(emptyKPI); } }, [mode, initialData]); const handleSave = async () => { if (!formData.name?.trim()) { alert('Name ist erforderlich'); return; } setIsSaving(true); try { await onSave(formData); } catch (error) { console.error('Error saving KPI:', error); } finally { setIsSaving(false); } }; const handleCancel = () => { onCancel(); }; const updateField = (field: keyof Kennzahl, value: any) => { setFormData(prev => ({ ...prev, [field]: value })); }; if (loading) { return ( {mode === 'edit' ? 'Lade KPI Details...' : 'Laden...'} ); } return ( Kennzahl updateField('name', e.target.value)} sx={{ mb: 2 }} required error={!formData.name?.trim()} helperText={!formData.name?.trim() ? 'Name ist erforderlich' : ''} /> Beschreibung updateField('description', e.target.value)} helperText="Beschreibung der Kennzahl" /> updateField('mandatory', e.target.checked)} sx={{ color: '#383838' }} /> } label="Erforderlich" /> Die Kennzahl erlaubt keine leeren Werte Format: {typeDisplayMapping[formData.type as keyof typeof typeDisplayMapping] || formData.type} Typ Synonyme & Übersetzungen updateField('translation', e.target.value)} helperText="z.B. Englische Übersetzung der Kennzahl" /> Beispiele von Kennzahl updateField('example', e.target.value)} helperText="Beispielwerte für diese Kennzahl" /> {mode === 'add' && ( <> updateField('active', e.target.checked)} sx={{ color: '#383838' }} /> } label="Aktiv" /> Die Kennzahl ist aktiv und wird angezeigt )} ); }