Blagodat/OknaFunciy/AddOtchotWindow.axaml.cs
2025-05-07 14:41:56 +03:00

148 lines
4.5 KiB
C#

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<ordersDao> 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<TextBox>("ClientTextBox").Text;
string usluga = this.FindControl<TextBox>("UslugaTextBox").Text;
string prokatText = this.FindControl<TextBox>("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<string, string>
// {
// {"Êîä çàêàçà", 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<TextBox>("ClientTextBox").Text = string.Empty;
this.FindControl<TextBox>("UslugaTextBox").Text = string.Empty;
this.FindControl<TextBox>("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);
}
}