ProductsPage.xaml.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using Sessioa.BaseModel;
  2. using Sessioa.Classes;
  3. using System.Collections.Generic;
  4. using System.Data.Entity;
  5. using System.Linq;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Media;
  9. namespace Sessioa.Pages
  10. {
  11. /// <summary>
  12. /// Логика взаимодействия для ProductsPage.xaml
  13. /// </summary>
  14. public partial class ProductsPage : Page
  15. {
  16. List<Product> listProduct = BaseClass.C_Base.Product.Include(x => x.Manufacturer).ToList();
  17. List<Product> listOrder = new List<Product>();
  18. User user = null;
  19. List<string> sort = new List<string>() { "Без сортировки", "По возрастанию", "По убыванию" };
  20. List<string> filtr = new List<string>() { "Все диапазоны", "0 - 9,99%", "10 - 14,99%", "15% >" };
  21. string selectSort = "Без сортировки";
  22. string selectFiltr = "Все диапазоны";
  23. public ProductsPage()
  24. {
  25. InitializeComponent();
  26. UserSign.Text = "Вход не выполнен";
  27. Init();
  28. }
  29. public ProductsPage(User userSign)
  30. {
  31. InitializeComponent();
  32. listOrder = (List<Product>)Application.Current.Properties["ListOrderUser"];
  33. user = userSign;
  34. UserSign.Text = $"Вход выолнен: {user.UserSurname} {user.UserName} {user.UserPatronymic}";
  35. Init();
  36. if(listOrder != null)
  37. {
  38. if (listOrder.Count > 0)
  39. {
  40. OrderActUser.Visibility = Visibility.Visible;
  41. OrderActUser.Content = $"Корзина({listOrder.Count})";
  42. }
  43. }
  44. }
  45. private void Init()
  46. {
  47. LB_Product.ItemsSource = listProduct;
  48. CB_Sort.ItemsSource = sort;
  49. CB_Sort.SelectedItem = selectSort;
  50. CB_Filtr.ItemsSource = filtr;
  51. CB_Filtr.SelectedItem = selectFiltr;
  52. TB_CountAll.Text = BaseClass.C_Base.Product.Count().ToString();
  53. }
  54. private void countPr_Loaded(object sender, RoutedEventArgs e)
  55. {
  56. TextBlock tb = (TextBlock)sender;
  57. Product product = BaseClass.C_Base.Product.FirstOrDefault(x => x.ProductArticleNumber == tb.Uid);
  58. if (product.ProductDiscountAmount != null && product.ProductDiscountAmount != 0)
  59. tb.TextDecorations = TextDecorations.Strikethrough;
  60. }
  61. private void countPr_Loaded_1(object sender, RoutedEventArgs e)
  62. {
  63. TextBlock tb = (TextBlock)sender;
  64. Product product = BaseClass.C_Base.Product.FirstOrDefault(x => x.ProductArticleNumber == tb.Uid);
  65. if (product.ProductDiscountAmount != null && product.ProductDiscountAmount != 0)
  66. {
  67. double j = (double)product.ProductDiscountAmount / 100;
  68. double ji = (double)(product.ProductCost) * j;
  69. double n = (double)(product.ProductCost) - ji;
  70. tb.Text = n.ToString() + "₽";
  71. tb.Visibility = Visibility.Visible;
  72. }
  73. }
  74. private void Border_Loaded(object sender, RoutedEventArgs e)
  75. {
  76. Border border = (Border)sender;
  77. Product product = BaseClass.C_Base.Product.FirstOrDefault(x => x.ProductArticleNumber == border.Uid);
  78. if (product.ProductDiscountAmount != null && product.ProductDiscountAmount > 15)
  79. border.Background = (Brush)(new BrushConverter().ConvertFrom("#7fff00"));
  80. }
  81. private void Discount_Loaded(object sender, RoutedEventArgs e)
  82. {
  83. TextBlock tb = (TextBlock)sender;
  84. Product product = BaseClass.C_Base.Product.FirstOrDefault(x => x.ProductArticleNumber == tb.Uid);
  85. if (product.ProductDiscountAmount != null && product.ProductDiscountAmount != 0)
  86. tb.Text = $"Скидка {product.ProductDiscountAmount}%";
  87. }
  88. private void SearchTB_TextChanged(object sender, TextChangedEventArgs e)
  89. {
  90. setting();
  91. }
  92. private void setting()
  93. {
  94. if (SearchTB.Text.Length > 0)
  95. listProduct = BaseClass.C_Base.Product.Where(x => x.ProductName.ToLower().Contains(SearchTB.Text.ToLower())).ToList();
  96. else listProduct = BaseClass.C_Base.Product.ToList();
  97. if (CB_Sort.SelectedIndex != 0)
  98. {
  99. if (CB_Sort.SelectedIndex == 1)
  100. {
  101. listProduct = listProduct.OrderBy(x => (x.ProductDiscountAmount != null ? x.ProductCost - x.ProductCost * x.ProductDiscountAmount / 100 : x.ProductCost)).ToList();
  102. }
  103. if (CB_Sort.SelectedIndex == 2)
  104. {
  105. listProduct = listProduct.OrderByDescending(x => (x.ProductDiscountAmount != null ? x.ProductCost - x.ProductCost * x.ProductDiscountAmount / 100 : x.ProductCost)).ToList();
  106. }
  107. }
  108. if (CB_Filtr.SelectedIndex != 0)
  109. {
  110. switch (CB_Filtr.SelectedIndex)
  111. {
  112. case 1:
  113. listProduct = listProduct.Where(x => x.ProductDiscountAmount >= 0 && x.ProductDiscountAmount <= 9.99).ToList();
  114. break;
  115. case 2:
  116. listProduct = listProduct.Where(x => x.ProductDiscountAmount >= 10 && x.ProductDiscountAmount <= 14.99).ToList();
  117. break;
  118. case 3:
  119. listProduct = listProduct.Where(x => x.ProductDiscountAmount >= 15).ToList();
  120. break;
  121. }
  122. }
  123. LB_Product.ItemsSource = null;
  124. LB_Product.ItemsSource = listProduct;
  125. TB_CountActual.Text = listProduct.Count.ToString();
  126. }
  127. private void CB_Filtr_SelectionChanged(object sender, SelectionChangedEventArgs e)
  128. {
  129. setting();
  130. }
  131. private void CB_Sort_SelectionChanged(object sender, SelectionChangedEventArgs e)
  132. {
  133. setting();
  134. }
  135. private void SignOf_Click(object sender, RoutedEventArgs e)
  136. {
  137. Application.Current.Properties["ListOrderUser"] = new List<Product>();
  138. FrameClass.C_Frame.Navigate(new Autorization());
  139. }
  140. private void ContextMenu_Loaded(object sender, RoutedEventArgs e)
  141. {
  142. ContextMenu cm = (ContextMenu)(sender);
  143. if (user == null)
  144. cm.Visibility = Visibility.Collapsed;
  145. }
  146. private void EditProduct_Loaded(object sender, RoutedEventArgs e)
  147. {
  148. MenuItem mt = (MenuItem)(sender);
  149. if (user == null)
  150. mt.Visibility = Visibility.Collapsed;
  151. else if (user.UserRole != 2)
  152. mt.Visibility = Visibility.Collapsed;
  153. }
  154. private void DeleteProduct_Loaded(object sender, RoutedEventArgs e)
  155. {
  156. MenuItem dt = (MenuItem)(sender);
  157. if (user == null)
  158. dt.Visibility = Visibility.Collapsed;
  159. else if (user.UserRole != 2)
  160. dt.Visibility = Visibility.Collapsed;
  161. }
  162. private void NewProduct_Click(object sender, RoutedEventArgs e)
  163. {
  164. FrameClass.C_Frame.Navigate(new ProductCard(user));
  165. }
  166. private void NewProduct_Loaded(object sender, RoutedEventArgs e)
  167. {
  168. Button btn = (Button)(sender);
  169. if (user != null && user.UserRole == 2)
  170. btn.Visibility = Visibility.Visible;
  171. }
  172. private void EditProduct_Click(object sender, RoutedEventArgs e)
  173. {
  174. try
  175. {
  176. FrameClass.C_Frame.Navigate(new ProductCard(user, (Product)LB_Product.SelectedItem));
  177. }
  178. catch
  179. {
  180. MessageBox.Show("Непредвиденная ошибка.", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Information);
  181. }
  182. }
  183. private void DeleteProduct_Click(object sender, RoutedEventArgs e)
  184. {
  185. MessageBoxResult mbr = MessageBox.Show("Удалить продукт?", "квешченс", MessageBoxButton.YesNo, MessageBoxImage.Question);
  186. if (mbr == MessageBoxResult.Yes)
  187. {
  188. try
  189. {
  190. BaseClass.C_Base.Product.Remove((Product)LB_Product.SelectedItem);
  191. BaseClass.C_Base.SaveChanges();
  192. MessageBox.Show("Продукт удален", "Оповещение", MessageBoxButton.OK, MessageBoxImage.Information);
  193. FrameClass.C_Frame.Navigate(new ProductsPage(user));
  194. }
  195. catch
  196. {
  197. MessageBox.Show("Ошибка. Продукт не может быть удален.", "Оповещение", MessageBoxButton.OK, MessageBoxImage.Error);
  198. }
  199. }
  200. }
  201. private void OrdersUser_Loaded(object sender, RoutedEventArgs e)
  202. {
  203. Button button = (Button)sender;
  204. if (user != null)
  205. {
  206. if (user.UserRole > 0)
  207. button.Visibility = Visibility.Visible;
  208. else if (BaseClass.C_Base.Order.Where(x => x.UserID == user.UserID).Count() > 0)
  209. button.Visibility = Visibility.Visible;
  210. }
  211. }
  212. private void OrdersUser_Click(object sender, RoutedEventArgs e)
  213. {
  214. FrameClass.C_Frame.Navigate(new OrderNew(user));
  215. }
  216. private void AddOrder_Click(object sender, RoutedEventArgs e)
  217. {
  218. if (listOrder.Contains((Product)LB_Product.SelectedItem))
  219. MessageBox.Show("Данный товар уже находится в корзине", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Warning);
  220. else
  221. {
  222. listOrder.Add((Product)LB_Product.SelectedItem);
  223. OrderActUser.Content = $"Корзина({listOrder.Count})";
  224. }
  225. OrderActUser.Visibility = Visibility.Visible;
  226. }
  227. private void OrderActUser_Click(object sender, RoutedEventArgs e)
  228. {
  229. Application.Current.Properties["ListOrderUser"] = listOrder;
  230. FrameClass.C_Frame.Navigate(new OrderCard(user));
  231. }
  232. }
  233. }