ServicePage.xaml.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. namespace AutoservisDrive.Pages
  16. {
  17. /// <summary>
  18. /// Логика взаимодействия для ServicePage.xaml
  19. /// </summary>
  20. public partial class ServicePage : Page
  21. {
  22. public ServicePage()
  23. {
  24. InitializeComponent();
  25. if (App.CurrentUser.RoleId ==1)
  26. {
  27. BtnAddService.Visibility = Visibility.Visible;
  28. }
  29. else
  30. {
  31. BtnAddService.Visibility= Visibility.Collapsed;
  32. }
  33. ComboDiscount.SelectedIndex = 0;
  34. ComboSortBy.SelectedIndex = 0;
  35. UpdateServices();
  36. LViewServices.ItemsSource = App.Context.Services.ToList();
  37. }
  38. private void BtnAddService_Click(object sender, RoutedEventArgs e)
  39. {
  40. NavigationService.Navigate(new AddEditPage());
  41. }
  42. private void BtnEdit_Click(object sender, RoutedEventArgs e)
  43. {
  44. var currentService = (sender as Button).DataContext as Entities.Service;
  45. NavigationService.Navigate(new AddEditPage(currentService));
  46. }
  47. private void BtnDelete_Click(object sender, RoutedEventArgs e)
  48. {
  49. var currentService = (sender as Button).DataContext as Entities.Service;
  50. if (MessageBox.Show($"Вы уверены, что хотите удалить услугу: " + $"{currentService.Title}?", "Внимание", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
  51. {
  52. App.Context.Services.Remove(currentService);
  53. App.Context.SaveChanges();
  54. UpdateServices();
  55. }
  56. }
  57. private void ComboDiscount_SelectionChanged(object sender, SelectionChangedEventArgs e)
  58. {
  59. UpdateServices();
  60. }
  61. private void ComboSortBy_SelectionChanged(object sender, SelectionChangedEventArgs e)
  62. {
  63. UpdateServices();
  64. }
  65. private void TBoxSearch_TextChanged(object sender, TextChangedEventArgs e)
  66. {
  67. UpdateServices();
  68. }
  69. private void UpdateServices()
  70. {
  71. var services = App.Context.Services.ToList();
  72. //Сортировка по цене
  73. if (ComboSortBy.SelectedIndex == 0)
  74. services = services.OrderBy(p => p.CostWithDiscount).ToList();
  75. else
  76. services = services.OrderByDescending(p => p.CostWithDiscount).ToList();
  77. //Фильтрация по размеру скидки
  78. if (ComboDiscount.SelectedIndex == 1)
  79. services = services.Where(p => p.Discount >= 0 && p.Discount < 0.05).ToList();
  80. if (ComboDiscount.SelectedIndex == 2)
  81. services = services.Where(p => p.Discount >= 0.05 && p.Discount < 0.15).ToList();
  82. if (ComboDiscount.SelectedIndex == 3)
  83. services = services.Where(p => p.Discount >= 0.15 && p.Discount < 0.3).ToList();
  84. if (ComboDiscount.SelectedIndex == 4)
  85. services = services.Where(p => p.Discount >= 0.3 && p.Discount < 0.7).ToList();
  86. if (ComboDiscount.SelectedIndex == 5)
  87. services = services.Where(p => p.Discount >= 0.7 && p.Discount < 1).ToList();
  88. //Поиск по названию
  89. services = services.Where(p => p.Title.ToLower().Contains(TBoxSearch.Text.ToLower())).ToList();
  90. LViewServices.ItemsSource = services;
  91. }
  92. private void Page_Loaded(object sender, RoutedEventArgs e)
  93. {
  94. UpdateServices();
  95. }
  96. }
  97. }