PIbd-42_Kashin_M.I_CPO_Cour.../EmployeeManagmentView/PhisicalPersonWindow.xaml.cs

150 lines
5.3 KiB
C#
Raw Permalink Normal View History

using EmployeeManagmentContracts.BindingModels;
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
{
/// <summary>
/// Логика взаимодействия для PhisicalPersonWindow.xaml
/// </summary>
public partial class PhisicalPersonWindow : Window
{
private readonly IPhisicalPersonLogic _phisicalPersonLogic;
public PhisicalPersonWindow(IPhisicalPersonLogic phisicalPersonLogic)
{
InitializeComponent();
_phisicalPersonLogic = phisicalPersonLogic;
LoadData();
}
// Загрузка данных в DataGrid
private void LoadData()
{
try
{
var physicalPersons = _phisicalPersonLogic.GetPhisicalPersons(null); // Здесь можно передавать параметры для фильтрации
PhysicalPersonsDataGrid.ItemsSource = physicalPersons;
}
catch (Exception ex)
{
MessageBox.Show($"Error loading data: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
var textBox = sender as TextBox;
if (textBox != null && textBox.Text == "Enter Name")
{
textBox.Text = "";
textBox.Foreground = new SolidColorBrush(Colors.Black); // Change text color to black
}
}
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
var textBox = sender as TextBox;
if (textBox != null && string.IsNullOrEmpty(textBox.Text))
{
textBox.Text = "Enter Name"; // Reset placeholder text
textBox.Foreground = new SolidColorBrush(Colors.Gray); // Change text color to gray
}
}
// Обработчик для кнопки "Add"
private void AddButton_Click(object sender, RoutedEventArgs e)
{
var model = new PhisicalPersonBindingModel
{
Name = NameTextBox.Text,
Surname = SurnameTextBox.Text,
Birthday = DateTime.TryParse(BirthdayTextBox.Text, out DateTime birthday) ? birthday : DateTime.Now,
Address = AddressTextBox.Text,
Telephone = TelephoneTextBox.Text,
Gender = GenderTextBox.Text
};
try
{
_phisicalPersonLogic.CreateOrUpdate(model);
MessageBox.Show("Physical person added successfully!");
LoadData();
}
catch (Exception ex)
{
MessageBox.Show($"Error adding data: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
// Обработчик для кнопки "Update"
private void UpdateButton_Click(object sender, RoutedEventArgs e)
{
// Получаем выбранную запись в DataGrid
var selectedPerson = PhysicalPersonsDataGrid.SelectedItem as PhisicalPersonViewModel;
if (selectedPerson == null)
{
MessageBox.Show("No person selected.");
return;
}
var model = new PhisicalPersonBindingModel
{
Id = selectedPerson.Id,
Name = NameTextBox.Text,
Surname = SurnameTextBox.Text,
Birthday = DateTime.TryParse(BirthdayTextBox.Text, out DateTime birthday) ? birthday : DateTime.Now,
Address = AddressTextBox.Text,
Telephone = TelephoneTextBox.Text,
Gender = GenderTextBox.Text
};
try
{
_phisicalPersonLogic.CreateOrUpdate(model);
MessageBox.Show("Physical person updated successfully!");
LoadData();
}
catch (Exception ex)
{
MessageBox.Show($"Error updating data: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
// Обработчик для кнопки "Delete"
private void DeleteButton_Click(object sender, RoutedEventArgs e)
{
var selectedPerson = PhysicalPersonsDataGrid.SelectedItem as PhisicalPersonViewModel;
if (selectedPerson == null)
{
MessageBox.Show("No person selected.");
return;
}
try
{
_phisicalPersonLogic.Delete(selectedPerson.Id);
MessageBox.Show("Physical person deleted successfully!");
LoadData();
}
catch (Exception ex)
{
MessageBox.Show($"Error deleting data: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}