using System.Collections.ObjectModel; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using LehrerApp.Core.Models; using LehrerApp.Desktop.ViewModels.Students; namespace LehrerApp.Desktop.ViewModels.ClassTeacher; // ── Anzeige-Helfer ──────────────────────────────────────────────────────── public static class VorgangTagDisplay { // Vom Nutzer selbst genannte Schlagwörter — eigene Labels bleiben trotzdem frei möglich // (AutoCompleteBox, gleiches Muster wie DocumentationTagDisplay.Suggestions). public static string[] Suggestions { get; } = ["Absentismus", "Hausaufgaben", "Verspätungen", "Konflikte", "Mitarbeit", "Elternkontakt", "Eskalation"]; } public static class VorgangStatusDisplay { public static string Label(VorgangStatus status) => status == VorgangStatus.Closed ? "Geschlossen" : "Offen"; } /// Ergebnis des "→ Vorgang"-Auswahldialogs beim Anheften einer Klassenbuchzeile: entweder ein /// bestehender, offener Vorgang oder der Titel für einen neu anzulegenden. public sealed record PinToVorgangChoice(Guid? ExistingVorgangId, string? NewVorgangTitle); /// Eingefrorene WebUntis-Klassenbuchzeile mit deutscher Anzeige-Aufbereitung — das Core-Modell /// bleibt bewusst framework-frei ohne Formatierungslogik. public sealed record VorgangClassRegisterEntryRow(VorgangClassRegisterEntry Model) { public string DateLabel => Model.Date.ToString("dd.MM.yyyy"); public string StudentName => Model.StudentName; public string SummaryLabel => string.Join(" · ", new[] { Model.Subject, Model.CategoryName, Model.Text } .Where(v => !string.IsNullOrWhiteSpace(v))); } // ── Mehrfachauswahl Schüler*innen (Anlegen/Bearbeiten-Dialog) ──────────────── public partial class VorgangStudentOption(Guid studentId, string name) : ObservableObject { public Guid StudentId { get; } = studentId; public string Name { get; } = name; [ObservableProperty] private bool _isSelected; } // ── Listen-Eintrag mit aufklappbarer Detailansicht ─────────────────────────── /// /// Ein Vorgang in der Liste des "Vorgänge"-Tabs. Verknüpfte Dokumentation/Klassenbucheinträge /// werden von beim Laden befüllt; Link/Unlink-Aktionen /// laufen über die hier übergebenen Callbacks zurück ins ViewModel (statt eigenem Repository- /// Zugriff hier), damit dieser Wrapper ein reiner Anzeige-Baustein bleibt — analog /// . /// public partial class VorgangItem : ObservableObject { private readonly Func _onLink; private readonly Func _onUnlink; private readonly Func _onRemoveClassRegisterEntry; private readonly Func _onEditDocumentation; public Vorgang Model { get; } public string StudentNames { get; } public List TagChips { get; } public string StatusLabel => VorgangStatusDisplay.Label(Model.Status); public bool IsOpen => Model.Status == VorgangStatus.Open; public string CreatedLabel => $"Angelegt {Model.CreatedAt:dd.MM.yyyy}"; public ObservableCollection LinkedDocumentation { get; } = []; public ObservableCollection AvailableDocumentation { get; } = []; public ObservableCollection ClassRegisterRows { get; } = []; public bool HasLinkedDocumentation => LinkedDocumentation.Count > 0; public bool HasAvailableDocumentation => AvailableDocumentation.Count > 0; public bool HasClassRegisterRows => ClassRegisterRows.Count > 0; [ObservableProperty] private bool _isRevealed; public VorgangItem(Vorgang model, string studentNames, Func onLink, Func onUnlink, Func onRemoveClassRegisterEntry, Func onEditDocumentation) { Model = model; StudentNames = studentNames; TagChips = model.Tags.Select(t => new TagChip(t)).ToList(); _onLink = onLink; _onUnlink = onUnlink; _onRemoveClassRegisterEntry = onRemoveClassRegisterEntry; _onEditDocumentation = onEditDocumentation; } [RelayCommand] private void Reveal() => IsRevealed = !IsRevealed; /// Nutzer-Feedback: standardisierte Dokumentationseinträge statt alles von Hand — nur im /// schülerbezogenen Vorgänge-Tab gesetzt (StudentVorgaengeViewModel), der Parameter ist ein /// Schlüssel aus VorgangDocumentationTemplates. Eigener Command je Item statt $parent-Bindung, /// weil die Auswahl in einem MenuFlyout-Popup liegt, das nicht im normalen visuellen Baum hängt. public Func? OnCreateFromTemplate { get; set; } public bool HasDocumentationTemplates => OnCreateFromTemplate is not null; [RelayCommand] private async Task CreateFromTemplate(string? key) { if (key is not null && OnCreateFromTemplate is not null) await OnCreateFromTemplate(this, key); } /// Öffnet den vollen Dokumentations-Dialog zum Nachlesen/Bearbeiten, ohne den Vorgänge-Tab zu /// verlassen (Nutzer-Feedback: bisher musste man dafür in den Schülerdatensatz wechseln). [RelayCommand] private async Task EditDocumentation(DocumentationItem? doc) { if (doc is not null) await _onEditDocumentation(doc); } [RelayCommand] private async Task Link(DocumentationItem? doc) { if (doc is not null) await _onLink(this, doc); } [RelayCommand] private async Task Unlink(DocumentationItem? doc) { if (doc is not null) await _onUnlink(this, doc); } [RelayCommand] private async Task RemoveClassRegisterEntry(VorgangClassRegisterEntryRow? row) { if (row is not null) await _onRemoveClassRegisterEntry(this, row); } } // ── Dialog: Vorgang anlegen/bearbeiten ──────────────────────────────────── public partial class VorgangDialogViewModel : ObservableObject { private readonly Vorgang? _editing; public ObservableCollection StudentOptions { get; } [ObservableProperty] private string _title = ""; [ObservableProperty] private string _description = ""; [ObservableProperty] private string _newTag = ""; public ObservableCollection Tags { get; } = []; public string[] TagSuggestions => VorgangTagDisplay.Suggestions; [ObservableProperty] private string _titleError = ""; [ObservableProperty] private string _studentsError = ""; private readonly bool _isNewDraft; public string DialogTitle => _editing is null || _isNewDraft ? "Vorgang anlegen" : "Vorgang bearbeiten"; public Vorgang? Result { get; private set; } /// Per Klick in die Beschreibung einfügbare Textbausteine mit dem aktuellen Stand (Fehlquote, /// Fehltermine, WebUntis-Fehlzeiten, …). Setzt sich aus festen Bausteinen (Schülerdetail) und — /// im Klassenlehrer-Tab mit Mehrfachauswahl — aus Bausteinen je ausgewählter/m Schüler*in /// zusammen, die bei jeder Änderung der Auswahl neu berechnet werden. public ObservableCollection QuickBlocks { get; } = []; public bool HasQuickBlocks => QuickBlocks.Count > 0; /// Datumsgestempelte Standardformulierungen ("Klassenleitung informiert", …), siehe VorgangPhraseBlocks. public IReadOnlyList PhraseBlocks { get; } public bool HasPhraseBlocks => PhraseBlocks.Count > 0; private readonly IReadOnlyList _fixedQuickBlocks; private readonly Func, IReadOnlyList>? _studentQuickBlocks; /// editing ist ein noch nicht gespeicherter, vorbefüllter Entwurf (z.B. /// aus einer Dashboard-Warnung) — Titel "anlegen" statt "bearbeiten". public VorgangDialogViewModel(List rosterStudents, Vorgang? editing, IReadOnlyList? quickBlocks = null, bool isNewDraft = false, IReadOnlyList? phraseBlocks = null, Func, IReadOnlyList>? studentQuickBlocks = null) { _editing = editing; _isNewDraft = isNewDraft; _fixedQuickBlocks = quickBlocks ?? []; _studentQuickBlocks = studentQuickBlocks; PhraseBlocks = phraseBlocks ?? []; // Bei genau einem Kandidaten (z.B. der schülerbezogene Vorgänge-Tab im Schülerdetail) und // einem neuen Vorgang ist ohnehin nur eine Auswahl möglich — Nutzer-Feedback: der sonst // nötige Extra-Klick auf die einzige Checkbox ist unnötige Reibung. StudentOptions = new ObservableCollection(rosterStudents.Select(s => new VorgangStudentOption(s.Id, s.Name) { IsSelected = editing?.StudentIds.Contains(s.Id) == true || (editing is null && rosterStudents.Count == 1), })); if (_studentQuickBlocks is not null) foreach (var option in StudentOptions) option.PropertyChanged += (_, e) => { if (e.PropertyName == nameof(VorgangStudentOption.IsSelected)) RebuildQuickBlocks(); }; RebuildQuickBlocks(); if (editing is null) return; Title = editing.Title; Description = editing.Description; foreach (var tag in editing.Tags) Tags.Add(tag); } [RelayCommand] private void AddTag() { if (string.IsNullOrWhiteSpace(NewTag)) return; var tag = NewTag.Trim(); if (!Tags.Contains(tag)) Tags.Add(tag); NewTag = ""; } [RelayCommand] private void RemoveTag(string? tag) { if (tag is not null) Tags.Remove(tag); } /// Hängt den Baustein als eigenen Absatz an — nie überschreiben, damit bereits Getipptes bleibt. [RelayCommand] private void InsertQuickBlock(VorgangQuickBlock? block) { if (block is null) return; var current = (Description ?? "").TrimEnd(); Description = current.Length == 0 ? block.Text : current + "\n\n" + block.Text; } /// Standardformulierungen sind Verlaufsnotizen — eine Zeile, kein eigener Absatz. [RelayCommand] private void InsertPhrase(VorgangQuickBlock? block) { if (block is null) return; var current = (Description ?? "").TrimEnd(); Description = current.Length == 0 ? block.Text : current + "\n" + block.Text; } private void RebuildQuickBlocks() { QuickBlocks.Clear(); foreach (var block in _fixedQuickBlocks) QuickBlocks.Add(block); if (_studentQuickBlocks is not null) { var selected = StudentOptions.Where(o => o.IsSelected).Select(o => o.StudentId).ToList(); foreach (var block in _studentQuickBlocks(selected)) QuickBlocks.Add(block); } OnPropertyChanged(nameof(HasQuickBlocks)); } [RelayCommand] private void Save() { TitleError = ""; StudentsError = ""; var valid = true; if (string.IsNullOrWhiteSpace(Title)) { TitleError = "Titel erforderlich."; valid = false; } var selectedIds = StudentOptions.Where(s => s.IsSelected).Select(s => s.StudentId).ToList(); if (selectedIds.Count == 0) { StudentsError = "Mindestens eine/n Schüler*in auswählen."; valid = false; } if (!valid) return; Result = _editing ?? new Vorgang(); Result.Title = Title.Trim(); Result.Description = (Description ?? "").Trim(); Result.StudentIds = selectedIds; Result.Tags = Tags.ToList(); } } // ── Dialog: Klassenbuchzeile an (bestehenden oder neuen) Vorgang anheften ──── public partial class PinToVorgangDialogViewModel : ObservableObject { public string RowSummary { get; } public List MatchingOpenCases { get; } public bool HasMatchingOpenCases => MatchingOpenCases.Count > 0; [ObservableProperty] private VorgangItem? _selectedCase; [ObservableProperty] private string _newTitle = ""; [ObservableProperty] private string _error = ""; public PinToVorgangChoice? Result { get; private set; } public PinToVorgangDialogViewModel(string rowSummary, List matchingOpenCases) { RowSummary = rowSummary; MatchingOpenCases = matchingOpenCases; } [RelayCommand] private void Confirm() { Error = ""; if (SelectedCase is not null) { Result = new PinToVorgangChoice(SelectedCase.Model.Id, null); return; } if (!string.IsNullOrWhiteSpace(NewTitle)) { Result = new PinToVorgangChoice(null, NewTitle.Trim()); return; } Error = "Bestehenden Vorgang wählen oder Titel für einen neuen Vorgang eingeben."; } }