demka/demofinish/MainWindow.axaml.cs
2025-03-26 12:39:33 +03:00

159 lines
4.3 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();
ComboboxPriority();
PriorityCombobox.SelectionChanged += PriorutyCombobox_SelectionChanged;
}
public class AgentPresenter : Agent
{
Bitmap? LogoImage
{
get
{
try
{
var absolutepass = Path.Combine(AppContext.BaseDirectory, Logo);
return new Bitmap(absolutepass);
}
catch
{
return null;
}
}
}
}
private void ComboboxPriority()
{
using var context = new User1Context();
var priorities = context.Agents
.Select(x => x.Priority)
.Distinct()
.OrderBy(x => x)
.ToList()
.Cast<object>()
.ToList();
priorities.Insert(0, "Все типы");
PriorityCombobox.ItemsSource = priorities;
PriorityCombobox.SelectedIndex = 0;
}
private void PriorutyCombobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
currentPage = 1;
ShowCurrentPage();
if (PriorityCombobox.SelectedItem == null)
return;
if (PriorityCombobox.SelectedItem is string && (string)PriorityCombobox.SelectedItem == "Все типы")
{
AgentListBox.ItemsSource = agents;
}
else if (PriorityCombobox.SelectedItem is int priority)
{
AgentListBox.ItemsSource = agents.Where(x => x.Priority == priority).ToList();
}
}
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);
}
}
private void NextPage()
{
if (currentPage < pageCount)
{
currentPage++;
ShowCurrentPage();
}
}
private void PrevPage()
{
if (currentPage > 1)
{
currentPage--;
ShowCurrentPage();
}
}
private void PrevPage_Click(object sender, RoutedEventArgs e) => PrevPage();
private void NextPage_Click(object sender, RoutedEventArgs e) => NextPage();
}
}