123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- 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 WriteErase.ClassBase;
- using WriteErase.ClassData;
- using WriteErase.ClassWindows;
- namespace WriteErase.ClassPages
- {
- /// <summary>
- /// Логика взаимодействия для PageMain.xaml
- /// </summary>
- public partial class PageMain : Page
- {
- private enum Role { Admin, Manager, User, Noname };
- private Role userRole;
- private string lastClickItem = "";
- public PageMain()
- {
- InitializeComponent();
- initCompomemt();
- }
- private void initCompomemt()
- {
- if (UserSession.user != null)
- {
- switch (UserSession.user.Role.RoleName)
- {
- case "Администратор":
- userRole = Role.Admin; break;
- case "Менеджер":
- userRole = Role.Manager; break;
- case "Клиент":
- userRole = Role.User; break;
- default:
- userRole = Role.Noname; break;
- }
- string FullName = UserSession.user.UserSurname + " " +
- UserSession.user.UserName[0] + ". ";
- if (!string.IsNullOrWhiteSpace(UserSession.user.UserPatronymic))
- FullName += UserSession.user.UserPatronymic[0] + ".";
- tbkFullName.Text = FullName;
- }
- else
- {
- tbkFullName.Visibility = Visibility.Collapsed;
- userRole = Role.Noname;
- }
- List<Product> products = BaseConnection.kotBase.Product.ToList();
- lvProduct.ItemsSource = products;
- tbkCountRecord.Text = "Количество записей " + products.Count + " из " + products.Count;
- if (userRole == Role.Noname || userRole == Role.User)
- {
- btnOrderView.Visibility = Visibility.Collapsed;
- }
- if (userRole != Role.Admin)
- btnAddProduct.Visibility = Visibility.Collapsed;
- if (UserBasket.basket.Count > 0)
- btnBasket.Visibility = Visibility.Visible;
- else
- btnBasket.Visibility = Visibility.Collapsed;
- }
- private void btnExitClick(object sender, RoutedEventArgs e)
- {
- UserSession.user = null;
- UserBasket.basket.Clear();
- MainFrame.mainFrame.Navigate(new PageAuthorization());
- }
- private void SearchSortFilter()
- {
- List<Product> _products = BaseConnection.kotBase.Product.ToList();
- int allRecordCount = _products.Count;
- if (!string.IsNullOrWhiteSpace(tbxName.Text))
- _products = _products.Where(tb => tb.ProductName.ToLower().Contains(tbxName.Text.ToLower())).ToList();
- switch (cbCost.SelectedIndex)
- {
- case 1:
- {
- _products = _products.OrderBy(tb => tb.Cost).ToList();
- break;
- }
- case 2:
- {
- _products = _products.OrderByDescending(tb => tb.Cost).ToList();
- break;
- }
- default:
- break;
- }
- switch (cbDiscount.SelectedIndex)
- {
- case 1:
- {
- _products = _products.Where(tb => tb.ProductDiscountMax >= 0.0 && tb.ProductDiscountMax <= 9.99).ToList();
- break;
- }
- case 2:
- {
- _products = _products.Where(tb => tb.ProductDiscountMax >= 10.0 && tb.ProductDiscountMax <= 14.99).ToList();
- break;
- }
- case 3:
- {
- _products = _products.Where(tb => tb.ProductDiscountMax >= 15.0).ToList();
- break;
- }
- default:
- break;
- }
- lvProduct.ItemsSource = _products;
- tbkCountRecord.Text = "Количество записей " + _products.Count + " из " + allRecordCount;
- }
- private void SortSelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (cbDiscount != null && lvProduct != null)
- SearchSortFilter();
- }
- private void FilterSelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (cbCost != null && lvProduct != null)
- SearchSortFilter();
- }
- private void SearchTextChanged(object sender, TextChangedEventArgs e)
- {
- if (tbxName != null && lvProduct != null)
- SearchSortFilter();
- }
- private void AddBasketClick(object sender, RoutedEventArgs e)
- {
- MenuItem menuItem = (MenuItem)sender;
- if (lastClickItem != "")
- {
- Product product = BaseConnection.kotBase.Product.First(tb => tb.ProductArticleNumber == lastClickItem);
- switch (menuItem.Header)
- {
- case "Добавить в корзину":
- {
- product.CountProduct = 1;
- UserBasket.basket.Add(product);
- break;
- }
- case "Удалить из корзины":
- {
- product.CountProduct = 0;
- UserBasket.basket.Remove(product);
- break;
- }
- }
- }
- if (UserBasket.basket.Count > 0)
- btnBasket.Visibility = Visibility.Visible;
- else
- btnBasket.Visibility = Visibility.Collapsed;
- }
- private void DelteClick(object sender, RoutedEventArgs e)
- {
- if (lastClickItem != "")
- {
- Product product = BaseConnection.kotBase.Product.First(tb => tb.ProductArticleNumber == lastClickItem);
- BaseConnection.kotBase.Product.Remove(product);
- BaseConnection.kotBase.SaveChanges();
- SearchSortFilter();
- }
- }
- private void EditClick(object sender, RoutedEventArgs e)
- {
- Product p = (Product)lvProduct.SelectedItem;
- MainFrame.mainFrame.Navigate(new PageEditProduct(p.ProductArticleNumber));
- }
- private void AddBasketLoaded(object sender, RoutedEventArgs e)
- {
- MenuItem menuItem = (MenuItem)sender;
- if (UserBasket.basket.FirstOrDefault(tb => tb.ProductArticleNumber == lastClickItem) == null)
- menuItem.Header = "Добавить в корзину";
- else
- menuItem.Header = "Удалить из корзины";
- }
- private void AdminLoaded(object sender, RoutedEventArgs e)
- {
- MenuItem menuItem = (MenuItem)sender;
- if (userRole == Role.Admin)
- menuItem.Visibility = Visibility.Visible;
- else
- menuItem.Visibility = Visibility.Collapsed;
- }
- private void ListViewItemMouseDown(object sender, MouseButtonEventArgs e)
- {
- Border border = (Border)sender;
- lastClickItem = border.Uid;
- }
- private void btnBasketClick(object sender, RoutedEventArgs e)
- {
- WindowOrderView window = new WindowOrderView();
- window.ShowDialog();
- if (UserBasket.basket.Count == 0)
- btnBasket.Visibility = Visibility.Collapsed;
- }
- private void btnOrderViewClick(object sender, RoutedEventArgs e)
- {
- MainFrame.mainFrame.Navigate(new PageOrderView());
- }
- private void btnAddProductClick(object sender, RoutedEventArgs e)
- {
- MainFrame.mainFrame.Navigate(new PageEditProduct());
- }
- }
- }
|