Blagodat/OknaFunciy/SotrudnikiWindow.axaml.cs
2025-04-23 10:04:16 +03:00

148 lines
4.2 KiB
C#

using Avalonia.Controls;
using Avalonia.Interactivity;
using Demka_Snova_1.Hardik.Conect;
using Demka_Snova_1.Hardik.Conect.Dao;
using Demka_Snova_1.OknaRoley;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Demka_Snova_1.OknaFunciy;
public partial class SotrudnikiWindow : Window
{
private List<SotrudnikDao> _sotrudnikiList;
private List<historyDao> _activityList;
public SotrudnikiWindow()
{
InitializeComponent();
this.AttachedToVisualTree += async (s, e) => await LoadSotrudnikiAsync();
}
private async Task LoadSotrudnikiAsync()
{
try
{
using (var db = new DatabaseConnection())
{
_sotrudnikiList = db.GetAllSotrudniki();
_activityList = db.GetAllHistory();
this.FindControl<DataGrid>("SotrudnikiDataGrid").ItemsSource = _sotrudnikiList;
}
}
catch (Exception ex)
{
await ShowErrorDialog($"Îøèáêà ïðè çàãðóçêå äàííûõ: {ex.Message}");
}
}
private async void AddSotrudnik(object sender, RoutedEventArgs e)
{
var addWindow = new AddSotrudnikWindow();
var result = await addWindow.ShowDialog<SotrudnikDao>(this);
if (result != null)
{
try
{
using (var db = new DatabaseConnection())
{
result.ID = _sotrudnikiList.Count > 0 ? _sotrudnikiList.Max(s => s.ID) + 1 : 1;
db.AddSotrudnik(result);
await LoadSotrudnikiAsync();
}
}
catch (Exception ex)
{
await ShowErrorDialog($"Îøèáêà ïðè äîáàâëåíèè ñîòðóäíèêà: {ex.Message}");
}
}
}
private async void DeleteSotrudnik(object sender, RoutedEventArgs e)
{
var selected = GetSelectedSotrudnik();
if (selected != null)
{
try
{
using (var db = new DatabaseConnection())
{
db.DeleteSotrudnik(selected.ID);
await LoadSotrudnikiAsync();
}
}
catch (Exception ex)
{
await ShowErrorDialog($"Îøèáêà ïðè óäàëåíèè ñîòðóäíèêà: {ex.Message}");
}
}
else
{
await ShowErrorDialog("Âûáåðèòå ñîòðóäíèêà äëÿ óäàëåíèÿ.");
}
}
private SotrudnikDao GetSelectedSotrudnik()
{
return this.FindControl<DataGrid>("SotrudnikiDataGrid").SelectedItem as SotrudnikDao;
}
private async void ChangeRole(object sender, RoutedEventArgs e)
{
var selected = GetSelectedSotrudnik();
if (selected != null)
{
var roleWindow = new ChangeRoleWindow(selected);
var result = await roleWindow.ShowDialog<SotrudnikDao>(this);
if (result != null)
{
try
{
using (var db = new DatabaseConnection())
{
db.UpdateSotrudnik(result);
await LoadSotrudnikiAsync();
}
}
catch (Exception ex)
{
await ShowErrorDialog($"Îøèáêà ïðè èçìåíåíèè ðîëè: {ex.Message}");
}
}
}
else
{
await ShowErrorDialog("Âûáåðèòå ñîòðóäíèêà äëÿ èçìåíåíèÿ ðîëè.");
}
}
private void Exitka(object sender, RoutedEventArgs e)
{
new AdminWindow().Show();
this.Close();
}
private async void ViewActivity(object sender, RoutedEventArgs e)
{
var activityWindow = new ActivityWindow(_activityList);
await activityWindow.ShowDialog(this);
}
private async Task ShowErrorDialog(string message)
{
var dialog = new Window
{
Title = "Îøèáêà",
Content = new TextBlock { Text = message },
Width = 300,
Height = 200,
WindowStartupLocation = WindowStartupLocation.CenterScreen
};
await dialog.ShowDialog(this);
}
}