demka/demofinish/MainWindow.axaml.cs
2025-03-31 21:03:22 +03:00

231 lines
6.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.Imaging;
using demofinish.Models;
namespace demofinish
{
public partial class MainWindow : Window
{
private ObservableCollection<Agent> agents = new ObservableCollection<Agent>();
public List<AgentPresenter> agentsList = new List<AgentPresenter>();
private const int pageSize = 10;
private int currentPage = 1;
private int pageCount = 0;
public MainWindow()
{
InitializeComponent();
LoadAgents();
ComboboxAgentType();
TypeAgentCombobox.SelectionChanged += TypeAgentCombobox_SelectionChanged;
NameComboBox.SelectionChanged += SortName_SelectedChanged;
PriorityCombobox.SelectionChanged += PrioritySort_SelectedChanged;
}
public class AgentPresenter : Agent
{
Bitmap? LogoImage
{
get
{
try
{
string absolutepass;
absolutepass = Path.Combine(AppContext.BaseDirectory, Logo);
return new Bitmap(absolutepass);
}
catch
{
return null;
}
}
}
}
private void ComboboxAgentType()
{
using var context = new User1Context();
var type = context.Agenttypes.Select(x => x.Title).ToList();
type.Insert(0, "Все типы");
TypeAgentCombobox.ItemsSource = type;
TypeAgentCombobox.SelectedIndex = 0;
}
private void TypeAgentCombobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
using var context = new User1Context();
string selectedType = TypeAgentCombobox.SelectedItem.ToString();
if (TypeAgentCombobox.SelectedItem == null)
return;
if (TypeAgentCombobox.SelectedItem is string && (string)TypeAgentCombobox.SelectedItem == "Все типы")
{
AgentListBox.ItemsSource = agents;
}
else
{
var selectedTypeId = context.Agenttypes
.Where(at => at.Title == selectedType)
.Select(at => at.Id)
.FirstOrDefault();
var filteredAgents = agentsList
.Where(x => x.Agenttypeid == selectedTypeId)
.ToList();
AgentListBox.ItemsSource = filteredAgents;
}
ShowCurrentPage();
}
private void SortName_SelectedChanged(object sender, SelectionChangedEventArgs e)
{
if (NameComboBox.SelectedItem == null)
return;
var currentPageItems = agents.ToList();
List<Agent> sorted;
if (NameComboBox.SelectedItem.ToString() == "по возрастанию")
{
sorted = currentPageItems.OrderBy(it => it.Title).ToList();
}
else
{
sorted = currentPageItems.OrderByDescending(it => it.Title).ToList();
}
agents.Clear();
foreach (var item in sorted)
{
agents.Add(item);
}
}
private void PrioritySort_SelectedChanged(object sender, SelectionChangedEventArgs e)
{
if (PriorityCombobox.SelectedItem == null)
return;
var currentPageItems = agents.ToList();
List<Agent> sorted;
if (PriorityCombobox.SelectedItem.ToString() == "по возрастанию")
{
sorted = currentPageItems.OrderBy(it => it.Priority).ToList();
}
else
{
sorted = currentPageItems.OrderByDescending(it => it.Priority).ToList();
}
agents.Clear();
foreach (var item in sorted)
{
agents.Add(item);
}
}
private void LoadAgents()
{
using var context = new User1Context();
agentsList = context.Agents.Select(agentPresenter => new AgentPresenter
{
Id = agentPresenter.Id,
Title = agentPresenter.Title,
Agenttypeid = agentPresenter.Agenttypeid,
Address = agentPresenter.Address,
Inn = agentPresenter.Inn,
Kpp = agentPresenter.Kpp,
Directorname = agentPresenter.Directorname,
Phone = agentPresenter.Phone,
Email = agentPresenter.Email,
Logo = agentPresenter.Logo,
Priority = agentPresenter.Priority,
}).ToList();
foreach (var agent in agentsList)
{
agents.Add(agent);
}
AgentListBox.ItemsSource = agents;
pageCount = (int)Math.Ceiling(agentsList.Count / (double) pageSize);
ShowCurrentPage();
}
private void ShowCurrentPage()
{
agents.Clear();
var pageAgents = agentsList
.Skip((currentPage - 1) * pageSize)
.Take(pageSize);
foreach (var e in pageAgents)
{
agents.Add(e);
}
pageCount = (int)Math.Ceiling(agentsList.Count / (double)pageSize);
}
private void NextPage()
{
if (currentPage < pageCount)
{
currentPage++;
ShowCurrentPage();
}
}
private void PrevPage()
{
if (currentPage > 1)
{
currentPage--;
ShowCurrentPage();
}
}
private void
private void PrevPage_Click(object sender, RoutedEventArgs e) => PrevPage();
private void NextPage_Click(object sender, RoutedEventArgs e) => NextPage();
}
}