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; using iText.IO.Font.Constants; 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); ObnuleniePoley(); ShowError("Заказ успешно создан!"); } //private void SaveOrderToPdf(ordersDao order) // Sohronenie v pdf //{ // string directoryPath = "C:/Users/Public/Documents/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; // try // { // font = PdfFontFactory.CreateFont("C:/Windows/Fonts/arial.ttf", PdfEncodings.IDENTITY_H); // } // catch // { // font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA); // } // document.Add(new Paragraph($"Отчет о заказе от {order.Date}") // .SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER) // .SetFontSize(20) // .SetFont(font)); // var fields = new Dictionary // { // {"Код заказа", order.CodeZakaz}, // {"Код клиента", order.CodeClient}, // {"Услуга", order.Usluga}, // {"Прокат", order.Prokat.ToString()}, // {"Дата", order.Date.ToString()}, // {"Время", order.Time.ToString()}, // {"Статус", order.Status} // }; // foreach (var field in fields) // { // document.Add(new Paragraph($"{field.Key}: {field.Value}").SetFont(font)); // } // document.Close(); // } // } private void ObnuleniePoley() { 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); } }