117 lines
3.7 KiB
C#
117 lines
3.7 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Collections.ObjectModel;
|
||
using System.Linq;
|
||
using System.Threading.Tasks;
|
||
using Avalonia.Controls;
|
||
using AvaloniaApplication14_Inventory_300326.Models.DataBase;
|
||
using AvaloniaApplication14_Inventory_300326.Models.Models;
|
||
using AvaloniaApplication14_Inventory_300326.Views;
|
||
using CommunityToolkit.Mvvm.ComponentModel;
|
||
using CommunityToolkit.Mvvm.Input;
|
||
using Microsoft.Extensions.DependencyInjection;
|
||
using MsBox.Avalonia;
|
||
using MsBox.Avalonia.Enums;
|
||
using Tmds.DBus.Protocol;
|
||
|
||
namespace AvaloniaApplication14_Inventory_300326.ViewModels;
|
||
|
||
public partial class EmployeeEditingWindowViewModel : ViewModelBase
|
||
{
|
||
private EmployeeEditingWindow _currentWindow;
|
||
private IServiceProvider _serviceProvider;
|
||
private bool _isEditing;
|
||
private Employee _employee;
|
||
|
||
[ObservableProperty] private string _fullName;
|
||
[ObservableProperty] private ObservableCollection<Position> _positions;
|
||
[ObservableProperty] private Position _selectedPosition;
|
||
|
||
[RelayCommand]
|
||
private void Cancel()
|
||
{
|
||
_currentWindow.Close();
|
||
}
|
||
|
||
[RelayCommand]
|
||
private async Task Fire()
|
||
{
|
||
List<Equipment> temp = null;
|
||
using (var repo = _serviceProvider.GetService<EquipmentRepository>())
|
||
{
|
||
temp = repo.DoesEmployeeHaveEquipments(_employee.Id);
|
||
}
|
||
|
||
if (temp != null)
|
||
{
|
||
if (temp.Count > 0)
|
||
{
|
||
string errorMessage = "За выбранным сотрудником числится следующие вещи: \n";
|
||
foreach (var item in temp)
|
||
{
|
||
errorMessage += $"{item.Name} \n";
|
||
}
|
||
var win = MessageBoxManager.GetMessageBoxStandard("Ошибка", errorMessage, ButtonEnum.Ok, Icon.Error);
|
||
await win.ShowWindowDialogAsync(_currentWindow);
|
||
}
|
||
}
|
||
}
|
||
|
||
[RelayCommand]
|
||
private async Task Confirm()
|
||
{
|
||
bool allow = true;
|
||
if (string.IsNullOrEmpty(FullName))
|
||
{
|
||
var win = MessageBoxManager.GetMessageBoxStandard("Ошибка", "Имя работника пустое", ButtonEnum.Ok,
|
||
Icon.Error, null, WindowStartupLocation.CenterOwner);
|
||
await win.ShowWindowDialogAsync(_currentWindow);
|
||
allow = false;
|
||
}
|
||
|
||
if (allow)
|
||
{
|
||
_employee.FullName = FullName;
|
||
_employee.PositionId = SelectedPosition.Id;
|
||
using (var repo = _serviceProvider.GetService<EmployeeRepository>())
|
||
{
|
||
if (_isEditing)
|
||
{
|
||
repo.Update(_employee);
|
||
}
|
||
else
|
||
{
|
||
repo.Add(_employee);
|
||
}
|
||
}
|
||
_currentWindow.Close();
|
||
}
|
||
}
|
||
|
||
public EmployeeEditingWindowViewModel(IServiceProvider serviceProvider, Employee employee)
|
||
{
|
||
_isEditing=!employee.IsNew();
|
||
_serviceProvider = serviceProvider;
|
||
_employee = employee;
|
||
using (var repo = _serviceProvider.GetService<PositionRepository>())
|
||
{
|
||
Positions = new ObservableCollection<Position>(repo.GetAll());
|
||
}
|
||
if (_isEditing)
|
||
{
|
||
FullName = employee.FullName;
|
||
SelectedPosition = Positions.FirstOrDefault(p => p.Id == employee.PositionId);
|
||
}
|
||
else
|
||
{
|
||
SelectedPosition = Positions[0];
|
||
}
|
||
}
|
||
|
||
|
||
public void SetScreen(EmployeeEditingWindow window)
|
||
{
|
||
_currentWindow = window;
|
||
_currentWindow.ConfirmButton.Content = _isEditing ? "Изменить" : "Добавить";
|
||
}
|
||
} |