DemoService/ServiceWindow.axaml.cs

154 lines
5.3 KiB
C#
Raw Permalink Normal View History

2024-09-04 09:08:53 +00:00
using Avalonia;
using Avalonia.Controls;
2024-09-05 13:11:39 +00:00
using Avalonia.Interactivity;
2024-09-09 11:45:33 +00:00
using Avalonia.Input;
2024-09-04 09:08:53 +00:00
using Avalonia.Markup.Xaml;
2024-09-05 13:11:39 +00:00
using DemoService.Models;
2024-09-06 14:13:21 +00:00
using System.Collections.Generic;
using System.Linq;
2024-09-09 11:45:33 +00:00
using Avalonia.Media.TextFormatting;
2024-09-09 13:26:07 +00:00
using Avalonia.Media.Imaging;
using System;
2024-09-10 13:12:58 +00:00
using DemoService.Context;
2024-09-04 09:08:53 +00:00
namespace DemoService;
public partial class ServiceWindow : Window
{
2024-09-09 13:26:07 +00:00
private bool AdminMode;
2024-09-06 14:13:21 +00:00
private List<Service> displayList = Utils.Context.Services;
2024-09-04 09:08:53 +00:00
public ServiceWindow()
{
InitializeComponent();
2024-09-06 14:13:21 +00:00
ServicesListBox.ItemsSource = displayList;
2024-09-09 11:45:33 +00:00
UserServicesListBox.ItemsSource = displayList;
ServicesListBox.Background = Colors.lightYellow;
UserServicesListBox.Background = Colors.lightYellow;
2024-09-09 06:46:08 +00:00
SortComboBox.SelectedIndex = 0;
2024-09-09 11:45:33 +00:00
FilterComboBox.SelectedIndex = 0;
2024-09-09 13:26:07 +00:00
Icon = new WindowIcon(new Bitmap(AppDomain.CurrentDomain.BaseDirectory + "/Assets/service_logo.ico"));
2024-09-04 09:08:53 +00:00
}
2024-09-09 13:26:07 +00:00
public ServiceWindow(bool AdminMode)
2024-09-04 09:08:53 +00:00
{
InitializeComponent();
2024-09-09 13:26:07 +00:00
this.AdminMode = AdminMode;
switch (this.AdminMode)
2024-09-05 13:11:39 +00:00
{
2024-09-09 11:45:33 +00:00
case true:
ServicesListBox.ItemsSource = displayList;
UserServicesListBox.IsVisible = false;
ServicesListBox.Background = Colors.lightYellow;
break;
case false:
AddButton.IsVisible = false;
UserServicesListBox.ItemsSource = displayList;
ServicesListBox.IsVisible = false;
UserServicesListBox.Background = Colors.lightYellow;
break;
2024-09-05 13:11:39 +00:00
}
2024-09-09 06:46:08 +00:00
SortComboBox.SelectedIndex = 0;
2024-09-09 11:45:33 +00:00
FilterComboBox.SelectedIndex = 0;
2024-09-09 13:26:07 +00:00
Icon = new WindowIcon(new Bitmap(AppDomain.CurrentDomain.BaseDirectory + "/Assets/service_logo.ico"));
2024-09-05 13:11:39 +00:00
}
public void AddButtonClick(object sender, RoutedEventArgs args)
{
2024-09-09 13:26:07 +00:00
AddEditWindow addEditWindow = new(AdminMode);
2024-09-05 13:11:39 +00:00
addEditWindow.Show();
Close();
}
2024-09-09 06:46:08 +00:00
/*private decimal? CostAfterDiscount(decimal previousCost, decimal? discount)
2024-09-05 13:11:39 +00:00
{
if (discount.HasValue || discount != 0)
{
decimal actualCost = previousCost - previousCost * discount.Value;
return actualCost;
}
return null;
2024-09-09 06:46:08 +00:00
}*/
2024-09-09 11:45:33 +00:00
private void AddButton_Click(object? sender, RoutedEventArgs e)
2024-09-06 14:13:21 +00:00
{
2024-09-10 13:12:58 +00:00
AddEditWindow addEditWindow = new(AdminMode);
2024-09-06 14:13:21 +00:00
addEditWindow.Show();
Close();
}
2024-09-09 11:45:33 +00:00
private void SortComboBox_SelectionChanged(object? sender, SelectionChangedEventArgs e)
{
DisplayService();
}
private void SearchTextBox_KeyUp(object? sender, KeyEventArgs e)
{
DisplayService();
}
private void FilterComboBox_SelectionChanged(object? sender, SelectionChangedEventArgs e)
2024-09-06 14:13:21 +00:00
{
DisplayService();
}
private void DisplayService()
{
2024-09-09 11:45:33 +00:00
displayList = Utils.Context.Services;
2024-09-09 06:46:08 +00:00
switch (SortComboBox.SelectedIndex)
2024-09-06 14:13:21 +00:00
{
2024-09-09 06:46:08 +00:00
case 0:
break;
case 1:
2024-09-06 14:13:21 +00:00
displayList = displayList.OrderBy(service => service.Cost).ToList();
break;
2024-09-09 06:46:08 +00:00
case 2:
2024-09-06 14:13:21 +00:00
displayList = displayList.OrderByDescending(service => service.Cost).ToList();
break;
}
2024-09-09 11:45:33 +00:00
switch (FilterComboBox.SelectedIndex)
{
case 0:
break;
case 1:
displayList = displayList.Where(x => x.Discount >= 0 && x.Discount < (decimal)0.05).ToList();
break;
case 2:
displayList = displayList.Where(x => x.Discount >= (decimal)0.05 && x.Discount < (decimal)0.15).ToList();
break;
case 3:
displayList = displayList.Where(x => x.Discount >= (decimal)0.15 && x.Discount < (decimal)0.30).ToList();
break;
case 4:
displayList = displayList.Where(x => x.Discount >= (decimal)0.30 && x.Discount < (decimal)0.70).ToList();
break;
case 5:
displayList = displayList.Where(x => x.Discount >= (decimal)0.70 && x.Discount < (decimal)1.00).ToList();
break;
}
if (!string.IsNullOrEmpty(SearchTextBox.Text))
{
displayList = displayList.Where(x => x.Title.ToLower().Contains(SearchTextBox.Text.ToLower())).ToList();
}
2024-09-09 06:46:08 +00:00
ServicesListBox.ItemsSource = displayList;
2024-09-06 14:13:21 +00:00
}
2024-09-10 13:12:58 +00:00
private void DeleteButton_Click(object? sender, RoutedEventArgs e)
{
using (var context = new OvsyannikovContext())
{
var service = context.Services.Find((int)(sender as Button).Tag);
context.Remove(service);
context.SaveChanges();
}
Utils.Context.Services = new List<Service>(Utils.Context.DbContext.Services);
displayList = Utils.Context.Services;
switch (AdminMode)
{
case true:
ServicesListBox.ItemsSource = displayList;
break;
case false:
UserServicesListBox.ItemsSource = displayList;
break;
}
}
private void EditButton_Click(object? sender, RoutedEventArgs e)
{
AddEditWindow addEditWindow = new(AdminMode, (int)(sender as Button).Tag);
addEditWindow.Show();
Close();
}
2024-09-04 09:08:53 +00:00
}