using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace TestDemo
{
///
/// Логика взаимодействия для WinCart.xaml
///
public partial class WinCart : Window
{
List Knigi = new List();
public decimal Sale { get; set; }
decimal sale;
public WinCart(List books, decimal sale)
{
try
{
InitializeComponent();
this.sale = sale;
Knigi = books;
foreach (books books1 in Knigi)
{
if (books1.CountInCart >= 2)
{
books1.VisibleMainPrice = "Visible";
}
if (sale != 0)
{
books1.TextDecor = "Strikethrough";
books1.OldPrice = (decimal)books1.CountInCart * (decimal)books1.price;
books1.NewPrice = books1.OldPrice - (books1.OldPrice * sale / 100);
books1.VisibleNewPrice = "Visible";
}
else
{
books1.OldPrice = (decimal)books1.CountInCart * (decimal)books1.price;
}
}
ListCart.ItemsSource = Knigi;
ListCart.Items.Refresh(); ;
}
catch
{
}
}
private void BtnClearAllList_Click(object sender, RoutedEventArgs e)
{
try
{
ListCart.ItemsSource = null;
this.Close();
LoadPages.SwitchPages.Navigate(new PgBooks());
}
catch
{
}
}
private void BtnPlus_Click(object sender, RoutedEventArgs e)
{
try
{
Button button = (Button)sender;
int id = Convert.ToInt32(button.Uid);
books book = BaseModel.BaseConnect.books.FirstOrDefault(x => x.id_book == id);
book.CountInCart++;
if (book.CountInCart > (book.count_in_shop + book.count_in_stock))
{
MessageBox.Show("Превышен лимит товара");
book.CountInCart--;
}
ListCart.Items.Refresh();
}
catch
{
}
}
private void BtnMinus_Click(object sender, RoutedEventArgs e)
{
try
{
Button button = (Button)sender;
int id = Convert.ToInt32(button.Uid);
books book = BaseModel.BaseConnect.books.FirstOrDefault(x => x.id_book == id);
book.CountInCart--;
if (book.CountInCart < 1)
{
MessageBox.Show("Количество товаров не может быть меньше 0");
book.CountInCart++;
}
ListCart.Items.Refresh();
if (ListCart.Items == null)
BtnZakazat.IsEnabled = false;
}
catch
{
}
}
private void BtnDelete_Click(object sender, RoutedEventArgs e)
{
try
{
Button button = (Button)sender;
int id = Convert.ToInt32(button.Uid);
books book = BaseModel.BaseConnect.books.FirstOrDefault(x => x.id_book == id);
Knigi.Remove(book);
ListCart.Items.Refresh();
}
catch
{
}
}
private void BtnZakazat_Click(object sender, RoutedEventArgs e)
{
try
{
List books = (List)ListCart.ItemsSource;
decimal totalprice = 0;
decimal totalpricenonsale = 0;
int count = 0;
string ZabratTovar = "";
foreach (books books1 in books)
{
if (sale == 0)
{
totalprice += (decimal)books1.price * books1.CountInCart;
}
else
{
totalprice += books1.NewPrice;
}
totalpricenonsale += (decimal)books1.price * books1.CountInCart;
count += books1.CountInCart;
if (books1.count_in_shop >= books1.CountInCart)
{
ZabratTovar = "Сейчас";
}
else
ZabratTovar = DateTime.Now.AddDays(3).AddHours(9).ToUniversalTime().ToString();
}
foreach (books books1 in books)
{
if (books1.count_in_shop > books1.CountInCart)
{
books1.count_in_shop -= books1.CountInCart;
}
else
{
int minus = books1.count_in_shop - books1.CountInCart;
books1.count_in_shop -= books1.CountInCart;
if (minus < 0)
{
books1.count_in_shop = 0;
books1.count_in_stock -= minus * -1;
}
}
}
BaseModel.BaseConnect.SaveChanges();
orders order = new orders() { sale = (int)sale, total_price = totalprice, count_book = count, date_reserve = DateTime.Now.AddDays(7) };
BaseModel.BaseConnect.orders.Add(order);
BaseModel.BaseConnect.SaveChanges();
orders ordercur = BaseModel.BaseConnect.orders.FirstOrDefault(x => x.total_price == totalprice && x.sale == (int)sale && x.count_book == count);
ListCart.Visibility = Visibility.Collapsed;
StackInfo.Visibility = Visibility.Visible;
TxtCount.Text = order.count_book.ToString();
TxtDateZakaza.Text = ZabratTovar;
TxtTotalPrice.Text = order.total_price.ToString("0.00");
TxtSale.Text = order.sale.ToString();
TxtNumber.Text = order.id_zakaza.ToString();
TxtDateReserv.Text = order.date_reserve.ToShortDateString();
TxtTotalPriceNonSale.Text = totalpricenonsale.ToString("0.00");
Knigi.Clear();
LoadPages.SwitchPages.Navigate(new PgBooks());
BtnZakazat.IsEnabled = false;
ListCart.ItemsSource = null;
ListCart.Items.Refresh();
}
catch
{
}
}
private void BtnGoBack_Click(object sender, RoutedEventArgs e)
{
try
{
this.Close();
LoadPages.SwitchPages.Navigate(new PgBooks());
}
catch
{
}
}
}
}