84 lines
3.3 KiB
C#
84 lines
3.3 KiB
C#
|
using Avalonia;
|
|||
|
using Avalonia.Controls;
|
|||
|
using Avalonia.Markup.Xaml;
|
|||
|
using demka2025_sedelnikov.Context;
|
|||
|
using MsBox.Avalonia;
|
|||
|
using System;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
|
|||
|
namespace demka2025_sedelnikov;
|
|||
|
|
|||
|
public partial class AddEditProductWindow : Window
|
|||
|
{
|
|||
|
Product currentProduct = new Product();
|
|||
|
User8Context context = new User8Context();
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
public AddEditProductWindow()
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
DataContext = currentProduct;
|
|||
|
ComboPartnerTypes.ItemsSource = context.Producttypes.Select(pt => pt.ProductTypeName).ToList();
|
|||
|
ArticulTX.IsEnabled = false;
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
public AddEditProductWindow(Product currentProduct)
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
this.currentProduct = currentProduct;
|
|||
|
DataContext = currentProduct;
|
|||
|
ArticulTX.IsEnabled = false;
|
|||
|
ComboPartnerTypes.ItemsSource = context.Producttypes.Select(pt => pt.ProductTypeName).ToList();
|
|||
|
ComboPartnerTypes.SelectedValue = context.Producttypes.FirstOrDefault(p => p.Id == currentProduct.TypeId);
|
|||
|
ComboPartnerTypes.SelectedItem = context.Producttypes.FirstOrDefault(p => p.Id == currentProduct.TypeId).ProductTypeName;
|
|||
|
ComboPartnerTypes.SelectedIndex = context.Producttypes.FirstOrDefault(p => p.Id == currentProduct.TypeId).Id - 1;
|
|||
|
}
|
|||
|
|
|||
|
private async void Button_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
StringBuilder errors = new StringBuilder();
|
|||
|
if (!decimal.TryParse(MinCOstTX.Text, out decimal res) && res > 0)
|
|||
|
errors.AppendLine("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!");
|
|||
|
if (string.IsNullOrEmpty(MinCOstTX.Text) || string.IsNullOrEmpty(NameTX.Text) ||
|
|||
|
ComboPartnerTypes.SelectedIndex == -1)
|
|||
|
errors.AppendLine("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!");
|
|||
|
|
|||
|
if (errors.Length > 0)
|
|||
|
{
|
|||
|
await MessageBoxManager.GetMessageBoxStandard("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!", $"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: {errors.ToString()}").ShowWindowDialogAsync(this);
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
if (currentProduct.Id == 0)
|
|||
|
{
|
|||
|
currentProduct.Articul = new Random().Next(1, 100000).ToString();
|
|||
|
context.Products.Add(currentProduct);
|
|||
|
context.SaveChanges();
|
|||
|
await MessageBoxManager.GetMessageBoxStandard("OK!", $"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!").ShowWindowDialogAsync(this);
|
|||
|
Close();
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
var edittedProduct = context.Products.FirstOrDefault(p => p.Id == currentProduct.Id);
|
|||
|
if (edittedProduct != null)
|
|||
|
{
|
|||
|
edittedProduct.ProductName = currentProduct.ProductName;
|
|||
|
edittedProduct.TypeId = ComboPartnerTypes.SelectedIndex + 1;
|
|||
|
edittedProduct.MinCost = currentProduct.MinCost;
|
|||
|
}
|
|||
|
|
|||
|
context.SaveChanges();
|
|||
|
await MessageBoxManager.GetMessageBoxStandard("OK!", $"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!").ShowWindowDialogAsync(this);
|
|||
|
Close();
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
await MessageBoxManager.GetMessageBoxStandard("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!", $"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: {ex.Message}!").ShowWindowDialogAsync(this);
|
|||
|
return;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|