Demka/Demka-2/Views/MainWindow.axaml.cs

183 lines
6.8 KiB
C#
Raw Normal View History

using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using System.Collections.ObjectModel;
using System.Linq;
using Demka_2.Use;
using System;
2024-12-27 11:47:18 +00:00
using Avalonia.Interactivity;
2024-12-27 12:48:34 +00:00
using DynamicData;
namespace Demka_2.Views;
public partial class MainWindow : Window
{
private ObservableCollection<Service> Services { get; set; } = new ObservableCollection<Service>();
private ObservableCollection<Service> FilteredServices { get; set; } = new ObservableCollection<Service>();
private TextBox _searchBox;
private ComboBox _discountFilter;
private ListBox _serviceList;
private TextBlock _serviceCountText;
2024-12-27 11:47:18 +00:00
private TextBox _adminCodeBox;
private Button _adminLoginButton;
private StackPanel _adminControls;
2024-12-27 11:47:18 +00:00
private const string AdminCode = "0000";
private bool IsAdminMode = false;
public MainWindow()
{
InitializeComponent();
LoadServices();
UpdateFilteredServices();
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
_searchBox = this.FindControl<TextBox>("SearchBox");
_discountFilter = this.FindControl<ComboBox>("DiscountFilter");
_serviceList = this.FindControl<ListBox>("ServiceList");
_serviceCountText = this.FindControl<TextBlock>("ServiceCountText");
2024-12-27 11:47:18 +00:00
_adminCodeBox = this.FindControl<TextBox>("AdminCodeBox");
_adminLoginButton = this.FindControl<Button>("AdminLoginButton");
_adminControls = this.FindControl<StackPanel>("AdminControls");
_serviceList.ItemsSource = FilteredServices;
_searchBox.TextChanged += SearchBox_TextChanged;
_discountFilter.SelectionChanged += DiscountFilter_SelectionChanged;
2024-12-27 11:47:18 +00:00
_adminLoginButton.Click += AdminLoginButton_Click;
}
private void AdminLoginButton_Click(object? sender, EventArgs e)
{
if (_adminCodeBox.Text == AdminCode)
{
IsAdminMode = true;
_adminControls.IsVisible = true;
}
else
{
IsAdminMode = false;
_adminControls.IsVisible = false;
}
}
private void OpenServiceFormWindow_Click(object sender, RoutedEventArgs e)
{
var selectedService = _serviceList.SelectedItem as Service;
var serviceFormWindow = new ServiceFormWindow(selectedService, Services);
serviceFormWindow.Show();
}
private void LoadServices()
{
2024-12-27 12:48:34 +00:00
/*using (var context = new AppDbContext())
2024-12-27 08:59:46 +00:00
{
var services = context.Services.ToList();
foreach (var service in services)
{
Services.Add(service);
}
2024-12-27 09:05:01 +00:00
2024-12-27 12:48:34 +00:00
}*/
Services.Add(new Service { Name = "Услуга 1", Price = 100, Duration = 30, Discount = 0 });
Services.Add(new Service { Name = "Услуга 2", Price = 200, Duration = 60, Discount = 10 });
Services.Add(new Service { Name = "Услуга 3", Price = 150, Duration = 45, Discount = 20 });
Services.Add(new Service { Name = "Услуга 3", Price = 150, Duration = 45, Discount = 20 });
Services.Add(new Service { Name = "Услуга 3", Price = 150, Duration = 45, Discount = 20 });
2024-12-27 13:07:13 +00:00
Services.Add(new Service { Name = "Услуга 3", Price = 150, Duration = 45, Discount = 40 });
Services.Add(new Service { Name = "Услуга 3", Price = 150, Duration = 45, Discount = 60 });
Services.Add(new Service { Name = "Услуга 3", Price = 150, Duration = 45, Discount = 80 });
2024-12-27 12:48:34 +00:00
Services.Add(new Service { Name = "Услуга 3", Price = 150, Duration = 45, Discount = 20 });
Services.Add(new Service { Name = "Услуга 3", Price = 150, Duration = 45, Discount = 20 });
Services.Add(new Service { Name = "Услуга 3", Price = 150, Duration = 45, Discount = 20 });
Services.Add(new Service { Name = "Услуга 3", Price = 150, Duration = 45, Discount = 20 });
Services.Add(new Service { Name = "Услуга 3", Price = 150, Duration = 45, Discount = 20 });
Services.Add(new Service { Name = "Услуга 3", Price = 150, Duration = 45, Discount = 20 });
Services.Add(new Service { Name = "Услуга 3", Price = 150, Duration = 45, Discount = 20 });
Services.Add(new Service { Name = "Услуга 3", Price = 150, Duration = 45, Discount = 20 });
Services.Add(new Service { Name = "Услуга 3", Price = 150, Duration = 45, Discount = 20 });
Services.Add(new Service { Name = "Услуга 3", Price = 150, Duration = 45, Discount = 20 });
Services.Add(new Service { Name = "Услуга 3", Price = 150, Duration = 45, Discount = 20 });
Services.Add(new Service { Name = "Услуга 3", Price = 150, Duration = 45, Discount = 20 });
}
private void UpdateFilteredServices()
{
var query = Services.AsEnumerable();
if (!string.IsNullOrWhiteSpace(_searchBox.Text))
{
string searchQuery = _searchBox.Text.ToLower();
query = query.Where(service =>
service.Name.ToLower().Contains(searchQuery) ||
service.Description.ToLower().Contains(searchQuery));
}
if (_discountFilter.SelectedItem is TextBlock selected && selected.Text != "Все")
{
var range = ParseDiscountRange(selected.Text);
if (range != null)
{
query = query.Where(service =>
service.Discount >= range.Value.min && service.Discount < range.Value.max);
}
}
FilteredServices.Clear();
foreach (var service in query)
{
FilteredServices.Add(service);
}
_serviceCountText.Text = $"{FilteredServices.Count} из {Services.Count}";
}
private void SearchBox_TextChanged(object? sender, EventArgs e)
{
UpdateFilteredServices();
}
private void DiscountFilter_SelectionChanged(object? sender, SelectionChangedEventArgs e)
{
UpdateFilteredServices();
}
2024-12-27 13:07:13 +00:00
public async void ShowMessage(string message)
{
var messageBox = new Window
{
Title = "Сообщение",
Content = message,
Width = 300,
Height = 150,
WindowStartupLocation = WindowStartupLocation.CenterScreen
};
2024-12-27 11:47:18 +00:00
2024-12-27 13:07:13 +00:00
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);
}
2024-12-27 11:47:18 +00:00
private (decimal min, decimal max)? ParseDiscountRange(string rangeText)
{
return rangeText switch
{
"0-5%" => (0, 5),
"5-15%" => (5, 15),
"15-30%" => (15, 30),
"30-70%" => (30, 70),
"70-100%" => (70, 100),
_ => null,
};
}
}