diff --git a/Policlinica/ViewLocator.cs b/Policlinica/ViewLocator.cs index ea9a044..9737ac7 100644 --- a/Policlinica/ViewLocator.cs +++ b/Policlinica/ViewLocator.cs @@ -1,6 +1,8 @@ using System; using System.Diagnostics.CodeAnalysis; +using Avalonia; using Avalonia.Controls; +using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Controls.Templates; using Policlinica.ViewModels; using Policlinica.Views; @@ -33,6 +35,16 @@ public class ViewLocator : IDataTemplate sugarVm.SetView(sugarView); } + // Специальная обработка для AdminViewModel + if (param is AdminViewModel adminVm) + { + // Получаем главное окно из ApplicationLifetime + if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) + { + adminVm.SetCloseAction(() => desktop.MainWindow?.Close()); + } + } + return view; } diff --git a/Policlinica/ViewModels/AdminViewModel.cs b/Policlinica/ViewModels/AdminViewModel.cs index 31f32f2..d3abbf8 100644 --- a/Policlinica/ViewModels/AdminViewModel.cs +++ b/Policlinica/ViewModels/AdminViewModel.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; +using Avalonia.Controls; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using Microsoft.Extensions.DependencyInjection; @@ -20,6 +21,7 @@ public partial class AdminViewModel : ViewModelBase private readonly UserRepository _userRepository; private readonly DoctorRepository _doctorRepository; private readonly ServiceRepository _serviceRepository; + private List _allRecords; // Сохраняем все записи для поиска [ObservableProperty] string _login; [ObservableProperty] int _id; @@ -27,6 +29,7 @@ public partial class AdminViewModel : ViewModelBase [ObservableProperty] private Record _selectedRecord; [ObservableProperty] private ObservableCollection userList = new ObservableCollection(); [ObservableProperty] string statusMessage = ""; + [ObservableProperty] string searchText = ""; [ObservableProperty] string editClientName = ""; [ObservableProperty] string editClientSurname = ""; @@ -37,6 +40,8 @@ public partial class AdminViewModel : ViewModelBase [ObservableProperty] decimal editTotalAmount = 0; [ObservableProperty] string editRecordDate = ""; + private Action _closeAction; + public AdminViewModel(Navigation navigation, IServiceProvider provider, RecordRep recordRep, User user, UserRepository userRepository, DoctorRepository doctorRepository, ServiceRepository serviceRepository) { _navigation = navigation; @@ -54,10 +59,16 @@ public partial class AdminViewModel : ViewModelBase Id = obj.Id; } - RecordsList = new ObservableCollection(recordRep.GetRecord(Id)); + _allRecords = recordRep.GetRecord(Id); + RecordsList = new ObservableCollection(_allRecords); DoctorList = new ObservableCollection(doctorRepository.GetDoctorsByTest()); } + public void SetCloseAction(Action closeAction) + { + _closeAction = closeAction; + } + partial void OnSelectedRecordChanged(Record value) { if (value != null) @@ -93,6 +104,34 @@ public partial class AdminViewModel : ViewModelBase } } + [RelayCommand] + void SearchRecords() + { + if (string.IsNullOrWhiteSpace(SearchText)) + { + // Если поле поиска пусто, показываем все записи + RecordsList = new ObservableCollection(_allRecords); + return; + } + + // Фильтруем записи по имени или фамилии клиента + var filteredRecords = _allRecords + .Where(r => r.ClientName.Contains(SearchText, StringComparison.OrdinalIgnoreCase) || + r.ClientSurname.Contains(SearchText, StringComparison.OrdinalIgnoreCase)) + .ToList(); + + RecordsList = new ObservableCollection(filteredRecords); + StatusMessage = $"Найдено {filteredRecords.Count} записей"; + } + + [RelayCommand] + void ShowAllRecords() + { + SearchText = ""; + RecordsList = new ObservableCollection(_allRecords); + StatusMessage = $"Показано {_allRecords.Count} записей"; + } + [RelayCommand] void UpdateRecord() { @@ -131,9 +170,11 @@ public partial class AdminViewModel : ViewModelBase if (updated) { StatusMessage = "Запись успешно обновлена"; - RecordsList = new ObservableCollection(_recordRep.GetRecord(Id)); + _allRecords = _recordRep.GetRecord(Id); + RecordsList = new ObservableCollection(_allRecords); SelectedRecord = null; ClearEditFields(); + SearchText = ""; } else { @@ -174,9 +215,11 @@ public partial class AdminViewModel : ViewModelBase if (deleted) { StatusMessage = "Запись успешно удалена"; - RecordsList = new ObservableCollection(_recordRep.GetRecord(Id)); + _allRecords = _recordRep.GetRecord(Id); + RecordsList = new ObservableCollection(_allRecords); SelectedRecord = null; ClearEditFields(); + SearchText = ""; } else { @@ -204,4 +247,11 @@ public partial class AdminViewModel : ViewModelBase _navigation.Navigate(vm); } + [RelayCommand] + void ExitApplication() + { + Console.WriteLine("Exit button clicked"); + _closeAction?.Invoke(); + } + } diff --git a/Policlinica/ViewModels/DoctorViewModel.cs b/Policlinica/ViewModels/DoctorViewModel.cs index 0601cc3..8bf1199 100644 --- a/Policlinica/ViewModels/DoctorViewModel.cs +++ b/Policlinica/ViewModels/DoctorViewModel.cs @@ -24,8 +24,6 @@ public partial class DoctorViewModel : ViewModelBase _provider = provider; _doctorList = new ObservableCollection(repository.GetDoctorsByTest()); _navigation = navigation; - - } [RelayCommand] @@ -43,5 +41,12 @@ public partial class DoctorViewModel : ViewModelBase var vm = ActivatorUtilities.CreateInstance(_provider, SelectedDoctor, repository, Name, Surname); _navigation.Navigate(vm); } + + [RelayCommand] + public void GoBack() + { + var vm = ActivatorUtilities.CreateInstance(_provider); + _navigation.Navigate(vm); + } } diff --git a/Policlinica/ViewModels/RecordItemsViewModel.cs b/Policlinica/ViewModels/RecordItemsViewModel.cs index 763c0da..28d85ce 100644 --- a/Policlinica/ViewModels/RecordItemsViewModel.cs +++ b/Policlinica/ViewModels/RecordItemsViewModel.cs @@ -64,7 +64,7 @@ public partial class RecordItemsViewModel : ViewModelBase int mainServiceId = selectedServices[0].Id; Console.WriteLine($"Main service ID: {mainServiceId}"); Console.WriteLine($"Selected services count: {selectedServices.Count}"); - + var record = new Record { ClientName = clientName, @@ -77,7 +77,7 @@ public partial class RecordItemsViewModel : ViewModelBase }; Console.WriteLine($"Saving record: Name={record.ClientName}, Surname={record.ClientSurname}, DoctorId={record.DoctorId}, UserId={record.UserId}, ServiceId={record.ServiceId}"); - + int recordId = _recordRepository.InsertRecord(record); if (recordId <= 0) @@ -88,7 +88,7 @@ public partial class RecordItemsViewModel : ViewModelBase } Console.WriteLine($"Record saved with ID: {recordId}"); - + foreach (var service in selectedServices) { Console.WriteLine($"Saving record item for service: {service.Id} (price: {service.Price})"); @@ -120,7 +120,7 @@ public partial class RecordItemsViewModel : ViewModelBase } [RelayCommand] - public void Cancel() + public void GoBack() { var repository = _provider.GetRequiredService(); var vm = ActivatorUtilities.CreateInstance(_provider, selectedDoctor, repository, clientName, clientSurname); diff --git a/Policlinica/ViewModels/RegistrationViewModel.cs b/Policlinica/ViewModels/RegistrationViewModel.cs index 5d4b672..3e5482f 100644 --- a/Policlinica/ViewModels/RegistrationViewModel.cs +++ b/Policlinica/ViewModels/RegistrationViewModel.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; @@ -14,16 +14,33 @@ public partial class RegistrationViewModel : ViewModelBase private readonly Navigation _navigation; private readonly IServiceProvider _provider; - [ObservableProperty] private string _password; - [ObservableProperty] private string _login; - [ObservableProperty] public string _eror; + [ObservableProperty] private string _password = ""; + [ObservableProperty] private string _login = ""; + [ObservableProperty] public string _eror = ""; public RegistrationViewModel(IServiceProvider serviceProvider, Navigation navigation, IServiceProvider provider) { _serviceProvider = serviceProvider; _navigation = navigation; _provider = provider; + } + partial void OnPasswordChanged(string value) + { + // Ограничиваем пароль до 8 символов + if (value != null && value.Length > 8) + { + Password = value.Substring(0, 8); + } + } + + partial void OnLoginChanged(string value) + { + // Ограничиваем логин до 15 символов + if (value != null && value.Length > 15) + { + Login = value.Substring(0, 15); + } } [RelayCommand] @@ -31,12 +48,28 @@ public partial class RegistrationViewModel : ViewModelBase { var vm = _serviceProvider.GetRequiredService(); _navigation.Navigate(vm); - } [RelayCommand] void Registration() { + if (string.IsNullOrWhiteSpace(Login)) + { + Eror = "Логин не может быть пустым"; + return; + } + + if (string.IsNullOrWhiteSpace(Password)) + { + Eror = "Пароль не может быть пустым"; + return; + } + + if (Password.Length < 4) + { + Eror = "Пароль должен быть минимум 4 символа"; + return; + } using (UserRepository repository = _provider.GetRequiredService()) { @@ -46,6 +79,7 @@ public partial class RegistrationViewModel : ViewModelBase Eror = "Такой логин уже существует"; return; } + var user = new User() { Name = Login, @@ -61,7 +95,4 @@ public partial class RegistrationViewModel : ViewModelBase _navigation.Navigate(vm); } } - - - -} \ No newline at end of file +} diff --git a/Policlinica/ViewModels/ServiceViewModel.cs b/Policlinica/ViewModels/ServiceViewModel.cs index e20c093..b490a8f 100644 --- a/Policlinica/ViewModels/ServiceViewModel.cs +++ b/Policlinica/ViewModels/ServiceViewModel.cs @@ -34,7 +34,6 @@ public partial class ServiceViewModel : ViewModelBase name = clientName; surname = clientSurname; Services = new ObservableCollection(repository.GetServicesByDoctors(selectedDoctor.Id).Select(service => new ServiceSelected(service)).ToList()); - } @@ -55,12 +54,20 @@ public partial class ServiceViewModel : ViewModelBase { return; } - + var recordRepository = _provider.GetRequiredService(); var recordItemsRepository = _provider.GetRequiredService(); + var vm = ActivatorUtilities.CreateInstance(_provider, _navigation, _selectedDoctor, selectedServices, recordRepository, recordItemsRepository, Name, Surname); _navigation.Navigate(vm); } + + [RelayCommand] + public void GoBack() + { + var vm = ActivatorUtilities.CreateInstance(_provider); + _navigation.Navigate(vm); + } } diff --git a/Policlinica/Views/AdminView.axaml b/Policlinica/Views/AdminView.axaml index 300d592..7bb1013 100644 --- a/Policlinica/Views/AdminView.axaml +++ b/Policlinica/Views/AdminView.axaml @@ -12,47 +12,88 @@ - - - + + - + + + + + + +