88 lines
3.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 = "Name"; // "FullName" - свойство с именем
PhysicalPersonComboBox.SelectedValuePath = "Id"; // "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 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);
}
}
}
}