using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using data.Repository; using Data.DAO; using domain.Service; using domain.UseCase; using Presence.Desktop.Models; using Presense.Desktop.Models; using ReactiveUI; namespace Presence.Desktop.ViewModels; public class PresenceViewModel : ViewModelBase { private readonly PresenceService _presenceService; private readonly IGroupUseCase _groupService; private ObservableCollection _presences; private ObservableCollection _groups; private GroupPresenter? _selectedGroupItem; private string? _selectedAttendanceType; public ObservableCollection Presences { get => _presences; set => this.RaiseAndSetIfChanged(ref _presences, value); } public ObservableCollection Groups => _groups; public GroupPresenter? SelectedGroupItem { get => _selectedGroupItem; set { if (_selectedGroupItem != value) { _selectedGroupItem = value; this.RaisePropertyChanged(); UpdatePresences(); } } } public string[] AttendanceTypes { get; } = { "Был", "Не был", "Болеет" }; public string? SelectedAttendanceType { get => _selectedAttendanceType; set { this.RaiseAndSetIfChanged(ref _selectedAttendanceType, value); FilterPresences(); } } public PresenceViewModel(IGroupUseCase groupService, PresenceService presenceService) { _groupService = groupService; _presenceService = presenceService; _presences = new ObservableCollection(); _groups = new ObservableCollection(); LoadGroups(); } private void LoadGroups() { var groups = _groupService.GetGroupsWithStudents(); // Получаем все группы _groups.Clear(); foreach (var group in groups) { _groups.Add(new GroupPresenter { GroupId = group.GroupId, GroupName = group.GroupName }); } // Выбираем первую группу по умолчанию if (_groups.Any()) { SelectedGroupItem = _groups.First(); } } private void UpdatePresences() { if (SelectedGroupItem == null) return; var PresencesListHere = _presenceService.GetAttendancesRelativeWithGroup(SelectedGroupItem.GroupId); ObservableCollection convertedList = new ObservableCollection(); _presences.Clear(); foreach (var attendance in PresencesListHere) { convertedList.Add(convertToPresencePresenter(attendance)); } Presences = convertedList; } private PresencePresenter convertToPresencePresenter(Attendance attendance) { return new PresencePresenter() { AttendanceId = attendance.AttendanceId, StudentFullName = $"{attendance.Student.FirstName} {attendance.Student.LastName} {attendance.Student.Patronymic}", Subject = attendance.GroupSubject.Subject.SubjectName, Date = attendance.Date, GroupName = attendance.Group.GroupName, LessonNumber = attendance.LessonNumber, PresenceType = attendance.Visit.VisitName }; } private void FilterPresences() { if (string.IsNullOrEmpty(SelectedAttendanceType)) return; UpdatePresences(); var filteredPresences = _presences.Where(a => a.PresenceType == SelectedAttendanceType).ToList(); _presences.Clear(); foreach (var attendance in filteredPresences) { _presences.Add(attendance); } } }