PageMain.xaml.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. using WriteErase.ClassBase;
  16. using WriteErase.ClassData;
  17. using WriteErase.ClassWindows;
  18. namespace WriteErase.ClassPages
  19. {
  20. /// <summary>
  21. /// Логика взаимодействия для PageMain.xaml
  22. /// </summary>
  23. public partial class PageMain : Page
  24. {
  25. private enum Role { Admin, Manager, User, Noname };
  26. private Role userRole;
  27. private string lastClickItem = "";
  28. public PageMain()
  29. {
  30. InitializeComponent();
  31. initCompomemt();
  32. }
  33. private void initCompomemt()
  34. {
  35. if (UserSession.user != null)
  36. {
  37. switch (UserSession.user.Role.RoleName)
  38. {
  39. case "Администратор":
  40. userRole = Role.Admin; break;
  41. case "Менеджер":
  42. userRole = Role.Manager; break;
  43. case "Клиент":
  44. userRole = Role.User; break;
  45. default:
  46. userRole = Role.Noname; break;
  47. }
  48. string FullName = UserSession.user.UserSurname + " " +
  49. UserSession.user.UserName[0] + ". ";
  50. if (!string.IsNullOrWhiteSpace(UserSession.user.UserPatronymic))
  51. FullName += UserSession.user.UserPatronymic[0] + ".";
  52. tbkFullName.Text = FullName;
  53. }
  54. else
  55. {
  56. tbkFullName.Visibility = Visibility.Collapsed;
  57. userRole = Role.Noname;
  58. }
  59. List<Product> products = BaseConnection.kotBase.Product.ToList();
  60. lvProduct.ItemsSource = products;
  61. tbkCountRecord.Text = "Количество записей " + products.Count + " из " + products.Count;
  62. if (userRole == Role.Noname || userRole == Role.User)
  63. {
  64. btnOrderView.Visibility = Visibility.Collapsed;
  65. }
  66. if (userRole != Role.Admin)
  67. btnAddProduct.Visibility = Visibility.Collapsed;
  68. if (UserBasket.basket.Count > 0)
  69. btnBasket.Visibility = Visibility.Visible;
  70. else
  71. btnBasket.Visibility = Visibility.Collapsed;
  72. }
  73. private void btnExitClick(object sender, RoutedEventArgs e)
  74. {
  75. UserSession.user = null;
  76. UserBasket.basket.Clear();
  77. MainFrame.mainFrame.Navigate(new PageAuthorization());
  78. }
  79. private void SearchSortFilter()
  80. {
  81. List<Product> _products = BaseConnection.kotBase.Product.ToList();
  82. int allRecordCount = _products.Count;
  83. if (!string.IsNullOrWhiteSpace(tbxName.Text))
  84. _products = _products.Where(tb => tb.ProductName.ToLower().Contains(tbxName.Text.ToLower())).ToList();
  85. switch (cbCost.SelectedIndex)
  86. {
  87. case 1:
  88. {
  89. _products = _products.OrderBy(tb => tb.Cost).ToList();
  90. break;
  91. }
  92. case 2:
  93. {
  94. _products = _products.OrderByDescending(tb => tb.Cost).ToList();
  95. break;
  96. }
  97. default:
  98. break;
  99. }
  100. switch (cbDiscount.SelectedIndex)
  101. {
  102. case 1:
  103. {
  104. _products = _products.Where(tb => tb.ProductDiscountMax >= 0.0 && tb.ProductDiscountMax <= 9.99).ToList();
  105. break;
  106. }
  107. case 2:
  108. {
  109. _products = _products.Where(tb => tb.ProductDiscountMax >= 10.0 && tb.ProductDiscountMax <= 14.99).ToList();
  110. break;
  111. }
  112. case 3:
  113. {
  114. _products = _products.Where(tb => tb.ProductDiscountMax >= 15.0).ToList();
  115. break;
  116. }
  117. default:
  118. break;
  119. }
  120. lvProduct.ItemsSource = _products;
  121. tbkCountRecord.Text = "Количество записей " + _products.Count + " из " + allRecordCount;
  122. }
  123. private void SortSelectionChanged(object sender, SelectionChangedEventArgs e)
  124. {
  125. if (cbDiscount != null && lvProduct != null)
  126. SearchSortFilter();
  127. }
  128. private void FilterSelectionChanged(object sender, SelectionChangedEventArgs e)
  129. {
  130. if (cbCost != null && lvProduct != null)
  131. SearchSortFilter();
  132. }
  133. private void SearchTextChanged(object sender, TextChangedEventArgs e)
  134. {
  135. if (tbxName != null && lvProduct != null)
  136. SearchSortFilter();
  137. }
  138. private void AddBasketClick(object sender, RoutedEventArgs e)
  139. {
  140. MenuItem menuItem = (MenuItem)sender;
  141. if (lastClickItem != "")
  142. {
  143. Product product = BaseConnection.kotBase.Product.First(tb => tb.ProductArticleNumber == lastClickItem);
  144. switch (menuItem.Header)
  145. {
  146. case "Добавить в корзину":
  147. {
  148. product.CountProduct = 1;
  149. UserBasket.basket.Add(product);
  150. break;
  151. }
  152. case "Удалить из корзины":
  153. {
  154. product.CountProduct = 0;
  155. UserBasket.basket.Remove(product);
  156. break;
  157. }
  158. }
  159. }
  160. if (UserBasket.basket.Count > 0)
  161. btnBasket.Visibility = Visibility.Visible;
  162. else
  163. btnBasket.Visibility = Visibility.Collapsed;
  164. }
  165. private void DelteClick(object sender, RoutedEventArgs e)
  166. {
  167. if (lastClickItem != "")
  168. {
  169. Product product = BaseConnection.kotBase.Product.First(tb => tb.ProductArticleNumber == lastClickItem);
  170. BaseConnection.kotBase.Product.Remove(product);
  171. BaseConnection.kotBase.SaveChanges();
  172. SearchSortFilter();
  173. }
  174. }
  175. private void EditClick(object sender, RoutedEventArgs e)
  176. {
  177. Product p = (Product)lvProduct.SelectedItem;
  178. MainFrame.mainFrame.Navigate(new PageEditProduct(p.ProductArticleNumber));
  179. }
  180. private void AddBasketLoaded(object sender, RoutedEventArgs e)
  181. {
  182. MenuItem menuItem = (MenuItem)sender;
  183. if (UserBasket.basket.FirstOrDefault(tb => tb.ProductArticleNumber == lastClickItem) == null)
  184. menuItem.Header = "Добавить в корзину";
  185. else
  186. menuItem.Header = "Удалить из корзины";
  187. }
  188. private void AdminLoaded(object sender, RoutedEventArgs e)
  189. {
  190. MenuItem menuItem = (MenuItem)sender;
  191. if (userRole == Role.Admin)
  192. menuItem.Visibility = Visibility.Visible;
  193. else
  194. menuItem.Visibility = Visibility.Collapsed;
  195. }
  196. private void ListViewItemMouseDown(object sender, MouseButtonEventArgs e)
  197. {
  198. Border border = (Border)sender;
  199. lastClickItem = border.Uid;
  200. }
  201. private void btnBasketClick(object sender, RoutedEventArgs e)
  202. {
  203. WindowOrderView window = new WindowOrderView();
  204. window.ShowDialog();
  205. if (UserBasket.basket.Count == 0)
  206. btnBasket.Visibility = Visibility.Collapsed;
  207. }
  208. private void btnOrderViewClick(object sender, RoutedEventArgs e)
  209. {
  210. MainFrame.mainFrame.Navigate(new PageOrderView());
  211. }
  212. private void btnAddProductClick(object sender, RoutedEventArgs e)
  213. {
  214. MainFrame.mainFrame.Navigate(new PageEditProduct());
  215. }
  216. }
  217. }