using Avalonia.Controls; using Avalonia.Interactivity; using Demka_Snova_1.Hardik.Conect; using Demka_Snova_1.Hardik.Conect.Dao; using Demka_Snova_1.OknaRoley; using iText.Kernel.Pdf; using iText.Layout; using iText.Layout.Element; using iText.Kernel.Font; using System; using System.Collections.Generic; using System.IO; using System.Linq; using iText.IO.Font; namespace Demka_Snova_1; public partial class AddOtchotWindow : Window { private List ordersList; private Random random; private int nextId = 1; public AddOtchotWindow() { InitializeComponent(); random = new Random(); LoadOrders(); } private void LoadOrders() { using (var db = new DatabaseConnection()) { ordersList = db.GetAllOrders(); nextId = ordersList.Count > 0 ? ordersList.Max(o => o.ID) + 1 : 1; } } private void FormatOtchot_Click(object sender, RoutedEventArgs e) { string client = this.FindControl("ClientTextBox").Text; string usluga = this.FindControl("UslugaTextBox").Text; string prokatText = this.FindControl("ProkatTextBox").Text; if (!decimal.TryParse(prokatText, out decimal prokat)) { ShowError("Некорректное значение для проката."); return; } string codeZakaz = $"{random.Next(10000, 99999)}.{random.Next(10000, 99999)}"; ordersDao order = new ordersDao { ID = nextId++, CodeZakaz = codeZakaz, Date = DateOnly.FromDateTime(DateTime.Now), Time = TimeOnly.FromDateTime(DateTime.Now), CodeClient = client, Usluga = usluga, Status = "Новый", DateClose = null, Prokat = prokat }; SaveOrderToPdf(order); ordersList.Add(order); ClearFields(); ShowError("Заказ успешно создан и сохранен в PDF!"); } private void SaveOrderToPdf(ordersDao order) { string directoryPath = "C:/Users/PC/source/Dopolnenia/Fails/Doky"; string pdfPath = Path.Combine(directoryPath, $"Order_{order.CodeZakaz}.pdf"); Directory.CreateDirectory(directoryPath); using (PdfWriter writer = new PdfWriter(pdfPath)) using (PdfDocument pdf = new PdfDocument(writer)) { Document document = new Document(pdf); PdfFont font = PdfFontFactory.CreateFont("C:/Windows/Fonts/arial.ttf", PdfEncodings.IDENTITY_H); document.Add(new Paragraph($"Отчет о заказе от {order.Date}") .SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER) .SetFontSize(20) .SetFont(font)); document.Add(new Paragraph($"Код заказа: {order.CodeZakaz}").SetFont(font)); document.Add(new Paragraph($"Код клиента: {order.CodeClient}").SetFont(font)); document.Add(new Paragraph($"Услуга: {order.Usluga}").SetFont(font)); document.Add(new Paragraph($"Прокат: {order.Prokat}").SetFont(font)); document.Add(new Paragraph($"Дата: {order.Date}").SetFont(font)); document.Add(new Paragraph($"Время: {order.Time}").SetFont(font)); document.Add(new Paragraph($"Статус: {order.Status}").SetFont(font)); document.Close(); } } private void ClearFields() { this.FindControl("ClientTextBox").Text = string.Empty; this.FindControl("UslugaTextBox").Text = string.Empty; this.FindControl("ProkatTextBox").Text = string.Empty; } private void Exitka(object sender, RoutedEventArgs e) { new AdminWindow().Show(); this.Close(); } private async void ShowError(string message) { var dialog = new Window { Title = "Ошибка", Content = message, Width = 300, Height = 200 }; await dialog.ShowDialog(this); } }