Agents/MainWindow.axaml.cs
2025-03-29 22:54:15 +03:00

171 lines
6.2 KiB
C#

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;
using System.Collections.Immutable;
namespace Agents
{
public partial class MainWindow : Window
{
static User11Context db = new User11Context();
ObservableCollection<Agent> AgentsitemSource = new ObservableCollection<Agent>(db.Agents.OrderBy(it=>it.Title));
ObservableCollection<Agent> ListAgents2;
public MainWindow()
{
InitializeComponent();
ListAgents.ItemsSource = AgentsitemSource;
ListAgents2 = AgentsitemSource;
}
private void ListBox_SelectionChanged(object? sender, Avalonia.Controls.SelectionChangedEventArgs e)
{
if(sender != null)
{
try
{
UpdateInfo();
}
catch (Exception ex)
{
ExceptionDispatchInfo.Capture(ex).Throw();
}
}
}
private void SortAg(object? sender, Avalonia.Controls.SelectionChangedEventArgs e)
{
if (sender != null)
{
try
{
SortAgent();
}
catch (Exception ex)
{
ExceptionDispatchInfo.Capture(ex).Throw();
}
}
}
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();
}
}
}
private void ButtonUpdate(object sender, RoutedEventArgs e)
{
UpdateInfo();
}
private void UpdateInfo()
{
Agent? agent = db.Agents.Where(it => it == ListAgents.SelectedItem).FirstOrDefault();
if (agent != null)
{
Productsale? PS = db.Productsales.Where(it => it.Agentid == agent.Id).FirstOrDefault();
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();
switch (SortListProductAgents.SelectedIndex)
{
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();
}
private void ButtonProductEidtor(object sender, RoutedEventArgs e)
{
new ProductEditor().Show();
Close();
}
private void ButtonAddProduct(object sender, RoutedEventArgs e)
{
new AddProduct().Show();
Close();
}
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();
}
}
}
}