236 lines
9.6 KiB
C#
236 lines
9.6 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
|
||
foreach (ComboBoxItem item in GenderComboBox.Items)
|
||
{
|
||
if (item.Content.ToString() == person.Gender)
|
||
{
|
||
GenderComboBox.SelectedItem = item;
|
||
break;
|
||
}
|
||
}
|
||
|
||
AddressTextBox.Text = person.Address;
|
||
TelephoneTextBox.Text = person.Telephone;
|
||
}
|
||
}
|
||
|
||
private void NameTextBox_PreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
|
||
{
|
||
// Разрешаем только буквы
|
||
e.Handled = !char.IsLetter(e.Text, 0);
|
||
}
|
||
|
||
private void NameTextBox_TextChanged(object sender, TextChangedEventArgs e)
|
||
{
|
||
var textBox = sender as TextBox;
|
||
if (textBox == null) return;
|
||
|
||
// Получаем текущий текст
|
||
string currentText = textBox.Text;
|
||
|
||
// Если текст не пустой, преобразуем первую букву в заглавную, а остальные в строчные
|
||
if (!string.IsNullOrEmpty(currentText))
|
||
{
|
||
// Разбиваем строку по пробелам, чтобы обрабатывать каждое слово отдельно
|
||
var words = currentText.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
|
||
|
||
for (int i = 0; i < words.Length; i++)
|
||
{
|
||
// Преобразуем первую букву в заглавную, а остальные в строчные
|
||
words[i] = char.ToUpper(words[i][0]) + words[i].Substring(1).ToLower();
|
||
}
|
||
|
||
// Объединяем слова обратно в строку и обновляем текст
|
||
textBox.Text = string.Join(" ", words);
|
||
|
||
// Устанавливаем курсор в конец текста
|
||
textBox.SelectionStart = textBox.Text.Length;
|
||
}
|
||
}
|
||
|
||
|
||
private void TelephoneTextBox_PreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
|
||
{
|
||
e.Handled = !char.IsDigit(e.Text, 0);
|
||
}
|
||
|
||
private void TelephoneTextBox_TextChanged(object sender, TextChangedEventArgs e)
|
||
{
|
||
var textBox = sender as TextBox;
|
||
if (textBox == null) return;
|
||
|
||
// Удаляем все символы, кроме цифр
|
||
string rawInput = new string(textBox.Text.Where(char.IsDigit).ToArray());
|
||
|
||
// Добавляем "7" по умолчанию
|
||
if (!rawInput.StartsWith("7"))
|
||
rawInput = "7" + rawInput;
|
||
|
||
if (rawInput.Length > 11) rawInput = rawInput.Substring(0, 11);
|
||
|
||
// Форматируем как +7 (XXX) XXX-XX-XX
|
||
if (rawInput.Length <= 1)
|
||
textBox.Text = "+7 ";
|
||
else if (rawInput.Length <= 4)
|
||
textBox.Text = $"+7 ({rawInput.Substring(1)}";
|
||
else if (rawInput.Length <= 7)
|
||
textBox.Text = $"+7 ({rawInput.Substring(1, 3)}) {rawInput.Substring(4)}";
|
||
else if (rawInput.Length <= 9)
|
||
textBox.Text = $"+7 ({rawInput.Substring(1, 3)}) {rawInput.Substring(4, 3)}-{rawInput.Substring(7)}";
|
||
else
|
||
textBox.Text = $"+7 ({rawInput.Substring(1, 3)}) {rawInput.Substring(4, 3)}-{rawInput.Substring(7, 2)}-{rawInput.Substring(9)}";
|
||
|
||
// Устанавливаем курсор в конец
|
||
textBox.SelectionStart = textBox.Text.Length;
|
||
}
|
||
|
||
|
||
// Фильтрация списка физических лиц по всем полям
|
||
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)
|
||
{
|
||
bool isValid = true;
|
||
|
||
// Проверка обязательных полей
|
||
if (string.IsNullOrWhiteSpace(NameTextBox.Text))
|
||
{
|
||
isValid = false;
|
||
MessageBox.Show("Поле 'Имя' не заполнено.");
|
||
}
|
||
|
||
if (string.IsNullOrWhiteSpace(SurnameTextBox.Text))
|
||
{
|
||
isValid = false;
|
||
MessageBox.Show("Поле 'Фамилия' не заполнено.");
|
||
}
|
||
|
||
if (string.IsNullOrWhiteSpace(PatronomicTextBox.Text))
|
||
{
|
||
isValid = false;
|
||
MessageBox.Show("Поле 'Отчество' не заполнено.");
|
||
}
|
||
|
||
if (!BirthdayPicker.SelectedDate.HasValue)
|
||
{
|
||
isValid = false;
|
||
MessageBox.Show("Поле 'Дата рождения' не выбрано.");
|
||
}
|
||
|
||
|
||
if (GenderComboBox.SelectedItem == null)
|
||
{
|
||
isValid = false;
|
||
MessageBox.Show("Поле 'Пол' не выбрано.");
|
||
}
|
||
|
||
if (string.IsNullOrWhiteSpace(AddressTextBox.Text))
|
||
{
|
||
isValid = false;
|
||
MessageBox.Show("Поле 'Адрес' не заполнено.");
|
||
}
|
||
|
||
if (string.IsNullOrWhiteSpace(TelephoneTextBox.Text))
|
||
{
|
||
isValid = false;
|
||
MessageBox.Show("Поле 'Телефон' не заполнено.");
|
||
}
|
||
|
||
|
||
// Если все поля заполнены, продолжаем выполнение
|
||
if (isValid)
|
||
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("Данные успешно обновлены!");
|
||
this.Close();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"Ошибка: {ex.Message}");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Выберите физическое лицо перед сохранением!", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||
}
|
||
}
|
||
}
|
||
}
|