Demka_kur/OknaFunciy/SotrudnikiWindow.axaml.cs

116 lines
3.6 KiB
C#
Raw Permalink Normal View History

2025-03-05 09:12:58 +00:00
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() // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><>
{
_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);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> DataGrid
this.FindControl<DataGrid>("SotrudnikiDataGrid").ItemsSource = null;
this.FindControl<DataGrid>("SotrudnikiDataGrid").ItemsSource = _sotrudnikiList;
}
else
{
ShowError("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.");
}
}
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) // <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
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("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>.");
}
}
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 = "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>",
Content = mes,
Width = 300,
Height = 200
};
await dialog.ShowDialog(this);
}
}