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 proverka
{
    /// <summary>
    /// Логика взаимодействия для Baskets.xaml
    /// </summary>
    public partial class Baskets : Page
    {
        List<ClassProduct> product;
        public Baskets(List<ClassProduct> product)
        {
            InitializeComponent();
            this.product = product;
            listOrder.ItemsSource = product;
            List<PointOfIssue> pointOfIssues = ClassBase.BD.PointOfIssue.ToList();
            street.Items.Add("Не выбрано");
            for(int i = 0; i < pointOfIssues.Count; i++)
            {
                street.Items.Add(pointOfIssues[i].Address);
            }
            street.SelectedIndex = 0;
        }

        private void textKolvo_SelectionChanged(object sender, RoutedEventArgs e)
        {
            ClassProduct classProduct = new ClassProduct();
            TextBox textBox=(TextBox)sender;
            string index = textBox.Uid;
            if (index != null)
            {
                if (textBox.Text == "0")
                {
                    classProduct = product.FirstOrDefault(x => x.product.ProductArticleNumber == index);
                    product.Remove(classProduct);
                    if (product.Count > 0)
                    {
                        ClassNavigate.perehod.Navigate(new Baskets(product));
                    }
                    else
                    {
                        Glavnay.basket = null;
                        ClassNavigate.perehod.Navigate(new Glavnay());
                    }
                }
                else
                {
                    try
                    {
                        classProduct = product.FirstOrDefault(x => x.product.ProductArticleNumber == index);
                        classProduct.count = Convert.ToInt32(textBox.Text);
                            }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Ошибка");
                    }
                }
            }
        }

        private void Delet_Click(object sender, RoutedEventArgs e)
        {
            ClassProduct classProduct = new ClassProduct();
            Button textBox = (Button)sender;
            string index = textBox.Uid;
            if (index != null)
            {
                classProduct = product.FirstOrDefault(x => x.product.ProductArticleNumber == index);
                product.Remove(classProduct);
                if (product.Count > 0)
                {
                    ClassNavigate.perehod.Navigate(new Baskets(product));
                }
                else
                {
                    Glavnay.basket = null;
                    ClassNavigate.perehod.Navigate(new Glavnay());
                }
            }
        }

      

        private void nazad_Click(object sender, RoutedEventArgs e)
        {
           if (product.Count > 0)
                    {
                Glavnay.basket = product;
                ClassNavigate.perehod.Navigate(new Glavnay());
            }
                    else
                    {
                        Glavnay.basket = null;
                        ClassNavigate.perehod.Navigate(new Glavnay());
                    }
        }

        private void oformit_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
