PIbd-42_Kashin_M.I_CPO_Cour.../EmployeeManagmentView/PhysicalPerson/EditPhysicalPersonWindow.xaml.cs

105 lines
4.3 KiB
C#

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using EmployeeManagmentContracts.BusinessLogicContracts;
using EmployeeManagmentContracts.ViewModels;
namespace EmployeeManagmentView.PhysicalPerson
{
public partial class EditPhysicalPersonWindow : Window
{
private readonly IPhisicalPersonLogic _phisicalPersonLogic;
private List<PhisicalPersonViewModel> _physicalPersons;
public EditPhysicalPersonWindow(IPhisicalPersonLogic phisicalPersonLogic)
{
_phisicalPersonLogic = phisicalPersonLogic;
InitializeComponent();
LoadPhysicalPersons();
}
// Загрузка всех физ.лиц в ComboBox
private void LoadPhysicalPersons()
{
_physicalPersons = _phisicalPersonLogic.GetFullList();
PhysicalPersonComboBox.ItemsSource = _physicalPersons;
PhysicalPersonComboBox.DisplayMemberPath = "FullNameWithBirthday";
PhysicalPersonComboBox.SelectedValuePath = "Id";
}
// Событие при выборе физ.лица
private void PhysicalPersonComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (PhysicalPersonComboBox.SelectedValue is int selectedPersonId)
{
LoadPerson(selectedPersonId);
}
}
// Загрузка данных выбранного физ.лица в поля
private void LoadPerson(int personId)
{
var person = _phisicalPersonLogic.GetElement(personId);
if (person != null)
{
NameTextBox.Text = person.Name;
SurnameTextBox.Text = person.Surname;
PatronomicTextBox.Text = person.Patronymic;
BirthdayPicker.SelectedDate = person.Birthday;
GenderComboBox.Text = person.Gender;
AddressTextBox.Text = person.Address;
TelephoneTextBox.Text = person.Telephone;
}
}
// Фильтрация списка физических лиц по всем полям
private void SearchTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
var searchText = SearchTextBox.Text.ToLower();
var filteredPersons = _physicalPersons
.Where(p => p.Name.ToLower().Contains(searchText) ||
p.Surname.ToLower().Contains(searchText) ||
p.Patronymic.ToLower().Contains(searchText) ||
p.Gender.ToLower().Contains(searchText) ||
p.Address.ToLower().Contains(searchText) ||
p.Telephone.ToLower().Contains(searchText) ||
p.Birthday.ToString("dd.MM.yyyy").Contains(searchText) // Поиск по дате рождения
).ToList();
PhysicalPersonComboBox.ItemsSource = filteredPersons;
}
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
if (PhysicalPersonComboBox.SelectedValue is int selectedPersonId)
{
try
{
var updatedPerson = new PhisicalPersonViewModel
{
Id = selectedPersonId,
Name = NameTextBox.Text,
Surname = SurnameTextBox.Text,
Patronymic = PatronomicTextBox.Text,
Birthday = BirthdayPicker.SelectedDate.Value.ToUniversalTime(),
Gender = GenderComboBox.Text,
Address = AddressTextBox.Text,
Telephone = TelephoneTextBox.Text
};
_phisicalPersonLogic.Update(updatedPerson);
MessageBox.Show("Данные успешно обновлены!");
}
catch (Exception ex)
{
MessageBox.Show($"Ошибка: {ex.Message}");
}
}
else
{
MessageBox.Show("Выберите физическое лицо перед сохранением!", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
}
}