ServicesPage.xaml.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 Practic.Pages
  16. {
  17. /// <summary>
  18. /// Логика взаимодействия для ServicesPage.xaml
  19. /// </summary>
  20. public partial class ServicesPage : Page
  21. {
  22. public ServicesPage()
  23. {
  24. InitializeComponent();
  25. LViewServices.ItemsSource = App.Context.Service.ToList();
  26. // 1 - админ, 2 - пользователь
  27. if (App.CurrentUser.Roleld == 1)
  28. {
  29. BtnAddService.Visibility = Visibility.Visible;
  30. }
  31. else
  32. {
  33. BtnAddService.Visibility = Visibility.Collapsed;
  34. }
  35. ComboDiscount.SelectedIndex = 0;
  36. ComboSortBy.SelectedIndex = 0;
  37. UpdateServices();
  38. }
  39. private void BtnAddService_Click(object sender, RoutedEventArgs e)
  40. {
  41. NavigationService.Navigate(new AddEditServicePage());
  42. }
  43. private void BtnEdit_Click(object sender, RoutedEventArgs e)
  44. {
  45. var currentSevice = (sender as Button).DataContext as Entities.Service;
  46. NavigationService.Navigate(new AddEditServicePage(currentSevice));
  47. }
  48. private void BtnDelete_Click(object sender, RoutedEventArgs e)
  49. {
  50. var currentService = (sender as Button).DataContext as Entities.Service;
  51. if (MessageBox.Show($"Вы уверены, что хотите удалить услугу:" + $"{currentService.Title}?", "Внимание!", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
  52. {
  53. App.Context.Service.Remove(currentService);
  54. App.Context.SaveChanges();
  55. UpdateServices();
  56. }
  57. }
  58. private void ComboSortBy_SelectionChanged(object sender, SelectionChangedEventArgs e)
  59. {
  60. UpdateServices();
  61. }
  62. private void ComboDiscount_SelectionChanged(object sender, SelectionChangedEventArgs e)
  63. {
  64. UpdateServices();
  65. }
  66. private void TBoxSearh_TextChanged(object sender, TextChangedEventArgs e)
  67. {
  68. UpdateServices();
  69. }
  70. private void UpdateServices()
  71. {
  72. var services = App.Context.Service.ToList();
  73. //Сортировка по цене
  74. if (ComboSortBy.SelectedIndex == 0)
  75. services = services.OrderBy(p => p.CostWithDiscount).ToList();
  76. else
  77. services = services.OrderByDescending(p => p.CostWithDiscount).ToList();
  78. // Фильтрация по рамеру скидки
  79. if (ComboDiscount.SelectedIndex == 1)
  80. services = services.Where(p => p.Discount >= 0 && p.Discount < 0.05).ToList();
  81. if (ComboDiscount.SelectedIndex == 2)
  82. services = services.Where(p => p.Discount >= 0.05 && p.Discount < 0.15).ToList();
  83. if (ComboDiscount.SelectedIndex == 3)
  84. services = services.Where(p => p.Discount >= 0.15 && p.Discount < 0.30).ToList();
  85. if (ComboDiscount.SelectedIndex == 4)
  86. services = services.Where(p => p.Discount >= 0.30 && p.Discount < 0.70).ToList();
  87. if (ComboDiscount.SelectedIndex == 5)
  88. services = services.Where(p => p.Discount >= 0.70 && p.Discount <= 1).ToList();
  89. // Поиск по названию (регистронезависимый)
  90. services = services.Where(p => p.Title.ToLower().Contains(TBoxSearh.Text.ToLower())).ToList();
  91. LViewServices.ItemsSource= services;
  92. }
  93. private void Page_Loaded (object sender, EventArgs e)
  94. {
  95. UpdateServices();
  96. }
  97. }
  98. }