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;
namespace Практическая__5
{
///
/// Логика взаимодействия для JewelryMain.xaml
///
public partial class JewelryMain : Page
{
public JewelryMain()
{
InitializeComponent();
init();
}
private void init()
{
JewelryList.ItemsSource = BaseConnect.kotBase.tbJewelry.ToList();
List typeJewelry = BaseConnect.kotBase.tbTypeJewelry.ToList();
tbTypeJewelry all = new tbTypeJewelry()
{
idType = 0,
nameType = "Все типы"
};
CBType.Items.Add(all);
foreach (tbTypeJewelry type in typeJewelry)
{
CBType.Items.Add(type);
}
CBType.DisplayMemberPath = "nameType";
CBType.SelectedValuePath = "idType";
}
private void CompositionJewelryLoaded(object sender, RoutedEventArgs e)
{
TextBlock tbk = (TextBlock)sender;
int index = Convert.ToInt32(tbk.Uid);
List list = BaseConnect.kotBase.tbCompositionJewelry.Where(tb => tb.idJewelry == index).ToList();
if (list.Count > 0)
{
string value = "";
foreach (tbCompositionJewelry a in list)
{
value += a.tbBatchMaterial.tbMaterial.nameMaterial + ", ";
}
value = value.Remove(value.Length - 2);
tbk.Text = value;
}
else tbk.Text = "Состовляющие украшения неизвестны";
}
private void OtherPriceLoaded(object sender, RoutedEventArgs e)
{
TextBlock tbk = (TextBlock)sender;
int index = Convert.ToInt32(tbk.Uid);
List list = BaseConnect.kotBase.tbCompositionJewelry.Where(tb => tb.idJewelry == index).ToList();
if (list.Count > 0)
{
double sum = 0;
foreach (tbCompositionJewelry a in list)
{
tbBatchMaterial price = BaseConnect.kotBase.tbBatchMaterial.Where(tb => tb.idBatch == a.idBatch).ToList().Last();
if (price != null)
{
sum += price.price * a.count;
}
}
tbk.Text = "Себестоимость изделия " + sum + " тыс. руб";
}
}
private void BTBackClick(object sender, RoutedEventArgs e)
{
FrameClass.frameMain.NavigationService.Navigate(new AdminMain());
}
private void BTAddClick(object sender, RoutedEventArgs e)
{
FrameClass.frameMain.NavigationService.Navigate(new JewelryAdd());
}
private void BtnDelJewelryClick(object sender, RoutedEventArgs e)
{
Button btn = (Button)sender;
int index = Convert.ToInt32(btn.Uid);
tbJewelry thisJewelry = BaseConnect.kotBase.tbJewelry.Where(tb => tb.idJewelry == index).ToList().LastOrDefault();
MessageBoxResult result = MessageBox.Show("Вы действительно хотите Удалить это украшение?", "Удалить украшение", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
BaseConnect.kotBase.tbJewelry.Remove(thisJewelry);
BaseConnect.kotBase.SaveChanges();
FrameClass.frameMain.NavigationService.Navigate(new JewelryMain());
MessageBox.Show("Украшение успешно удалено!");
}
}
private void BtnEditJewelryClick(object sender, RoutedEventArgs e)
{
Button btn = (Button)sender;
int index = Convert.ToInt32(btn.Uid);
MessageBoxResult result = MessageBox.Show("Вы действительно хотите Отредактировать это украшение?", "Редактировать информацию об украшении", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
FrameClass.frameMain.NavigationService.Navigate(new JewelryAdd(index));
}
}
private void BTFilterClick(object sender, RoutedEventArgs e)
{
if (GFilter.Visibility == Visibility.Visible)
{
GFilter.Visibility = Visibility.Collapsed;
}
else
{
GFilter.Visibility = Visibility.Visible;
}
}
private void Filter()
{
List listJewelry = new List();
//тип украшения
tbTypeJewelry thisType = (tbTypeJewelry)CBType.SelectedItem;
if(CBType.SelectedIndex != 0)
{
listJewelry = BaseConnect.kotBase.tbJewelry.Where(tb => tb.tbTypeJewelry.nameType == thisType.nameType).ToList();
}
else
{
listJewelry = BaseConnect.kotBase.tbJewelry.ToList();
}
//поиск по названию
if (!string.IsNullOrWhiteSpace(TBSearchName.Text)) // если строка не пустая и если она не состоит из пробелов
{
listJewelry = listJewelry.Where(tb => tb.name.ToLower().Contains(TBSearchName.Text.ToLower())).ToList();
}
//наличие в магазине
if (CBAvailability.IsChecked == true)
{
List OrderItems = BaseConnect.kotBase.tbOrderItems.ToList();
List newList = new List();
foreach(tbJewelry jewelry in listJewelry)
{
tbOrderItems availability = OrderItems.Where(tb => tb.idJewelry == jewelry.idJewelry).FirstOrDefault();
if (availability == null)
{
newList.Add(jewelry);
}
}
listJewelry = newList;
}
//сортировка
if(RBPriceUp.IsChecked == true)
{
listJewelry.Sort((tb, tb2) => tb.price.CompareTo(tb2.price));
}
if(RBPriceDown.IsChecked == true)
{
listJewelry.Sort((tb, tb2) => tb.price.CompareTo(tb2.price));
listJewelry.Reverse();
}
//запись о количестве используемых материалов
foreach(tbJewelry element in listJewelry)
{
List composition = BaseConnect.kotBase.tbCompositionJewelry.Where(tb => tb.idJewelry == element.idJewelry).ToList();
if(composition != null)
{
element.count = composition.Count;
}
else
{
element.count = 0;
}
}
if(RBCountUp.IsChecked == true)
{
listJewelry.Sort((tb, tb2) => tb.count.CompareTo(tb2.count));
}
if(RBCountDown.IsChecked == true)
{
listJewelry.Sort((tb, tb2) => tb.count.CompareTo(tb2.count));
listJewelry.Reverse();
}
JewelryList.ItemsSource = listJewelry;
}
private void CountChecked(object sender, RoutedEventArgs e)
{
Filter();
}
private void PriceChecked(object sender, RoutedEventArgs e)
{
Filter();
}
private void AvailabilityChecked(object sender, RoutedEventArgs e)
{
Filter();
}
private void AvailabilityUnchecked(object sender, RoutedEventArgs e)
{
Filter();
}
private void CBTypeSelectionChanged(object sender, SelectionChangedEventArgs e)
{
Filter();
}
private void TBSearchNameTextChanged(object sender, TextChangedEventArgs e)
{
Filter();
}
}
}