Guest.xaml.cs 7.5 KB

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