207 lines
5.9 KiB
C#
207 lines
5.9 KiB
C#
using System.Linq;
|
||
using Avalonia;
|
||
using Avalonia.Controls;
|
||
using Avalonia.Interactivity;
|
||
using Avalonia.Markup.Xaml;
|
||
using demofinish.Model;
|
||
using Avalonia.Controls.ApplicationLifetimes;
|
||
using System;
|
||
using Avalonia.Layout;
|
||
using MsBox.Avalonia;
|
||
using MsBox.Avalonia.Enums;
|
||
|
||
namespace demofinish;
|
||
|
||
public partial class EditWindow : Window
|
||
{
|
||
private readonly MainWindow.AgentPresenter _selectedAgent;
|
||
private readonly User1Context _context;
|
||
|
||
public EditWindow()
|
||
{
|
||
InitializeComponent();
|
||
}
|
||
|
||
public EditWindow(MainWindow.AgentPresenter selectedAgent) : this()
|
||
{
|
||
_selectedAgent = selectedAgent;
|
||
_context = new User1Context();
|
||
|
||
InitializeComponent();
|
||
LoadAgentData();
|
||
}
|
||
|
||
private void LoadAgentData()
|
||
{
|
||
|
||
NameBox.Text = _selectedAgent.Title;
|
||
AdressBox.Text = _selectedAgent.Address;
|
||
PhoneBox.Text = _selectedAgent.Phone;
|
||
EmailBox.Text = _selectedAgent.Email;
|
||
InnBox.Text = _selectedAgent.Inn;
|
||
KppBox.Text = _selectedAgent.Kpp;
|
||
BossNameBox.Text = _selectedAgent.Directorname;
|
||
PriorityBox.Text = _selectedAgent.Priority.ToString();
|
||
|
||
|
||
var agentTypes = _context.Agenttypes.ToList();
|
||
AgentTypeBox.ItemsSource = agentTypes;
|
||
|
||
|
||
var currentType = agentTypes.FirstOrDefault(at => at.Id == _selectedAgent.Agenttypeid);
|
||
AgentTypeBox.SelectedItem = currentType;
|
||
}
|
||
|
||
private void BackButton(object? sender, RoutedEventArgs e)
|
||
{
|
||
Close();
|
||
}
|
||
|
||
private async void EditAgent_Button(object? sender, RoutedEventArgs e)
|
||
{
|
||
|
||
if (string.IsNullOrEmpty(NameBox.Text) ||
|
||
string.IsNullOrEmpty(InnBox.Text) || InnBox.Text.Length != 10 ||
|
||
string.IsNullOrEmpty(KppBox.Text) || KppBox.Text.Length != 9 ||
|
||
string.IsNullOrEmpty(PhoneBox.Text) ||
|
||
string.IsNullOrEmpty(AdressBox.Text) ||
|
||
string.IsNullOrEmpty(PriorityBox.Text) ||
|
||
string.IsNullOrEmpty(BossNameBox.Text) ||
|
||
string.IsNullOrEmpty(EmailBox.Text) || !EmailBox.Text.Contains("@"))
|
||
{
|
||
ErrorTextBlock.Text = "Агент не отредактирован";
|
||
return;
|
||
}
|
||
|
||
|
||
|
||
_selectedAgent.Title = NameBox.Text;
|
||
_selectedAgent.Address = AdressBox.Text;
|
||
_selectedAgent.Phone = PhoneBox.Text;
|
||
_selectedAgent.Email = EmailBox.Text;
|
||
_selectedAgent.Inn = InnBox.Text;
|
||
_selectedAgent.Kpp = KppBox.Text;
|
||
_selectedAgent.Directorname = BossNameBox.Text;
|
||
|
||
if (int.TryParse(PriorityBox.Text, out int priority))
|
||
{
|
||
_selectedAgent.Priority = priority;
|
||
}
|
||
|
||
|
||
|
||
if (AgentTypeBox.SelectedItem is Agenttype selectedType)
|
||
{
|
||
_selectedAgent.Agenttypeid = selectedType.Id;
|
||
}
|
||
|
||
|
||
var dbAgent = _context.Agents.FirstOrDefault(a => a.Id == _selectedAgent.Id);
|
||
if (dbAgent != null)
|
||
{
|
||
|
||
|
||
|
||
dbAgent.Title = _selectedAgent.Title;
|
||
dbAgent.Address = _selectedAgent.Address;
|
||
dbAgent.Phone = _selectedAgent.Phone;
|
||
dbAgent.Email = _selectedAgent.Email;
|
||
dbAgent.Inn = _selectedAgent.Inn;
|
||
dbAgent.Kpp = _selectedAgent.Kpp;
|
||
dbAgent.Directorname = _selectedAgent.Directorname;
|
||
dbAgent.Priority = _selectedAgent.Priority;
|
||
dbAgent.Agenttypeid = _selectedAgent.Agenttypeid;
|
||
|
||
await _context.SaveChangesAsync();
|
||
|
||
|
||
Close();
|
||
}
|
||
}
|
||
|
||
private async void DeleteButton(object? sender, RoutedEventArgs e)
|
||
{
|
||
|
||
bool hasProductSales = _context.Productsales.Any(ps => ps.Agentid == _selectedAgent.Id);
|
||
|
||
if (hasProductSales)
|
||
{
|
||
ErrorTextBlock.Text = "Нельзя удалить агента с историей продаж";
|
||
return;
|
||
}
|
||
|
||
|
||
var confirmDialog = new Window
|
||
{
|
||
Title = "Подтверждение удаления",
|
||
Width = 300,
|
||
Height = 150,
|
||
WindowStartupLocation = WindowStartupLocation.CenterOwner
|
||
};
|
||
|
||
var panel = new StackPanel { Margin = new Thickness(10) };
|
||
|
||
var textBlock = new TextBlock
|
||
{
|
||
Text = "Вы уверены, что хотите удалить этого агента?",
|
||
Margin = new Thickness(0, 0, 0, 10)
|
||
};
|
||
|
||
var buttonPanel = new StackPanel { Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Center };
|
||
|
||
var yesButton = new Button { Content = "Да", Margin = new Thickness(5), Width = 70 };
|
||
var noButton = new Button { Content = "Нет", Margin = new Thickness(5), Width = 70 };
|
||
|
||
bool confirmed = false;
|
||
|
||
yesButton.Click += (s, args) =>
|
||
{
|
||
confirmed = true;
|
||
confirmDialog.Close();
|
||
};
|
||
|
||
noButton.Click += (s, args) => confirmDialog.Close();
|
||
|
||
buttonPanel.Children.Add(yesButton);
|
||
buttonPanel.Children.Add(noButton);
|
||
|
||
panel.Children.Add(textBlock);
|
||
panel.Children.Add(buttonPanel);
|
||
|
||
confirmDialog.Content = panel;
|
||
|
||
await confirmDialog.ShowDialog(this);
|
||
|
||
if (!confirmed)
|
||
{
|
||
return;
|
||
}
|
||
|
||
try
|
||
{
|
||
|
||
var dbAgent = _context.Agents.FirstOrDefault(a => a.Id == _selectedAgent.Id);
|
||
if (dbAgent != null)
|
||
{
|
||
|
||
var priorityHistories = _context.Agentpriorityhistories.Where(aph => aph.Agentid == dbAgent.Id);
|
||
_context.Agentpriorityhistories.RemoveRange(priorityHistories);
|
||
|
||
|
||
var shops = _context.Shops.Where(s => s.Agentid == dbAgent.Id);
|
||
_context.Shops.RemoveRange(shops);
|
||
|
||
|
||
_context.Agents.Remove(dbAgent);
|
||
|
||
await _context.SaveChangesAsync();
|
||
|
||
Close();
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
ErrorTextBlock.Text = $"Ошибка при удалении: {ex.Message}";
|
||
}
|
||
}
|
||
} |