Agents/MainWindow.axaml.cs

171 lines
6.2 KiB
C#
Raw Normal View History

2025-03-24 09:34:12 +00:00
using Avalonia;
using Avalonia.Controls;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using System.Reflection;
using System;
using Agents.Models;
using System.Collections.ObjectModel;
using Avalonia.Interactivity;
using System.Linq;
using System.Runtime.ExceptionServices;
2025-03-29 19:54:15 +00:00
using System.Collections.Immutable;
2025-03-24 09:34:12 +00:00
namespace Agents
{
public partial class MainWindow : Window
{
static User11Context db = new User11Context();
ObservableCollection<Agent> AgentsitemSource = new ObservableCollection<Agent>(db.Agents.OrderBy(it=>it.Title));
2025-03-29 19:54:15 +00:00
ObservableCollection<Agent> ListAgents2;
2025-03-24 09:34:12 +00:00
public MainWindow()
{
InitializeComponent();
ListAgents.ItemsSource = AgentsitemSource;
2025-03-29 19:54:15 +00:00
ListAgents2 = AgentsitemSource;
2025-03-24 09:34:12 +00:00
}
private void ListBox_SelectionChanged(object? sender, Avalonia.Controls.SelectionChangedEventArgs e)
{
if(sender != null)
{
try
2025-03-24 09:34:12 +00:00
{
UpdateInfo();
2025-03-24 09:34:12 +00:00
}
catch (Exception ex)
{
ExceptionDispatchInfo.Capture(ex).Throw();
}
2025-03-24 09:34:12 +00:00
}
}
2025-03-29 19:54:15 +00:00
private void SortAg(object? sender, Avalonia.Controls.SelectionChangedEventArgs e)
2025-03-24 09:34:12 +00:00
{
if (sender != null)
{
2025-03-29 19:54:15 +00:00
try
{
SortAgent();
}
catch (Exception ex)
{
ExceptionDispatchInfo.Capture(ex).Throw();
}
}
}
2025-03-24 09:34:12 +00:00
2025-03-29 19:54:15 +00:00
void SortAgent()
{
switch (SortListAgents.SelectedIndex)
{
case 1: AgentsitemSource = new ObservableCollection<Agent>(AgentsitemSource.OrderByDescending(it => it.Title).ToList()); break;
case 2: AgentsitemSource = new ObservableCollection<Agent>(AgentsitemSource.OrderBy(it => it.Title).ToList()); break;
default: AgentsitemSource = ListAgents2; break;
}
ListAgents.ItemsSource = new ObservableCollection<Agent>(AgentsitemSource);
}
private void ComboBox_SelectionChanged_3(object? sender, Avalonia.Controls.SelectionChangedEventArgs e)
{
if (sender != null)
{
try
{
UpdateInfo();
}
catch (Exception ex)
{
ExceptionDispatchInfo.Capture(ex).Throw();
}
2025-03-24 09:34:12 +00:00
}
}
private void ButtonUpdate(object sender, RoutedEventArgs e)
{
UpdateInfo();
}
private void UpdateInfo()
{
Agent? agent = db.Agents.Where(it => it == ListAgents.SelectedItem).FirstOrDefault();
if (agent != null)
2025-03-24 09:34:12 +00:00
{
Productsale? PS = db.Productsales.Where(it => it.Agentid == agent.Id).FirstOrDefault();
2025-03-24 09:34:12 +00:00
var prods = db.Productsales
.Where(PWS => PWS.Agentid == agent.Id)
.Select(PWS => new Product
{
Title = PWS.Product.Title,
Image = PWS.Product.Image,
Producttypeid = PWS.Product.Producttypeid,
Productionpersoncount = PWS.Product.Productionpersoncount,
Productionworkshopnumber = PWS.Product.Productionworkshopnumber,
Mincostforagent = PWS.Product.Mincostforagent,
Description = PWS.Product.Description,
Articlenumber = PWS.Product.Articlenumber
}).ToList();
2025-03-29 19:54:15 +00:00
switch (SortListProductAgents.SelectedIndex)
2025-03-24 09:34:12 +00:00
{
case 1: prods = prods.OrderBy(p => p.Title).ToList(); break;
case 2: prods = prods.OrderByDescending(p => p.Title).ToList(); break;
default: break;
}
ListProducts.ItemsSource = new ObservableCollection<Product>(prods);
}
}
private void ButtonAgentEidtor(object sender, RoutedEventArgs e)
{
new AgentEidtor().Show();
Close();
}
2025-03-29 19:54:15 +00:00
private void ButtonProductEidtor(object sender, RoutedEventArgs e)
{
new ProductEditor().Show();
Close();
}
private void ButtonAddProduct(object sender, RoutedEventArgs e)
{
new AddProduct().Show();
Close();
}
2025-03-29 19:54:15 +00:00
void Button_search(object sender, RoutedEventArgs e)
{
string? Search = search.Text;
if (search.Text != "")
{
AgentsitemSource = new ObservableCollection<Agent>(db.Agents.Where(it => it.Title == Search ||
it.Address == Search ||
it.Inn == Search ||
it.Kpp == Search ||
it.Directorname == Search ||
it.Phone == Search ||
it.Email == Search ||
it.Priority == int.Parse(Search)
));
if (AgentsitemSource != null)
{
ListAgents.ItemsSource = AgentsitemSource;
ListAgents2 = AgentsitemSource;
SortAgent();
}
else
{
ListAgents.ItemsSource = null;
}
}
else
{
AgentsitemSource = new ObservableCollection<Agent>(db.Agents.OrderBy(it => it.Title));
ListAgents.ItemsSource = AgentsitemSource;
ListAgents2 = AgentsitemSource;
SortAgent();
}
}
2025-03-24 09:34:12 +00:00
}
}