2025-04-02 10:09:44 +00:00
|
|
|
|
using System.Linq;
|
2025-04-01 11:42:23 +00:00
|
|
|
|
using Avalonia;
|
|
|
|
|
using Avalonia.Controls;
|
2025-04-02 10:09:44 +00:00
|
|
|
|
using Avalonia.Interactivity;
|
2025-04-01 11:42:23 +00:00
|
|
|
|
using Avalonia.Markup.Xaml;
|
2025-04-03 20:00:11 +00:00
|
|
|
|
using demofinish.Model;
|
2025-04-02 10:09:44 +00:00
|
|
|
|
using Avalonia.Controls.ApplicationLifetimes;
|
|
|
|
|
using System;
|
2025-04-11 10:23:21 +00:00
|
|
|
|
using Avalonia.Layout;
|
|
|
|
|
using MsBox.Avalonia;
|
|
|
|
|
using MsBox.Avalonia.Enums;
|
2025-04-01 11:42:23 +00:00
|
|
|
|
|
|
|
|
|
namespace demofinish;
|
|
|
|
|
|
|
|
|
|
public partial class EditWindow : Window
|
|
|
|
|
{
|
2025-04-02 10:09:44 +00:00
|
|
|
|
private readonly MainWindow.AgentPresenter _selectedAgent;
|
|
|
|
|
private readonly User1Context _context;
|
|
|
|
|
|
2025-04-01 11:42:23 +00:00
|
|
|
|
public EditWindow()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
2025-04-02 10:09:44 +00:00
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
|
2025-04-11 08:55:27 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-04-02 10:09:44 +00:00
|
|
|
|
|
2025-04-11 10:23:21 +00:00
|
|
|
|
_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;
|
2025-04-02 10:09:44 +00:00
|
|
|
|
|
2025-04-11 10:23:21 +00:00
|
|
|
|
if (int.TryParse(PriorityBox.Text, out int priority))
|
|
|
|
|
{
|
|
|
|
|
_selectedAgent.Priority = priority;
|
|
|
|
|
}
|
2025-04-02 10:09:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-04-11 10:23:21 +00:00
|
|
|
|
if (AgentTypeBox.SelectedItem is Agenttype selectedType)
|
|
|
|
|
{
|
|
|
|
|
_selectedAgent.Agenttypeid = selectedType.Id;
|
|
|
|
|
}
|
2025-04-02 10:09:44 +00:00
|
|
|
|
|
|
|
|
|
|
2025-04-11 10:23:21 +00:00
|
|
|
|
var dbAgent = _context.Agents.FirstOrDefault(a => a.Id == _selectedAgent.Id);
|
|
|
|
|
if (dbAgent != null)
|
|
|
|
|
{
|
2025-04-11 08:55:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-04-11 10:23:21 +00:00
|
|
|
|
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;
|
2025-04-02 10:09:44 +00:00
|
|
|
|
|
2025-04-11 10:23:21 +00:00
|
|
|
|
await _context.SaveChangesAsync();
|
2025-04-02 10:09:44 +00:00
|
|
|
|
|
|
|
|
|
|
2025-04-11 10:23:21 +00:00
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void DeleteButton(object? sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
bool hasProductSales = _context.Productsales.Any(ps => ps.Agentid == _selectedAgent.Id);
|
|
|
|
|
|
|
|
|
|
if (hasProductSales)
|
|
|
|
|
{
|
|
|
|
|
ErrorTextBlock.Text = "Нельзя удалить агента с историей продаж";
|
|
|
|
|
return;
|
2025-04-02 10:09:44 +00:00
|
|
|
|
}
|
2025-04-11 10:23:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
2025-04-02 10:09:44 +00:00
|
|
|
|
|
2025-04-11 10:23:21 +00:00
|
|
|
|
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}";
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-01 11:42:23 +00:00
|
|
|
|
}
|