using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using LehrerApp.Core.Interfaces; using LehrerApp.Core.Models; using System.Collections.ObjectModel; namespace LehrerApp.Desktop.ViewModels.Students; public partial class StudentListViewModel : ObservableObject { private readonly IStudentRepository _students; public Action? OnNavigateToDetail { get; set; } [ObservableProperty] private string _searchText = ""; [ObservableProperty] private bool _showInactive; [ObservableProperty] private StudentListItem? _selectedStudent; public ObservableCollection Students { get; } = []; public StudentListViewModel(IStudentRepository students) { _students = students; LoadStudents(); } partial void OnSearchTextChanged(string value) => LoadStudents(); partial void OnShowInactiveChanged(bool value) => LoadStudents(); partial void OnSelectedStudentChanged(StudentListItem? value) { if (value is not null) OnNavigateToDetail?.Invoke(value.Id); } public void LoadStudents() { Students.Clear(); var all = _students.GetAll(ShowInactive); var f = string.IsNullOrWhiteSpace(SearchText) ? all : all.Where(s => s.LastName.Contains(SearchText, StringComparison.OrdinalIgnoreCase) || s.FirstName.Contains(SearchText, StringComparison.OrdinalIgnoreCase)); foreach (var s in f) Students.Add(new StudentListItem(s)); } [RelayCommand] private void AddStudent() { /* TODO */ } [RelayCommand] private void Refresh() => LoadStudents(); } public class StudentListItem { public Guid Id { get; } public string FullName { get; } public string DateOfBirth { get; } public StudentListItem(Student s) { Id = s.Id; FullName = s.FullName; DateOfBirth = s.DateOfBirth?.ToString("dd.MM.yyyy") ?? ""; } } public partial class StudentDetailViewModel : ObservableObject { private readonly IStudentRepository _students; private readonly IEnrollmentRepository _enrollments; private readonly IGroupRepository _groups; private readonly IDocumentationRepository _docs; [ObservableProperty] private Student? _student; [ObservableProperty] private string _studentTitle = ""; [ObservableProperty] private bool _isEditing; [ObservableProperty] private string _editFirstName = ""; [ObservableProperty] private string _editLastName = ""; public ObservableCollection Enrollments { get; } = []; public ObservableCollection Documentation { get; } = []; public StudentDetailViewModel(IStudentRepository students, IEnrollmentRepository enrollments, IGroupRepository groups, IDocumentationRepository docs) { _students = students; _enrollments = enrollments; _groups = groups; _docs = docs; } public void LoadStudent(Guid id) { Student = _students.GetById(id); if (Student is null) return; StudentTitle = Student.FullName; EditFirstName = Student.FirstName; EditLastName = Student.LastName; Enrollments.Clear(); foreach (var e in _enrollments.GetByStudent(Student.Id)) { var g = _groups.GetById(e.GroupId); if (g is null) continue; Enrollments.Add(new() { SchoolYear = e.SchoolYear, GroupName = g.Name, Subject = g.Subject ?? "" }); } Documentation.Clear(); foreach (var d in _docs.GetByStudent(Student.Id)) Documentation.Add(new() { Date = d.Date.ToString("dd.MM.yyyy"), Title = d.Title, TypeLabel = d.Type switch { DocumentationType.Conversation => "Gespräch", DocumentationType.Incident => "Vorkommnis", DocumentationType.SupportPlan => "Förderplan", DocumentationType.Absence => "Fehlzeit", _ => "", }, IsConfidential = d.IsConfidential }); } [RelayCommand] private void StartEdit() => IsEditing = true; [RelayCommand] private void CancelEdit() { if (Student is null) return; EditFirstName = Student.FirstName; EditLastName = Student.LastName; IsEditing = false; } [RelayCommand] private void SaveEdit() { if (Student is null) return; Student.FirstName = EditFirstName; Student.LastName = EditLastName; _students.Save(Student); StudentTitle = Student.FullName; IsEditing = false; } } public class EnrollmentEntry { public string SchoolYear { get; set; } = ""; public string GroupName { get; set; } = ""; public string Subject { get; set; } = ""; } public class DocEntry { public string Date { get; set; } = ""; public string Title { get; set; } = ""; public string TypeLabel { get; set; } = ""; public bool IsConfidential { get; set; } }