112 lines
4.3 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 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();
}
private void LoadSalaries()
{
_salaries = _salaryLogic.GetFullList();
SalaryComboBox.ItemsSource = _salaries;
SalaryComboBox.DisplayMemberPath = "EmployeeName";
SalaryComboBox.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;
}
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,
};
_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);
}
}
}
}