using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Media;
using Reks.Windows;
namespace Reks.Pages
{
///
/// Логика взаимодействия для UserGuestPage.xaml
///
public partial class UserGuestPage : Page
{
TradeBase DB = new TradeBase();
int AllData;
int id;
public UserGuestPage()
{
InitializeComponent();
ProductList.ItemsSource = DB.Product.ToList();
FilterByPromo.ItemsSource = new List() { "Все", "Скидка 0-9", "Скидка 10-14", "Скидка 15% и более" };
SortByName.ItemsSource = new List() { "Без сортировки", "По названию от А-Я", "По названию от Я-А" };
SortByName.SelectedIndex = 0;
FilterByPromo.SelectedIndex = 0;
AllData = ProductList.Items.Count;
AmountOfData.Text = AllData + " из " + AllData + " товаров";
}
public UserGuestPage(int id)
{
InitializeComponent();
ProductList.ItemsSource = DB.Product.ToList();
User user = DB.User.Where(x => x.UserID == id).FirstOrDefault();
if (user.UserRole == 1)
{
ButtonsForAdmin.Visibility = Visibility.Visible;
ViewOrder.Visibility = Visibility.Visible;
}
if (user.UserRole == 3)
{
ViewOrder.Visibility = Visibility.Visible;
}
FilterByPromo.ItemsSource = new List() { "Все", "Скидка 0-9", "Скидка 10-14", "Скидка 15% и более" };
SortByName.ItemsSource = new List() { "Без сортировки", "По названию от А-Я", "По названию от Я-А" };
SortByName.SelectedIndex = 0;
FilterByPromo.SelectedIndex = 0;
this.id = id;
}
public void BackgroundLoad(object sender, RoutedEventArgs e)
{
Grid bgr = (Grid)sender;
string Article = bgr.Uid.ToString();
Product pr = DB.Product.Where(x => x.ProductArticleNumber == Article).FirstOrDefault();
if (pr.ProductMaxDiscountAmount > 15)
{
bgr.Background = (SolidColorBrush)new BrushConverter().ConvertFrom("#7fff00");
}
}
public void StrikePrice(object sender, RoutedEventArgs e)
{
TextBlock tb = (TextBlock)sender;
string Article = tb.Uid.ToString();
Product pr = DB.Product.Where(x => x.ProductArticleNumber == Article).FirstOrDefault();
tb.Text = "Цена: " + string.Format("{0:C2}", pr.ProductCost);
if (pr.ProductMaxDiscountAmount > 0)
{
tb.TextDecorations = TextDecorations.Strikethrough;
}
}
public void PromoPrice(object sender, RoutedEventArgs e)
{
TextBlock tb = (TextBlock)sender;
string Article = tb.Uid.ToString();
Product pr = DB.Product.Where(x => x.ProductArticleNumber == Article).FirstOrDefault();
if (pr.ProductMaxDiscountAmount > 0)
{
tb.Text = "Цена: " + string.Format("{0:C2}", (pr.ProductCost - (pr.ProductCost / 100 * pr.ProductMaxDiscountAmount)));
tb.FontWeight = FontWeights.Bold;
}
}
private void Photo(object sender, RoutedEventArgs e)
{
Image pic = (Image)sender;
string Article = pic.Uid.ToString();
string photoPath = DB.Product.Where(x => x.ProductArticleNumber == Article).Select(x => x.ProductPhoto).FirstOrDefault();
if (photoPath != null && photoPath != "")
{
pic.Source = new BitmapImage(new Uri(Environment.CurrentDirectory + "\\Pictures\\" + photoPath, UriKind.RelativeOrAbsolute));
}
else
{
pic.Source = new BitmapImage(new Uri(Environment.CurrentDirectory + "\\Pictures\\picture.png", UriKind.RelativeOrAbsolute));
}
}
private void Filter()
{
List products = DB.Product.ToList();
if (SortByName.SelectedIndex != 0)
{
switch (SortByName.SelectedIndex)
{
case 1:
products = products.OrderBy(x => x.ProductName).ToList();
break;
case 2:
products = products.OrderByDescending(x => x.ProductName).ToList();
break;
}
}
if (FilterByPromo.SelectedIndex != 0)
{
switch (FilterByPromo.SelectedIndex)
{
case 1:
products = products.Where(x => x.ProductMaxDiscountAmount >= 0 && x.ProductMaxDiscountAmount < 10).ToList();
break;
case 2:
products = products.Where(x => x.ProductMaxDiscountAmount >= 10 && x.ProductMaxDiscountAmount < 15).ToList();
break;
case 3:
products = products.Where(x => x.ProductMaxDiscountAmount >= 15).ToList();
break;
}
}
if (!string.IsNullOrEmpty(SearchByName.Text))
{
products = products.Where(x => x.ProductName.ToUpper().Contains(SearchByName.Text.ToUpper())).ToList();
}
ProductList.ItemsSource = products;
AmountOfData.Text = products.Count() + " из " + AllData + " товаров";
}
private void SortFilterChanged(object sender, SelectionChangedEventArgs e)
{
Filter();
}
private void SearchChanged(object sender, TextChangedEventArgs e)
{
Filter();
}
private void CartAdd(object sender, RoutedEventArgs e)
{
MenuItem bt = (MenuItem)sender;
string article = bt.Uid.ToString();
Product pr = DB.Product.Where(x => x.ProductArticleNumber == article).First();
ProductAmount pa = new ProductAmount();
if (Cart.Products.Count != 0 && Cart.Products.Contains(pr))
{
pa = Cart.ProductsAmount.Where(x => x.Article == article).First();
}
if (pa.Article != null)
{
int index = Cart.ProductsAmount.IndexOf(pa);
Cart.ProductsAmount[index].Amount++;
}
else
{
pa.Amount = 1;
pa.Article = article;
Cart.Products.Add(pr);
Cart.ProductsAmount.Add(pa);
}
ToCart.Visibility = Visibility.Visible;
}
private void CartGo(object sender, RoutedEventArgs e)
{
Cart ct = new Cart();
if (Cart.Products.Count > 0)
{
ct.ShowDialog();
}
else
{
MessageBox.Show("Корзина пустая");
}
}
private void ViewOrders(object sender, RoutedEventArgs e)
{
MainFrame.mFrame.Navigate(new Orders(id));
}
private void BackToAuth(object sender, RoutedEventArgs e)
{
MainFrame.mFrame.Navigate(new Authorization());
}
}
}