ProductListUser.xaml.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. using UP_Venediktov.Classes;
  17. using UP_Venediktov.ModelBase;
  18. namespace UP_Venediktov.Pages
  19. {
  20. /// <summary>
  21. /// Логика взаимодействия для ProductListUser.xaml
  22. /// </summary>
  23. public partial class ProductListUser : Page
  24. {
  25. Entities DB = new Entities();
  26. int AllData;
  27. int id;
  28. public ProductListUser()
  29. {
  30. InitializeComponent();
  31. ProductList.ItemsSource = DB.Product.ToList();
  32. CB_Filter.ItemsSource = new List<string>() { "Все", "Скидка 0-9,99%", "Скидка 10-14,99%", "Скидка 15% и более" };
  33. CB_Sort.ItemsSource = new List<string>() { "Без сортировки", "По имени А-Я", "По имени Я-А" };
  34. CB_Sort.SelectedIndex = 0;
  35. CB_Filter.SelectedIndex = 0;
  36. AllData = ProductList.Items.Count;
  37. //AmountOfData.Text = AllData + " из " + AllData + " товаров";
  38. }
  39. public ProductListUser(int id)
  40. {
  41. InitializeComponent();
  42. ProductList.ItemsSource = DB.Product.ToList();
  43. User user = DB.User.Where(x => x.UserID == id).FirstOrDefault();
  44. if (user.UserRole == 1)
  45. {
  46. ButtonsForAdmin.Visibility = Visibility.Visible;
  47. ViewOrder.Visibility = Visibility.Visible;
  48. }
  49. if (user.UserRole == 3)
  50. {
  51. ViewOrder.Visibility = Visibility.Visible;
  52. }
  53. CB_Filter.ItemsSource = new List<string>() { "Все", "Скидка 0-9,99%", "Скидка 10-14,99%", "Скидка 15% и более" };
  54. CB_Sort.ItemsSource = new List<string>() { "Без сортировки", "По имени А-Я", "По имени Я-А" };
  55. CB_Sort.SelectedIndex = 0;
  56. CB_Filter.SelectedIndex = 0;
  57. this.id = id;
  58. }
  59. public void BackgroundLoad(object sender, RoutedEventArgs e)
  60. {
  61. Grid bgr = (Grid)sender;
  62. string Article = bgr.Uid.ToString();
  63. Product pr = DB.Product.Where(x => x.ProductArticleNumber == Article).FirstOrDefault();
  64. if (pr.ProductDiscountAmount > 15)
  65. {
  66. bgr.Background = (SolidColorBrush)new BrushConverter().ConvertFrom("#7fff00");
  67. }
  68. }
  69. public void StrikePrice(object sender, RoutedEventArgs e)
  70. {
  71. TextBlock tb = (TextBlock)sender;
  72. string Article = tb.Uid.ToString();
  73. Product pr = DB.Product.Where(x => x.ProductArticleNumber == Article).FirstOrDefault();
  74. tb.Text = "Цена: " + string.Format("{0:C2}", pr.ProductCost);
  75. if (pr.ProductDiscountAmount > 0)
  76. {
  77. tb.TextDecorations = TextDecorations.Strikethrough;
  78. }
  79. }
  80. public void PromoPrice(object sender, RoutedEventArgs e)
  81. {
  82. TextBlock tb = (TextBlock)sender;
  83. string Article = tb.Uid.ToString();
  84. Product pr = DB.Product.Where(x => x.ProductArticleNumber == Article).FirstOrDefault();
  85. if (pr.ProductDiscountAmount > 0)
  86. {
  87. tb.Text = "Цена: " + string.Format("{0:C2}", (pr.ProductCost - (pr.ProductCost / 100 * pr.ProductDiscountAmount)));
  88. tb.FontWeight = FontWeights.Bold;
  89. }
  90. }
  91. private void Photo(object sender, RoutedEventArgs e)
  92. {
  93. Image pic = (Image)sender;
  94. string Article = pic.Uid.ToString();
  95. string photoPath = DB.Product.Where(x => x.ProductArticleNumber == Article).Select(x => x.ProductPhoto).FirstOrDefault();
  96. if (photoPath != null && photoPath != "")
  97. {
  98. pic.Source = new BitmapImage(new Uri(Environment.CurrentDirectory + "\\Pictures\\" + photoPath, UriKind.RelativeOrAbsolute));
  99. }
  100. else
  101. {
  102. pic.Source = new BitmapImage(new Uri(Environment.CurrentDirectory + "\\Pictures\\picture.png", UriKind.RelativeOrAbsolute));
  103. }
  104. }
  105. private void Filter()
  106. {
  107. List<Product> products = DB.Product.ToList();
  108. if (CB_Sort.SelectedIndex != 0)
  109. {
  110. switch (CB_Sort.SelectedIndex)
  111. {
  112. case 1:
  113. products = products.OrderBy(x => x.ProductName).ToList();
  114. break;
  115. case 2:
  116. products = products.OrderByDescending(x => x.ProductName).ToList();
  117. break;
  118. }
  119. }
  120. if (CB_Filter.SelectedIndex != 0)
  121. {
  122. switch (CB_Filter.SelectedIndex)
  123. {
  124. case 1:
  125. products = products.Where(x => x.ProductDiscountAmount >= 0 && x.ProductDiscountAmount < 10).ToList();
  126. break;
  127. case 2:
  128. products = products.Where(x => x.ProductDiscountAmount >= 10 && x.ProductDiscountAmount < 15).ToList();
  129. break;
  130. case 3:
  131. products = products.Where(x => x.ProductDiscountAmount >= 15).ToList();
  132. break;
  133. }
  134. }
  135. if (!string.IsNullOrEmpty(TB_Search.Text))
  136. {
  137. products = products.Where(x => x.ProductName.ToUpper().Contains(TB_Search.Text.ToUpper())).ToList();
  138. }
  139. ProductList.ItemsSource = products;
  140. //AmountOfData.Text = products.Count() + " из " + AllData + " товаров";
  141. }
  142. private void SortFilterChanged(object sender, SelectionChangedEventArgs e)
  143. {
  144. Filter();
  145. }
  146. private void SearchChanged(object sender, TextChangedEventArgs e)
  147. {
  148. Filter();
  149. }
  150. private void CartAdd(object sender, RoutedEventArgs e)
  151. {
  152. MenuItem bt = (MenuItem)sender;
  153. string article = bt.Uid.ToString();
  154. Product pr = DB.Product.Where(x => x.ProductArticleNumber == article).First();
  155. AmountofProduct pa = new AmountofProduct();
  156. if (CardOrder.Products.Count != 0 && CardOrder.Products.Contains(pr))
  157. {
  158. pa = CardOrder.AmountofProducts.Where(x => x.Article == article).First();
  159. }
  160. if (pa.Article != null)
  161. {
  162. int index = CardOrder.AmountofProducts.IndexOf(pa);
  163. CardOrder.AmountofProducts[index].Amount++;
  164. }
  165. else
  166. {
  167. pa.Amount = 1;
  168. pa.Article = article;
  169. CardOrder.Products.Add(pr);
  170. CardOrder.AmountofProducts.Add(pa);
  171. }
  172. GoToCard.Visibility = Visibility.Visible;
  173. }
  174. private void CartGo(object sender, RoutedEventArgs e)
  175. {
  176. CardOrder ct = new CardOrder();
  177. if (CardOrder.Products.Count > 0)
  178. {
  179. FrameClass.MainFrame.Navigate(new CardOrder());
  180. }
  181. else
  182. {
  183. MessageBox.Show("Чтобы перейти в корзину добавьте в неё товар");
  184. }
  185. }
  186. private void ViewOrders(object sender, RoutedEventArgs e)
  187. {
  188. FrameClass.MainFrame.Navigate(new Orders(id));
  189. }
  190. private void BackToAvt(object sender, RoutedEventArgs e)
  191. {
  192. FrameClass.MainFrame.Navigate(new Avtorization());
  193. }
  194. }
  195. }