145 lines
4.9 KiB
C#
145 lines
4.9 KiB
C#
using Avalonia.Controls;
|
||
using Avalonia.Interactivity;
|
||
using Avalonia.Media.Imaging;
|
||
using Demka_2.Use;
|
||
using System;
|
||
using System.Collections.ObjectModel;
|
||
using System.Linq;
|
||
|
||
namespace Demka_2.Views
|
||
{
|
||
public partial class ServiceFormWindow : Window
|
||
{
|
||
private Service _service;
|
||
private ObservableCollection<Service> _services;
|
||
|
||
public ServiceFormWindow(Service service, ObservableCollection<Service> services)
|
||
{
|
||
InitializeComponent();
|
||
_service = service;
|
||
_services = services;
|
||
|
||
LoadServiceData();
|
||
|
||
SaveButton.Click += SaveButton_Click;
|
||
SelectImageButton.Click += SelectImageButton_Click;
|
||
|
||
|
||
/*
|
||
* AddServiceButton.Click += (s, e) =>
|
||
{
|
||
var serviceForm = new ServiceFormWindow(new Service(), Services);
|
||
serviceForm.ShowDialog(this).ContinueWith(_ => UpdateFilteredServices());
|
||
};
|
||
|
||
EditServiceButton.Click += (s, e) =>
|
||
{
|
||
if (_serviceList.SelectedItem is Service selectedService)
|
||
{
|
||
var serviceForm = new ServiceFormWindow(selectedService, Services);
|
||
serviceForm.ShowDialog(this).ContinueWith(_ => UpdateFilteredServices());
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Выберите услугу для редактирования.");
|
||
}
|
||
};
|
||
*/
|
||
}
|
||
|
||
private void LoadServiceData()
|
||
{
|
||
NameTextBox.Text = _service.Name;
|
||
PriceTextBox.Text = _service.Price.ToString();
|
||
DiscountTextBox.Text = _service.Discount.ToString();
|
||
DurationTextBox.Text = _service.Duration.ToString();
|
||
DescriptionTextBox.Text = _service.Description;
|
||
if (!string.IsNullOrEmpty(_service.ImagePath))
|
||
{
|
||
ThumbnailImage.Source = new Bitmap(_service.ImagePath);
|
||
}
|
||
}
|
||
private async void ShowMessage(string message)
|
||
{
|
||
var messageBox = new Window
|
||
{
|
||
Title = "Сообщение",
|
||
Content = message,
|
||
Width = 300,
|
||
Height = 150,
|
||
WindowStartupLocation = WindowStartupLocation.CenterScreen
|
||
};
|
||
|
||
var okButton = new Button { Content = "ОК" };
|
||
okButton.Click += (s, e) => messageBox.Close();
|
||
messageBox.Content = new StackPanel
|
||
{
|
||
Children = { new TextBlock { Text = message }, okButton }
|
||
};
|
||
|
||
await messageBox.ShowDialog(this);
|
||
}
|
||
private void SaveButton_Click(object? sender, RoutedEventArgs e)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(NameTextBox.Text))
|
||
{
|
||
ShowMessage("Название услуги не может быть пустым.");
|
||
return;
|
||
}
|
||
|
||
if (_services.Any(s => s.Name == NameTextBox.Text && s.guid != _service.guid))
|
||
{
|
||
ShowMessage("Услуга с таким названием уже существует.");
|
||
return;
|
||
}
|
||
|
||
if (!decimal.TryParse(PriceTextBox.Text, out var price) || price < 0)
|
||
{
|
||
ShowMessage("Введите корректную стоимость.");
|
||
return;
|
||
}
|
||
|
||
if (!decimal.TryParse(DiscountTextBox.Text, out var discount) || discount < 0 || discount > 100)
|
||
{
|
||
ShowMessage("Введите корректную скидку (0-100%).");
|
||
return;
|
||
}
|
||
|
||
if (!int.TryParse(DurationTextBox.Text, out var duration) || duration <= 0 || duration > 240)
|
||
{
|
||
ShowMessage("Введите корректную длительность (1-240 минут).");
|
||
return;
|
||
}
|
||
|
||
_service.Name = NameTextBox.Text;
|
||
_service.Price = price;
|
||
_service.Discount = discount;
|
||
_service.Duration = duration;
|
||
_service.Description = DescriptionTextBox.Text;
|
||
|
||
if (_service.guid == Guid.Empty)
|
||
{
|
||
_service.guid = Guid.NewGuid();
|
||
_services.Add(_service);
|
||
}
|
||
|
||
this.Close();
|
||
}
|
||
|
||
private async void SelectImageButton_Click(object? sender, RoutedEventArgs e)
|
||
{
|
||
var openFileDialog = new OpenFileDialog
|
||
{
|
||
Filters = { new FileDialogFilter { Name = "Images", Extensions = { "jpg", "png" } } }
|
||
};
|
||
|
||
var result = await openFileDialog.ShowAsync(this);
|
||
if (result?.Length > 0)
|
||
{
|
||
var imagePath = result[0];
|
||
_service.ImagePath = imagePath;
|
||
ThumbnailImage.Source = new Bitmap(imagePath);
|
||
}
|
||
}
|
||
}
|
||
} |