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; } }