using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Diagnostics;
using System.Linq;
using System.Security.Cryptography.Xml;
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 Write_erase.Classes;
using static Write_erase.Classes.RoleClass;
namespace Write_erase.UserPages
{
///
/// Логика взаимодействия для ShowProductPage.xaml
///
public partial class ShowProductPage : Page
{
RoleUser nowRole;
User nowUser;
List products = DataBaseClass.entity.Product.ToList();
string Click = "";
int id;
public ShowProductPage()
{
InitializeComponent();
List users = DataBaseClass.entity.User.Where(x => x.UserID == id).ToList();
foreach (User user in users)
{
FIO.Text = user.UserPatronymic + " " + user.UserName + " " + user.UserSurname;
}
ProductPanelUser.ItemsSource = DataBaseClass.entity.Product.ToList();
CBDiscount.Items.Add("Все диапазоны");
CBDiscount.Items.Add("0-9,99%");
CBDiscount.Items.Add("10-14,99%");
CBDiscount.Items.Add("15% и более");
CBDiscount.SelectedIndex = 0;
TBShowAll.Text = "Всего товаров: " + DataBaseClass.entity.Product.ToList().Count;
nowRole = RoleUser.Noname;
}
public ShowProductPage(int id)
{
id = id;
InitializeComponent();
List users = DataBaseClass.entity.User.Where(x=> x.UserID == id).ToList();
foreach (User user in users)
{
FIO.Text = user.UserPatronymic + " " + user.UserName + " " + user.UserSurname;
}
ProductPanelUser.ItemsSource = DataBaseClass.entity.Product.ToList();
CBDiscount.Items.Add("Все диапазоны");
CBDiscount.Items.Add("0-9,99%");
CBDiscount.Items.Add("10-14,99%");
CBDiscount.Items.Add("15% и более");
CBDiscount.SelectedIndex = 0;
TBShowAll.Text = "Всего товаров: " + DataBaseClass.entity.Product.ToList().Count;
nowRole = RoleUser.User;
nowUser = DataBaseClass.entity.User.FirstOrDefault(x => x.UserID == id);
}
private void Filter()
{
switch (CBDiscount.SelectedIndex)
{
case 0: products = DataBaseClass.entity.Product.ToList(); break;
case 1: products = DataBaseClass.entity.Product.Where(x => x.ProductDiscountAmount / 100 < 0.0999).ToList(); break;
case 2: products = DataBaseClass.entity.Product.Where(x => x.ProductDiscountAmount / 100 >= 10 && x.ProductDiscountAmount / 100 < 0.1499).ToList(); break;
case 3: products = DataBaseClass.entity.Product.Where(x => x.ProductDiscountAmount / 100 >= 0.15).ToList(); break;
}
ProductPanelUser.ItemsSource = products;
TBShowPart.Text = "Показано предложений: " + products.Count;
}
private void Sorter()
{
if (!string.IsNullOrWhiteSpace(TBXSearch.Text))
{
products = DataBaseClass.entity.Product.Where(x => x.ProductName.ToLower().Contains(TBXSearch.Text.ToLower())).ToList();
}
if (RBUp.IsChecked == true)
{
products.Sort((x, y) => x.ProductCost.CompareTo(y.ProductCost));
}
else if (RBDown.IsChecked == true)
{
products.Sort((x, y) => x.ProductCost.CompareTo(y.ProductCost));
products.Reverse();
}
else if (RBDefault.IsChecked == true)
{
products.Sort((x, y) => x.ProductArticleNumber.CompareTo(y.ProductArticleNumber));
}
if (products.Count == 0)
{
MessageBox.Show("Товаров с таким диапазоном скидок нет");
}
ProductPanelUser.ItemsSource = products;
TBShowPart.Text = "Показано предложений: " + products.Count;
}
private void RBUp_Click(object sender, RoutedEventArgs e)
{
Sorter();
}
private void RBDown_Click(object sender, RoutedEventArgs e)
{
Sorter();
}
private void RBDefault_Click(object sender, RoutedEventArgs e)
{
Sorter();
}
private void CBDiscount_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Filter();
}
private void TBXSearch_TextChanged(object sender, TextChangedEventArgs e)
{
Sorter();
}
private void Delete_Click(object sender, RoutedEventArgs e)
{
}
private void Add_Click(object sender, RoutedEventArgs e)
{
Product p = DataBaseClass.entity.Product.FirstOrDefault(x => x.ProductArticleNumber == ((Product)ProductPanelUser.SelectedItem).ProductArticleNumber);
if (p != null)
{
switch (((MenuItem)sender).Header)
{
case "Добавить к заказу":
if (BascketClass.bascket.Contains(p))
{
p.CountProduct++;
}
else
{
p.CountProduct++;
BascketClass.bascket.Add(p);
}
Basket.Visibility = Visibility.Visible;
break;
case "Удалить из заказа":
if (BascketClass.bascket.Contains(p) && p.CountProduct > 1)
{
p.CountProduct--;
}
else if (BascketClass.bascket.Contains(p) && p.CountProduct == 1)
{
p.CountProduct--;
BascketClass.bascket.Remove(p);
Basket.Visibility = Visibility.Hidden;
}
break;
}
}
}
private void Back_Click(object sender, RoutedEventArgs e)
{
FrameClass.MFrame.Navigate(new MainPages.AutorizePage());
}
private void Basket_Click(object sender, RoutedEventArgs e)
{
BasketWindow bw;
if (nowRole == RoleUser.Noname)
bw = new BasketWindow(BascketClass.bascket);
else
bw = new BasketWindow(BascketClass.bascket, nowUser);
bw.ShowDialog();
if (BascketClass.bascket.Count > 0)
Basket.Visibility = Visibility.Visible;
else
Basket.Visibility = Visibility.Collapsed;
}
}
}