using Avalonia.Controls; using Avalonia.Interactivity; using Demka_Snova_1.Hardik.Conect.Dao; using Demka_Snova_1.OknaRoley; using System; using System.Collections.Generic; using System.IO; using iText.Kernel.Pdf; using iText.Layout.Element; using Document = iText.Layout.Document; using iText.Kernel.Font; 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(); ordersList = new List(); } private void TestOrders() { ordersList = new List { new ordersDao { ID = 1, CodeZakaz = "Иванов Иван", Date = DateOnly.FromDayNumber(8), Time = TimeOnly.FromDateTime(DateTime.Now), CodeClient = "123", Usluga = "2023-10-01", Status = "Admin", DateClose = null, Prokat = 23 } }; } 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)}"; int id = nextId++; DateOnly date = DateOnly.FromDateTime(DateTime.Now); TimeOnly time = TimeOnly.FromDateTime(DateTime.Now); ordersDao Order = new ordersDao { ID = id, CodeZakaz = codeZakaz, Date = date, Time = time, CodeClient = client, Usluga = usluga, Status = "Новый", DateClose = null, Prokat = prokat }; ordersList.Add(Order); SaveOrderToPdf(Order); this.FindControl("ClientTextBox").Text = string.Empty; this.FindControl("UslugaTextBox").Text = string.Empty; this.FindControl("ProkatTextBox").Text = string.Empty; 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"); if (!Directory.Exists(directoryPath)) { 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($"Отчет о заказе от {DateOnly.FromDateTime(DateTime.Now)}") .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 Exitka(object sender, RoutedEventArgs e) { var login = new AdminWindow(); login.Show(); this.Close(); } async void ShowError(string mes) { var dialog = new Window { Title = "Ошибка", Content = mes, Width = 300, Height = 200 }; await dialog.ShowDialog(this); } }