98 lines
3.9 KiB
C#
98 lines
3.9 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 AddEditPartnerWindow : Window
|
|
{
|
|
Partner currentPartner = new Partner();
|
|
User8Context context = new User8Context();
|
|
// äîáàâëåíèå
|
|
public AddEditPartnerWindow()
|
|
{
|
|
InitializeComponent();
|
|
SellButton.IsEnabled = false;
|
|
DataContext = currentPartner;
|
|
ComboPartnerTypes.ItemsSource = context.Partnertypes.Select(pt => pt.PartnerTypeName).ToList();
|
|
}
|
|
|
|
// ðåäàêòèðîâàíèå (çàïîëíåíèå ïîëåé)
|
|
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("Ðåéòèíã íå ìîæåò áûòü îòðèöàòåëüíûì ÷èñëîì!");
|
|
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("Äàííûå íå ìîãóò áûòü ïóñòûìè!");
|
|
|
|
if (errors.Length > 0)
|
|
{
|
|
await MessageBoxManager.GetMessageBoxStandard("Îøèáêà!", $"Ïðîèçîøëà îøèáêà: {errors.ToString()}").ShowWindowDialogAsync(this);
|
|
return;
|
|
}
|
|
|
|
//äîáàâëÿåì
|
|
if (currentPartner.Id == 0)
|
|
{
|
|
context.Partners.Add(currentPartner);
|
|
context.SaveChanges();
|
|
await MessageBoxManager.GetMessageBoxStandard("OK!", $"Ïàðòí¸ð áûë äîáàâëåí!").ShowWindowDialogAsync(this);
|
|
Close();
|
|
}
|
|
|
|
//ðåäàêòèðóåì
|
|
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!", $"Ïàðòí¸ð áûë èçìåí¸í!").ShowWindowDialogAsync(this);
|
|
Close();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
await MessageBoxManager.GetMessageBoxStandard("Îøèáêà!", $"Ïðîèçîøëà îøèáêà: {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;
|
|
}
|
|
} |