Guest.xaml.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 GGwp
  16. {
  17. /// <summary>
  18. /// Логика взаимодействия для Guest.xaml
  19. /// </summary>
  20. public partial class Guest : Page
  21. {
  22. TradePrytovEntities DB = new TradePrytovEntities();
  23. int AllData;
  24. public Guest()
  25. {
  26. InitializeComponent();
  27. ProductList.ItemsSource = DB.Product.ToList();
  28. FilterByPromo.ItemsSource = new List<string>() { "Все", "Скаидка 0-9,99%", "Скидка 10-14,99%", "Скидка 15% и более" };
  29. SortByName.ItemsSource = new List<string>() { "Без сортировки", "По имени А-Я", "По имени Я-А" };
  30. SortByName.SelectedIndex = 0;
  31. FilterByPromo.SelectedIndex = 0;
  32. AllData = ProductList.Items.Count;
  33. AmountOfData.Text = AllData + " из " + AllData + " товаров";
  34. }
  35. public Guest(int id)
  36. {
  37. InitializeComponent();
  38. ProductList.ItemsSource = DB.Product.ToList();
  39. User user = DB.User.Where(x => x.UserID == id).FirstOrDefault();
  40. if (user.UserRole == 1)
  41. {
  42. ButtonsForAdmin.Visibility = Visibility.Visible;
  43. }
  44. FilterByPromo.ItemsSource = new List<string>() { "Все", "Скидка 0-9,99%", "Скидка 10-14,99%", "Скидка 15% и более" };
  45. SortByName.ItemsSource = new List<string>() { "Без сортировки", "По имени А-Я", "По имени Я-А" };
  46. SortByName.SelectedIndex = 0;
  47. FilterByPromo.SelectedIndex = 0;
  48. }
  49. public void BackgroundLoad(object sender, RoutedEventArgs e)
  50. {
  51. Grid bgr = (Grid)sender;
  52. string Article = bgr.Uid.ToString();
  53. Product pr = DB.Product.Where(x => x.ProductArticleNumber == Article).FirstOrDefault();
  54. if (pr.ProductMaxDiscountAmount > 15)
  55. {
  56. bgr.Background = (SolidColorBrush)new BrushConverter().ConvertFrom("#7fff00");
  57. }
  58. }
  59. public void StrikePrice(object sender, RoutedEventArgs e)
  60. {
  61. TextBlock tb = (TextBlock)sender;
  62. string Article = tb.Uid.ToString();
  63. Product pr = DB.Product.Where(x => x.ProductArticleNumber == Article).FirstOrDefault();
  64. tb.Text = "Цена: " + string.Format("{0:C2}", pr.ProductCost);
  65. if (pr.ProductMaxDiscountAmount > 0)
  66. {
  67. tb.TextDecorations = TextDecorations.Strikethrough;
  68. }
  69. }
  70. public void PromoPrice(object sender, RoutedEventArgs e)
  71. {
  72. TextBlock tb = (TextBlock)sender;
  73. string Article = tb.Uid.ToString();
  74. Product pr = DB.Product.Where(x => x.ProductArticleNumber == Article).FirstOrDefault();
  75. if (pr.ProductMaxDiscountAmount > 0)
  76. {
  77. tb.Text = "Цена: " + string.Format("{0:C2}", (pr.ProductCost - (pr.ProductCost / 100 * pr.ProductMaxDiscountAmount)));
  78. tb.FontWeight = FontWeights.Bold;
  79. }
  80. }
  81. private void Photo(object sender, RoutedEventArgs e)
  82. {
  83. Image pic = (Image)sender;
  84. string Article = pic.Uid.ToString();
  85. string photoPath = DB.Product.Where(x => x.ProductArticleNumber == Article).Select(x => x.ProductPhoto).FirstOrDefault();
  86. if (photoPath != null && photoPath != "")
  87. {
  88. pic.Source = new BitmapImage(new Uri(Environment.CurrentDirectory + "\\Pictures\\" + photoPath, UriKind.RelativeOrAbsolute));
  89. }
  90. else
  91. {
  92. pic.Source = new BitmapImage(new Uri(Environment.CurrentDirectory + "\\Pictures\\picture.png", UriKind.RelativeOrAbsolute));
  93. }
  94. }
  95. private void Filter()
  96. {
  97. List<Product> products = DB.Product.ToList();
  98. if (SortByName.SelectedIndex != 0)
  99. {
  100. switch (SortByName.SelectedIndex)
  101. {
  102. case 1:
  103. products = products.OrderBy(x => x.ProductName).ToList();
  104. break;
  105. case 2:
  106. products = products.OrderByDescending(x => x.ProductName).ToList();
  107. break;
  108. }
  109. }
  110. if (FilterByPromo.SelectedIndex != 0)
  111. {
  112. switch (FilterByPromo.SelectedIndex)
  113. {
  114. case 1:
  115. products = products.Where(x => x.ProductMaxDiscountAmount >= 0 && x.ProductMaxDiscountAmount < 10).ToList();
  116. break;
  117. case 2:
  118. products = products.Where(x => x.ProductMaxDiscountAmount >= 10 && x.ProductMaxDiscountAmount < 15).ToList();
  119. break;
  120. case 3:
  121. products = products.Where(x => x.ProductMaxDiscountAmount >= 15).ToList();
  122. break;
  123. }
  124. }
  125. if (!string.IsNullOrEmpty(SearchByName.Text))
  126. {
  127. products = products.Where(x => x.ProductName.ToUpper().Contains(SearchByName.Text.ToUpper())).ToList();
  128. }
  129. ProductList.ItemsSource = products;
  130. AmountOfData.Text = products.Count() + " из " + AllData + " товаров";
  131. }
  132. private void SortFilterChanged(object sender, SelectionChangedEventArgs e)
  133. {
  134. Filter();
  135. }
  136. private void SearchChanged(object sender, TextChangedEventArgs e)
  137. {
  138. Filter();
  139. }
  140. private void CartAdd(object sender, RoutedEventArgs e)
  141. {
  142. MenuItem bt = (MenuItem)sender;
  143. string article = bt.Uid.ToString();
  144. Product pr = DB.Product.Where(x => x.ProductArticleNumber == article).First();
  145. AmountProduct pa = new AmountProduct();
  146. if (Cart.AmountProduct.Count != 0 && Cart.AmountProduct.Contains(pa))
  147. {
  148. pa = Cart.AmountProduct.Where(x => x.Article == article).First();
  149. }
  150. if (pa.Article != null)
  151. {
  152. int index = Cart.AmountProduct.IndexOf(pa);
  153. Cart. AmountProduct[index].Amount++;
  154. }
  155. else
  156. {
  157. pa.Amount = 1;
  158. pa.Article = article;
  159. Cart.Products.Add(pr);
  160. Cart.AmountProduct.Add(pa);
  161. }
  162. ToCart.Visibility = Visibility.Visible;
  163. }
  164. private void CartGo(object sender, RoutedEventArgs e)
  165. {
  166. Cart ct = new Cart();
  167. ct.ShowDialog();
  168. }
  169. }
  170. }