Demka_kur/OknaFunciy/SotrudnikiWindow.axaml.cs
2025-03-05 12:12:58 +03:00

116 lines
3.6 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 Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Demka_Snova_1.OknaFunciy;
public partial class SotrudnikiWindow : Window
{
private List<SotrudnikDao> _sotrudnikiList;
private List<historyDao> _activityList;
private readonly AppDbContext _dbContext;
public SotrudnikiWindow()
{
InitializeComponent();
_dbContext = new AppDbContext();
LoadSotrudnikiAsync().Wait();
this.DataContext = this;
}
private async Task LoadSotrudnikiAsync() // àñèíõðîííàÿ çàãðóçêà èç ÁÄ
{
_sotrudnikiList = await _dbContext.sotrudnik.ToListAsync();
this.FindControl<DataGrid>("SotrudnikiDataGrid").ItemsSource = _sotrudnikiList;
}
private void AddSotrudnik(object sender, RoutedEventArgs e)
{
var addWindow = new AddSotrudnikWindow();
addWindow.Closed += (s, args) =>
{
if (addWindow.NewSotrudnik != null)
{
_sotrudnikiList.Add(addWindow.NewSotrudnik);
this.FindControl<DataGrid>("SotrudnikiDataGrid").ItemsSource = null;
this.FindControl<DataGrid>("SotrudnikiDataGrid").ItemsSource = _sotrudnikiList;
}
};
addWindow.ShowDialog(this);
}
private void DeleteSotrudnik(object sender, RoutedEventArgs e)
{
var selectedSotrudnik = GetSelectedSotrudnik();
if (selectedSotrudnik != null)
{
_sotrudnikiList.Remove(selectedSotrudnik);
// Îáíîâëåíèå DataGrid
this.FindControl<DataGrid>("SotrudnikiDataGrid").ItemsSource = null;
this.FindControl<DataGrid>("SotrudnikiDataGrid").ItemsSource = _sotrudnikiList;
}
else
{
ShowError("Âûáåðèòå ñîòðóäíèêà äëÿ óäàëåíèÿ.");
}
}
private SotrudnikDao GetSelectedSotrudnik()
{
return this.FindControl<DataGrid>("SotrudnikiDataGrid").SelectedItem as SotrudnikDao;
}
private void ChangeRole(object sender, RoutedEventArgs e)
{
var selectedSotrudnik = GetSelectedSotrudnik();
if (selectedSotrudnik != null)
{
var roleWindow = new ChangeRoleWindow(selectedSotrudnik);
roleWindow.Closed += (s, args) =>
{
if (roleWindow.UpdatedSotrudnik != null) // äëÿ îáíîâëåíèÿ ñïèñêà ñîòðóäíåêîâ
{
var index = _sotrudnikiList.IndexOf(selectedSotrudnik);
_sotrudnikiList[index] = roleWindow.UpdatedSotrudnik;
this.FindControl<DataGrid>("SotrudnikiDataGrid").ItemsSource = null;
this.FindControl<DataGrid>("SotrudnikiDataGrid").ItemsSource = _sotrudnikiList;
}
};
roleWindow.ShowDialog(this);
}
else
{
ShowError("Âûáåðèòå ñîòðóäíèêà äëÿ èçìåíåíèÿ ðîëè.");
}
}
private void ViewActivity(object sender, RoutedEventArgs e)
{
var activityWindow = new ActivityWindow(_activityList);
activityWindow.ShowDialog(this);
}
private void Exitka(object sender, RoutedEventArgs e)
{
var login = new AdminWindow();
login.Show();
this.Close();
}
async void ShowError(string mes)
{
var dialog = new Window
{
Title = "Îøèáêà",
Content = mes,
Width = 300,
Height = 200
};
await dialog.ShowDialog(this);
}
}