125 lines
5.0 KiB
C#
125 lines
5.0 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.Salary
|
||
{
|
||
/// <summary>
|
||
/// Логика взаимодействия для EditSalaryWindow.xaml
|
||
/// </summary>
|
||
public partial class EditSalaryWindow : Window
|
||
{
|
||
private readonly ISalaryLogic _salaryLogic; // Логика для работы с зарплатами
|
||
private readonly IEmployeeLogic _employeeLogic; // Логика для работы с сотрудниками
|
||
private List<SalaryViewModel> _salaries;
|
||
private List<EmployeeViewModel> _employees; // Список сотрудников
|
||
|
||
public EditSalaryWindow(ISalaryLogic salaryLogic, IEmployeeLogic employeeLogic)
|
||
{
|
||
_salaryLogic = salaryLogic;
|
||
_employeeLogic = employeeLogic;
|
||
InitializeComponent();
|
||
LoadSalaries();
|
||
LoadEmployees();
|
||
}
|
||
|
||
private void LoadSalaries()
|
||
{
|
||
_salaries = _salaryLogic.GetFullList();
|
||
SalaryComboBox.ItemsSource = _salaries;
|
||
SalaryComboBox.DisplayMemberPath = "EmployeeName";
|
||
SalaryComboBox.SelectedValuePath = "Id";
|
||
}
|
||
|
||
private void LoadEmployees()
|
||
{
|
||
_employees = _employeeLogic.GetFullList(); // Получение списка всех сотрудников
|
||
EmployeeComboBox.ItemsSource = _employees;
|
||
EmployeeComboBox.DisplayMemberPath = "FullName"; // Предполагается, что у сотрудника есть свойство FullName
|
||
EmployeeComboBox.SelectedValuePath = "Id";
|
||
}
|
||
|
||
private void SalaryComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||
{
|
||
if (SalaryComboBox.SelectedValue is int selectedSalaryId)
|
||
{
|
||
LoadSalary(selectedSalaryId);
|
||
}
|
||
}
|
||
|
||
private void LoadSalary(int salaryId)
|
||
{
|
||
var salary = _salaryLogic.GetElement(salaryId);
|
||
if (salary != null)
|
||
{
|
||
CountHoursTextBox.Text = salary.CountHours.ToString();
|
||
PriceHourTextBox.Text = salary.PriceHour.ToString();
|
||
PremiumTextBox.Text = salary.Premium?.ToString() ?? string.Empty;
|
||
DatePicker.SelectedDate = salary.Date;
|
||
PassedCheckBox.IsChecked = salary.Passed;
|
||
|
||
// Устанавливаем выбранного сотрудника
|
||
EmployeeComboBox.SelectedValue = salary.EmployeeId;
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Зарплата не найдена", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
|
||
}
|
||
}
|
||
|
||
private void SearchTextBox_TextChanged(object sender, TextChangedEventArgs e)
|
||
{
|
||
var searchText = SearchTextBox.Text.ToLower();
|
||
var filteredSalaries = _salaries
|
||
.Where(sal => sal.EmployeeName.ToLower().Contains(searchText))
|
||
.ToList();
|
||
|
||
SalaryComboBox.ItemsSource = filteredSalaries;
|
||
}
|
||
|
||
private void SaveButton_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
try
|
||
{
|
||
if (SalaryComboBox.SelectedValue is int selectedSalaryId)
|
||
{
|
||
var updatedSalary = new SalaryViewModel
|
||
{
|
||
Id = selectedSalaryId,
|
||
CountHours = int.Parse(CountHoursTextBox.Text),
|
||
PriceHour = float.Parse(PriceHourTextBox.Text),
|
||
Premium = float.TryParse(PremiumTextBox.Text, out var premium) ? premium : (float?)null,
|
||
Date = DatePicker.SelectedDate.Value.ToUniversalTime(),
|
||
Passed = PassedCheckBox.IsChecked ?? false,
|
||
EmployeeId = (int?)EmployeeComboBox.SelectedValue // Устанавливаем выбранного сотрудника
|
||
};
|
||
|
||
_salaryLogic.Update(updatedSalary);
|
||
|
||
MessageBox.Show("Зарплата успешно обновлена!");
|
||
this.Close();
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Выберите зарплату перед сохранением!", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"Ошибка при сохранении данных: {ex.Message}", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
|
||
}
|
||
}
|
||
}
|
||
} |