demka/demofinish/MainWindow.axaml.cs
2025-04-18 14:41:58 +03:00

288 lines
9.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Media;
using Avalonia.Media.Imaging;
using demofinish.Model;
using Microsoft.EntityFrameworkCore;
namespace demofinish
{
public partial class MainWindow : Window
{
private ObservableCollection<Agent> agents = new ObservableCollection<Agent>();
private List<AgentPresenter> allAgents = new List<AgentPresenter>();
private List<AgentPresenter> filteredAgents = new List<AgentPresenter>();
private const int pageSize = 10;
private int currentPage = 1;
private int pageCount = 0;
public MainWindow()
{
InitializeComponent();
LoadAgents();
ComboboxAgentType();
InitializeSearchFields();
TypeAgentCombobox.SelectionChanged += TypeAgentCombobox_SelectionChanged;
NameComboBox.SelectionChanged += SortName_SelectedChanged;
PriorityCombobox.SelectionChanged += PrioritySort_SelectedChanged;
SaleCombobox.SelectionChanged += SaleSort_SelectedChanged;
}
public class AgentPresenter : Agent
{
public string AgentTypeName { get; set; }
public int DiscountPercent { get; set; }
public decimal TotalSales { get; set; }
public Bitmap? LogoImage
{
get
{
try
{
string absolutePath = Path.Combine(AppContext.BaseDirectory, Logo);
return new Bitmap(absolutePath);
}
catch
{
return null;
}
}
}
}
private void LoadAgents()
{
using var context = new User1Context();
allAgents = context.Agents
.Include(a => a.Agenttype)
.Include(a => a.Productsales)
.ThenInclude(ps => ps.Product)
.AsEnumerable()
.Select(agent =>
{
decimal totalSales = agent.Productsales?.Sum(ps =>
ps.Productcount * (ps.Product?.Mincostforagent ?? 0)) ?? 0;
return new AgentPresenter
{
Id = agent.Id,
Title = agent.Title,
Agenttypeid = agent.Agenttypeid,
AgentTypeName = agent.Agenttype?.Title ?? "Неизвестный тип",
Address = agent.Address,
Inn = agent.Inn,
Kpp = agent.Kpp,
Directorname = agent.Directorname,
Phone = agent.Phone,
Email = agent.Email,
Logo = agent.Logo,
Priority = agent.Priority,
TotalSales = totalSales,
DiscountPercent = CalculateDiscount(totalSales)
};
})
.ToList();
filteredAgents = allAgents.ToList();
UpdateDisplay();
}
private int CalculateDiscount(decimal totalSales)
{
if (totalSales >= 500000) return 25;
if (totalSales >= 150000) return 20;
if (totalSales >= 50000) return 10;
if (totalSales >= 10000) return 5;
return 0;
}
private void ComboboxAgentType()
{
using var context = new User1Context();
var types = context.Agenttypes.Select(x => x.Title).ToList();
types.Insert(0, "Все типы");
TypeAgentCombobox.ItemsSource = types;
TypeAgentCombobox.SelectedIndex = 0;
}
private void InitializeSearchFields()
{
using var context = new User1Context();
var searchItems = context.Agents
.Select(a => a.Title)
.Union(context.Agents.Select(a => a.Phone))
.Union(context.Agents.Select(a => a.Email))
.Where(x => !string.IsNullOrEmpty(x))
.ToList();
SearchBox.ItemsSource = searchItems;
}
private void SearchBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (SearchBox.SelectedItem == null || string.IsNullOrEmpty(SearchBox.Text))
{
ApplyFilters();
return;
}
string searchValue = SearchBox.SelectedItem.ToString();
filteredAgents = allAgents
.Where(a => a.Title.Contains(searchValue) ||
a.Phone.Contains(searchValue) ||
a.Email.Contains(searchValue))
.ToList();
ApplyCurrentSort();
UpdateDisplay();
}
private void TypeAgentCombobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ApplyFilters();
}
private void ApplyFilters()
{
if (TypeAgentCombobox.SelectedItem == null)
return;
using var context = new User1Context();
if (TypeAgentCombobox.SelectedItem.ToString() == "Все типы")
{
filteredAgents = allAgents.ToList();
}
else
{
string selectedType = TypeAgentCombobox.SelectedItem.ToString();
var selectedTypeId = context.Agenttypes
.Where(at => at.Title == selectedType)
.Select(at => at.Id)
.FirstOrDefault();
filteredAgents = allAgents
.Where(x => x.Agenttypeid == selectedTypeId)
.ToList();
}
ApplyCurrentSort();
currentPage = 1;
UpdateDisplay();
}
private void ApplyCurrentSort()
{
if (NameComboBox.SelectedItem != null)
{
filteredAgents = NameComboBox.SelectedItem.ToString() == "по возрастанию"
? filteredAgents.OrderBy(it => it.Title).ToList()
: filteredAgents.OrderByDescending(it => it.Title).ToList();
}
else if (PriorityCombobox.SelectedItem != null)
{
filteredAgents = PriorityCombobox.SelectedItem.ToString() == "по возрастанию"
? filteredAgents.OrderBy(it => it.Priority).ToList()
: filteredAgents.OrderByDescending(it => it.Priority).ToList();
}
else if (SaleCombobox.SelectedItem != null)
{
filteredAgents = SaleCombobox.SelectedItem.ToString() == "по возрастанию"
? filteredAgents.OrderBy(it => it.DiscountPercent).ToList()
: filteredAgents.OrderByDescending(it => it.DiscountPercent).ToList();
}
}
private void SortName_SelectedChanged(object sender, SelectionChangedEventArgs e)
{
if (NameComboBox.SelectedItem == null)
return;
ApplyCurrentSort();
currentPage = 1;
UpdateDisplay();
}
private void PrioritySort_SelectedChanged(object sender, SelectionChangedEventArgs e)
{
if (PriorityCombobox.SelectedItem == null)
return;
ApplyCurrentSort();
currentPage = 1;
UpdateDisplay();
}
private void SaleSort_SelectedChanged(object sender, SelectionChangedEventArgs e)
{
if (SaleCombobox.SelectedItem == null)
return;
ApplyCurrentSort();
currentPage = 1;
UpdateDisplay();
}
private void UpdateDisplay()
{
agents.Clear();
var pageAgents = filteredAgents
.Skip((currentPage - 1) * pageSize)
.Take(pageSize);
foreach (var agent in pageAgents)
{
agents.Add(agent);
}
AgentListBox.ItemsSource = agents;
pageCount = (int)Math.Ceiling(filteredAgents.Count / (double)pageSize);
}
private void PrevPage_Click(object sender, RoutedEventArgs e)
{
if (currentPage > 1)
{
currentPage--;
UpdateDisplay();
}
}
private void NextPage_Click(object sender, RoutedEventArgs e)
{
if (currentPage < pageCount)
{
currentPage++;
UpdateDisplay();
}
}
private void AddNew_Agent(object sender, RoutedEventArgs e)
{
new AddAgent_Window().ShowDialog(this);
}
private void AgentListBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (AgentListBox.SelectedItem is AgentPresenter selectedAgent)
{
var editWindow = new EditWindow(selectedAgent);
editWindow.ShowDialog(this);
}
}
private void HistoryRealisation(object? sender, RoutedEventArgs e)
{
new HistoryWindow().ShowDialog(this);
}
}
}