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.Generic; using System.Collections.ObjectModel; using System.Globalization; using System.Linq; 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 ObservableCollection _selectedItems = new(); public ObservableCollection SelectedItems { get => _selectedItems; set => this.RaiseAndSetIfChanged(ref _selectedItems, value); } private GroupPresenter? _selectedGroup; public GroupPresenter? SelectedGroup { get => _selectedGroup; set { this.RaiseAndSetIfChanged(ref _selectedGroup, value); FilterAttendanceRecords(); } } private List _selectedDate = new List(); public List SelectedDate { get => _selectedDate; set { this.RaiseAndSetIfChanged(ref _selectedDate, value); FilterAttendanceRecords(); } } public ReactiveCommand NavigateBackCommand { get; } public ReactiveCommand DeleteSelectedPresenceCommand { get; } public PresenceViewModel(GroupUseCase groupUseCase, PresenceUseCase presenceUseCase) { _groupUseCase = groupUseCase; _presenceUseCase = presenceUseCase; NavigateBackCommand = ReactiveCommand.Create(() => { }); DeleteSelectedPresenceCommand = ReactiveCommand.Create(DeleteSelectedItems); 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 }); } } public void FilterAttendanceRecords() { if (SelectedGroup == null || SelectedDate == null) { AttendanceRecords.Clear(); return; } AttendanceRecords.Clear(); foreach (var selDate in SelectedDate) { var records = _presenceUseCase.GetPresenceByGroupAndDate( SelectedGroup.Id, DateOnly.FromDateTime(selDate.Value)); foreach (var record in records) { AttendanceRecords.Add(new PresencePresenter { Id = record.Id, Date = record.Date, ClassNumber = record.ClassNumber, IsAttendence = record.IsAttendence, User = new UserPresenter { Id = record.User.Id, Name = record.User.FIO } }); } } // Логирование для отладки Console.WriteLine($"Загружено записей посещаемости: {AttendanceRecords.Count}"); } private void DeleteSelectedItems() { if (SelectedItems.Any()) { foreach (var item in SelectedItems.ToList()) { _presenceUseCase.DeletePresenceByClassNumberAndDateAndUserId(item.ClassNumber, item.Date, item.User.Id); // Удаляем из базы данных AttendanceRecords.Remove(item); // Удаляем из коллекции } } } } }