PIbd-42_Kashin_M.I_CPO_Cour.../EmployeeManagmentView/Employee/EditEmployeeWindow.xaml.cs

119 lines
4.4 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();
EmployeeComboBox.ItemsSource = _employees;
EmployeeComboBox.DisplayMemberPath = "NameJob";
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 SaveButton_Click(object sender, RoutedEventArgs e)
{
if (EmployeeComboBox.SelectedValue is int selectedEmployeeId)
{
try
{
var updatedEmployee = new EmployeeViewModel
{
Id = selectedEmployeeId,
NameJob = JobNameTextBox.Text,
StartJob = StartJobPicker.SelectedDate,
EndJob = EndJobPicker.SelectedDate,
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);
}
}
}
}