repo2403/AddEditPartnerWindow.axaml.cs

98 lines
3.9 KiB
C#
Raw Normal View History

2025-03-24 11:59:01 +00:00
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 AddEditPartnerWindow : Window
{
Partner currentPartner = new Partner();
User8Context context = new User8Context();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public AddEditPartnerWindow()
{
InitializeComponent();
SellButton.IsEnabled = false;
DataContext = currentPartner;
ComboPartnerTypes.ItemsSource = context.Partnertypes.Select(pt => pt.PartnerTypeName).ToList();
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> (<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>)
public AddEditPartnerWindow(Partner partner)
{
InitializeComponent();
currentPartner = partner;
DataContext = currentPartner;
ComboPartnerTypes.ItemsSource = context.Partnertypes.Select(pt => pt.PartnerTypeName).ToList();
ComboPartnerTypes.SelectedValue = context.Partnertypes.FirstOrDefault(p => p.Id == currentPartner.PartnerTypeId);
ComboPartnerTypes.SelectedItem = context.Partnertypes.FirstOrDefault(p => p.Id == currentPartner.PartnerTypeId).PartnerTypeName;
ComboPartnerTypes.SelectedIndex = context.Partnertypes.FirstOrDefault(p => p.Id == currentPartner.PartnerTypeId).Id - 1;
}
private async void Button_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
try
{
StringBuilder errors = new StringBuilder();
if (!decimal.TryParse(RatingTX.Text, out decimal res) && res > 0)
errors.AppendLine("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!");
if (string.IsNullOrEmpty(RatingTX.Text) || string.IsNullOrEmpty(NameTX.Text) ||
string.IsNullOrEmpty(DirectorNameTX.Text) || string.IsNullOrEmpty(PhoneTX.Text) ||
string.IsNullOrEmpty(EmailTX.Text) || string.IsNullOrEmpty(AddressTX.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;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (currentPartner.Id == 0)
{
context.Partners.Add(currentPartner);
context.SaveChanges();
await MessageBoxManager.GetMessageBoxStandard("OK!", $"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!").ShowWindowDialogAsync(this);
Close();
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
else
{
var edittedPartner = context.Partners.FirstOrDefault(p => p.Id == currentPartner.Id);
if (edittedPartner != null)
{
edittedPartner.PartnerName = currentPartner.PartnerName;
edittedPartner.PartnerTypeId = ComboPartnerTypes.SelectedIndex + 1;
edittedPartner.Rating = currentPartner.Rating;
edittedPartner.DirectorName = currentPartner.DirectorName;
edittedPartner.Phone = currentPartner.Phone;
edittedPartner.Email = currentPartner.Email;
edittedPartner.Address = currentPartner.Address;
}
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;
}
}
private async void Button_Click_1(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
await new PartnerProductHistoryWindow(currentPartner.Id).ShowDialog(this);
return;
}
}