dmeo040225/HistoryOrdersWindow.axaml.cs
2025-03-04 15:41:58 +03:00

162 lines
5.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using dmeo040225.Models;
using dmeo040225.Services;
using PdfSharpCore.Drawing;
using PdfSharpCore.Pdf;
namespace dmeo040225;
public partial class HistoryOrdersWindow : Window
{
ObservableCollection<Order> Orders = new ObservableCollection<Order>();
List<Service> dataSourceServices;
List<OrderPresenter> dataSourceOrders;
public HistoryOrdersWindow()
{
InitializeComponent();
using var context = new DatabaseContext();
dataSourceOrders = context.Orders.Select(order => new OrderPresenter
{
Id = order.Id,
Code = order.Code,
Orderdata = order.Orderdata,
Ordertime = order.Ordertime,
UserId = order.UserId,
Status = order.Status,
Prokattime = order.Prokattime,
Services = order.OrdersServices.Select(os => os.Service).ToList(),
UserFio = context.Users.FirstOrDefault(u => u.Id == order.UserId).Fio,
ServicesNames = new ObservableCollection<string>(
order.OrdersServices.Select(os => os.Service.Name).ToList()
)
}).ToList();
dataSourceServices = context.Services.Select(order => new Service()).ToList();
ListBoxHistory.ItemsSource = Orders;
FilterComboboxUsers.ItemsSource = context.Users.Select(user => user.Fio).ToList();
FilterComboboxServices.ItemsSource = context.Services.Select(service => service.Name).ToList();
DisplayServices();
TimerService.Instance.TimeUpdated += UpdateTimerText;
TimerService.Instance.TimerExpired += LogoutUser;
}
public class OrderPresenter() : Order
{
public List<Service> Services { get; set; } = new();
public String UserFio;
public ObservableCollection<string> ServicesNames { get; set; } = new();
}
public void DisplayServices()
{
var temp = dataSourceOrders;
Orders.Clear();
if (FilterComboboxUsers.SelectionBoxItem != null)
{
temp = temp.Where(u => u.UserFio == FilterComboboxUsers.SelectionBoxItem.ToString()).ToList();
}
if (FilterComboboxServices.SelectionBoxItem != null)
{
var selectedServiceName = FilterComboboxServices.SelectionBoxItem.ToString();
temp = temp.Where(o => o.Services.Any(s => s.Name == selectedServiceName)).ToList();
}
foreach (var item in temp)
{
Orders.Add(item);
}
}
private void FilterComboboxUsers_OnSelectionChanged(object? sender, SelectionChangedEventArgs e)
{
DisplayServices();
}
private void FilterComboboxServices_OnSelectionChanged(object? sender, SelectionChangedEventArgs e)
{
DisplayServices();
}
private void Back_OnClick(object? sender, RoutedEventArgs e)
{
Close();
}
private void UpdateTimerText(TimeSpan time)
{
TimerText.Text = $"Осталось: {time:mm\\:ss}";
}
private void LogoutUser()
{
Close();
var mainWindow = new MainWindow();
mainWindow.Show();
}
protected override void OnClosed(EventArgs e)
{
TimerService.Instance.TimeUpdated -= UpdateTimerText;
TimerService.Instance.TimerExpired -= LogoutUser;
base.OnClosed(e);
}
private void FormOrder_OnClick(object? sender, RoutedEventArgs e)
{
if (ListBoxHistory.SelectedItem is OrderPresenter selectedOrder)
{
var resultSum = 0;
var serviceNames = "";
using var document = new PdfDocument();
var page = document.AddPage();
var gfx = XGraphics.FromPdfPage(page);
var font = new XFont("Arial", 14, XFontStyle.Regular);
foreach (var service in selectedOrder.Services)
{
resultSum += int.Parse(service.Cost);
serviceNames += service.Name + " ";
}
var lines = new List<string>
{
$"Дата заказа: {selectedOrder.Orderdata}",
$"ID пользователя: {selectedOrder.UserId}",
$"Код заказа: {selectedOrder.Code}",
$"ФИО пользователя: {selectedOrder.UserFio}",
$"Время заказа: {selectedOrder.Ordertime}",
$"Услуги: {serviceNames}",
$"Итоговая сумма: {resultSum}"
};
double x = 50, y = 50;
double lineHeight = 20;
foreach (var line in lines)
{
gfx.DrawString(line, font, XBrushes.Black, new XPoint(x, y));
y += lineHeight;
}
string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), $"Order_{selectedOrder.Id}.pdf");
document.Save(filePath);
Console.WriteLine($"PDF сохранен: {filePath}");
}
}
}