243 lines
9.5 KiB
C#
243 lines
9.5 KiB
C#
using EmployeeManagmentContracts.BusinessLogicContracts;
|
||
using EmployeeManagmentContracts.ViewModels;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows;
|
||
using System.Windows.Controls;
|
||
using System.Windows.Data;
|
||
using System.Windows.Documents;
|
||
using System.Windows.Input;
|
||
using System.Windows.Media;
|
||
using System.Windows.Media.Imaging;
|
||
using System.Windows.Shapes;
|
||
|
||
namespace EmployeeManagmentView.Employee
|
||
{
|
||
/// <summary>
|
||
/// Логика взаимодействия для EditEmployeeWindow.xaml
|
||
/// </summary>
|
||
public partial class EditEmployeeWindow : Window
|
||
{
|
||
|
||
private readonly IEmployeeLogic _employeeLogic;
|
||
private readonly IPhisicalPersonLogic _physicalPersonLogic;
|
||
private List<EmployeeViewModel> _employees;
|
||
|
||
public EditEmployeeWindow(IEmployeeLogic employeeLogic, IPhisicalPersonLogic physicalPersonLogic)
|
||
{
|
||
_employeeLogic = employeeLogic;
|
||
_physicalPersonLogic = physicalPersonLogic;
|
||
InitializeComponent();
|
||
LoadEmployees();
|
||
LoadPhysicalPersons();
|
||
}
|
||
|
||
private void LoadEmployees()
|
||
{
|
||
_employees = _employeeLogic.GetFullList();
|
||
|
||
// Заполняем комбинированное свойство, если нужно
|
||
foreach (var employee in _employees)
|
||
{
|
||
var physicalPerson = _physicalPersonLogic.GetElement(employee.PhysicalPersonsId ?? 0);
|
||
employee.PhysicalPersonName = physicalPerson?.FullNameWithBirthday;
|
||
}
|
||
|
||
EmployeeComboBox.ItemsSource = _employees;
|
||
EmployeeComboBox.DisplayMemberPath = "DisplayText"; // Используем новое свойство
|
||
EmployeeComboBox.SelectedValuePath = "Id";
|
||
}
|
||
|
||
|
||
private void LoadPhysicalPersons()
|
||
{
|
||
var physicalPersons = _physicalPersonLogic.GetFullList();
|
||
PhysicalPersonComboBox.ItemsSource = physicalPersons;
|
||
PhysicalPersonComboBox.DisplayMemberPath = "FullNameWithBirthday";
|
||
PhysicalPersonComboBox.SelectedValuePath = "Id";
|
||
}
|
||
|
||
private void EmployeeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||
{
|
||
if (EmployeeComboBox.SelectedValue is int selectedEmployeeId)
|
||
{
|
||
LoadEmployee(selectedEmployeeId);
|
||
}
|
||
}
|
||
|
||
|
||
private void LoadEmployee(int employeeId)
|
||
{
|
||
var employee = _employeeLogic.GetElement(employeeId);
|
||
if (employee != null)
|
||
{
|
||
JobNameTextBox.Text = employee.NameJob;
|
||
StartJobPicker.SelectedDate = employee.StartJob;
|
||
EndJobPicker.SelectedDate = employee.EndJob;
|
||
PartTimeTextBox.Text = employee.PartTimeJob;
|
||
BidTextBox.Text = employee.Bid.ToString();
|
||
PhysicalPersonComboBox.SelectedValue = employee.PhysicalPersonsId;
|
||
}
|
||
}
|
||
|
||
private void SearchTextBox_TextChanged(object sender, TextChangedEventArgs e)
|
||
{
|
||
var searchText = SearchTextBox.Text.ToLower();
|
||
var filteredEmployees = _employees
|
||
.Where(emp => emp.NameJob.ToLower().Contains(searchText) ||
|
||
emp.PhysicalPersonName?.ToLower().Contains(searchText) == true)
|
||
.ToList();
|
||
|
||
EmployeeComboBox.ItemsSource = filteredEmployees;
|
||
}
|
||
|
||
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 SaveButton_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
bool isValid = true;
|
||
|
||
// Проверка обязательных полей
|
||
if (string.IsNullOrWhiteSpace(JobNameTextBox.Text))
|
||
{
|
||
isValid = false;
|
||
MessageBox.Show("Поле 'Название должности' не заполнено.");
|
||
}
|
||
|
||
if (string.IsNullOrWhiteSpace(PartTimeTextBox.Text))
|
||
{
|
||
isValid = false;
|
||
MessageBox.Show("Поле 'Совместительство' не заполнено.");
|
||
}
|
||
|
||
if (string.IsNullOrWhiteSpace(BidTextBox.Text))
|
||
{
|
||
isValid = false;
|
||
MessageBox.Show("Поле 'Ставка' не заполнено.");
|
||
}
|
||
|
||
if (!StartJobPicker.SelectedDate.HasValue)
|
||
{
|
||
isValid = false;
|
||
MessageBox.Show("Поле 'Начало работы' не выбрано.");
|
||
}
|
||
|
||
if (!EndJobPicker.SelectedDate.HasValue)
|
||
{
|
||
isValid = false;
|
||
MessageBox.Show("Поле 'Конец работы' не выбрано.");
|
||
}
|
||
|
||
if (PhysicalPersonComboBox.SelectedItem == null)
|
||
{
|
||
isValid = false;
|
||
MessageBox.Show("Поле 'Пол' не выбрано.");
|
||
}
|
||
|
||
|
||
// Если все поля заполнены, продолжаем выполнение
|
||
if (isValid)
|
||
if (EmployeeComboBox.SelectedValue is int selectedEmployeeId)
|
||
{
|
||
try
|
||
{
|
||
var updatedEmployee = new EmployeeViewModel
|
||
{
|
||
Id = selectedEmployeeId,
|
||
NameJob = JobNameTextBox.Text,
|
||
StartJob = StartJobPicker.SelectedDate.Value.ToUniversalTime(),
|
||
EndJob = EndJobPicker.SelectedDate.Value.ToUniversalTime(),
|
||
PartTimeJob = PartTimeTextBox.Text,
|
||
Bid = float.Parse(BidTextBox.Text),
|
||
PhysicalPersonsId = PhysicalPersonComboBox.SelectedValue as int?
|
||
};
|
||
|
||
_employeeLogic.Update(updatedEmployee);
|
||
MessageBox.Show("Данные успешно обновлены!");
|
||
this.Close();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"Ошибка: {ex.Message}");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Выберите сотрудника перед сохранением!", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||
}
|
||
}
|
||
}
|
||
} |