using Avalonia.Data.Converters; using presence.data.RemoteData.RemoteDataBase.DAO; using presence.domain.Models; using presence.domain.UseCase; using Presence.Desktop.Models; using Presence.Desktop.ViewModels; using ReactiveUI; using System; using System.Collections.ObjectModel; using System.Globalization; using System.Reactive; namespace Presence.Desktop.ViewModels { public class PresenceViewModel : ViewModelBase, IRoutableViewModel { public string? UrlPathSegment { get; } = "Presence"; public IScreen HostScreen { get; } private readonly GroupUseCase _groupUseCase; private readonly PresenceUseCase _presenceUseCase; public ObservableCollection AttendanceRecords { get; set; } = new(); public ObservableCollection Groups { get; set; } = new(); private GroupPresenter? _selectedGroup; public GroupPresenter? SelectedGroup { get => _selectedGroup; set { this.RaiseAndSetIfChanged(ref _selectedGroup, value); FilterAttendanceRecords(); } } private DateOnly? _selectedDate; public DateOnly? SelectedDate { get => _selectedDate; set { this.RaiseAndSetIfChanged(ref _selectedDate, value); FilterAttendanceRecords(); } } public ReactiveCommand NavigateBackCommand { get; } public PresenceViewModel(GroupUseCase groupUseCase) { _groupUseCase = groupUseCase; NavigateBackCommand = ReactiveCommand.Create(() => { }); LoadGroups(); } private void LoadGroups() { Groups.Clear(); var groups = _groupUseCase.GetAllGroups(); foreach (var group in groups) { Groups.Add(new GroupPresenter { Id = group.Id, Name = group.Name }); } } private void FilterAttendanceRecords() { if (SelectedGroup == null || SelectedDate == null) { AttendanceRecords.Clear(); return; } var records = _presenceUseCase.GetPresenceByGroupAndDate( SelectedGroup.Id, SelectedDate.Value); AttendanceRecords.Clear(); foreach (var record in records) { AttendanceRecords.Add(new PresencePresenter { Date = record.Date, ClassNumber = record.ClassNumber, IsAttendence = record.IsAttendence, User = new UserPresenter { Id = record.User.Id, Name= record.User.FIO } }); } } } }