using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
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.Shapes;
namespace Eight
{
///
/// Логика взаимодействия для UpdateProductWindow.xaml
///
public partial class UpdateProductWindow : Window
{
private string _path;
public UpdateProductWindow(Product product)
{
InitializeComponent();
Load();
DataContext = product;
foreach(var productMaterial in product.ProductMaterial)
{
BasketBox.Items.Add(productMaterial);
}
_path = product.ImagePath;
Img.Source = GetBitmapImage(_path);
}
private BitmapImage GetBitmapImage(string path)
{
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.UriSource = new Uri(path);
bitmapImage.EndInit();
return bitmapImage;
}
private void Load()
{
CmbType.ItemsSource = Helper.GetContext().ProductType.ToList();
ProductsBox.ItemsSource = Helper.GetContext().Material.ToList();
}
private void BtnChoose_Click(object sender, RoutedEventArgs e)
{
try
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == true)
{
var fileName = openFileDialog.FileName;
_path = "\\products\\" + openFileDialog.SafeFileName;
File.Copy(fileName, Environment.CurrentDirectory + "\\products\\" + openFileDialog.SafeFileName, true);
Img.Source = GetBitmapImage(Environment.CurrentDirectory + "\\products\\" + openFileDialog.SafeFileName);
}
}
catch (Exception)
{
return;
}
}
private void BtnAdd_Click(object sender, RoutedEventArgs e)
{
if (ProductsBox.SelectedItem is Material material)
{
var materialDuplicate = BasketBox.Items.Cast().ToList().SingleOrDefault(x => x.Material == material);
if (materialDuplicate == null)
{
BasketBox.Items.Add(new ProductMaterial() { Material = material, Count = 1 });
}
else
{
materialDuplicate.Count++;
}
BasketBox.Items.Refresh();
}
}
private void BtnRemove_Click(object sender, RoutedEventArgs e)
{
if (BasketBox.SelectedItem is ProductMaterial productMaterial)
{
if (productMaterial.Count == 1)
{
BasketBox.Items.Remove(productMaterial);
}
else
{
productMaterial.Count--;
}
BasketBox.Items.Refresh();
}
}
private void BtnSubmit_Click(object sender, RoutedEventArgs e)
{
var product = DataContext as Product;
var productMaterials = BasketBox.Items.Cast().ToList();
product.Image = _path;
product.ProductMaterial = productMaterials;
Helper.GetContext().SaveChanges();
Close();
}
private void BtnDelete_Click(object sender, RoutedEventArgs e)
{
Helper.GetContext().Product.Remove(DataContext as Product);
Helper.GetContext().SaveChanges();
Close();
}
}
}