UserBlank.xaml.cs 7.8 KB

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