using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Metsys.Bson;
using Microsoft.EntityFrameworkCore;
using demo0411.Models;
using System.Collections.Generic;
using System.Linq;

namespace demo0411;

public partial class History : Window
{
    private List<PartnerProduct> partnerProducts;
    private List<Partner> partners;
    private int namePart;
    private int retur;
    public History()
    {
        InitializeComponent();
        partnerProducts = Helper.DbContext.PartnerProducts.Include(c => c.Product).Include(s => s.Partner).ToList();
        partners = Helper.DbContext.Partners.Include(c => c.PartnerType).ToList();
        PartName.ItemsSource = partners.Select(s => s.Name).ToList();
    }
    public History(int log)
    {
        InitializeComponent();
        retur = log;
        partnerProducts = Helper.DbContext.PartnerProducts.Include(c => c.Product).Include(s => s.Partner).ToList();
        partners = Helper.DbContext.Partners.Include(c => c.PartnerType).ToList();
        PartName.ItemsSource = partners.Select(s => s.Name).ToList();
    }
    private void Button_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
    {
        if (retur == 1)
        {
            new ListPartner().Show();
            Close();
        }
        else 
        {
            new ProductList().Show();
            Close();
        }    
    }
    private void ComboBox_SelectionChanged(object? sender, Avalonia.Controls.SelectionChangedEventArgs e)
    {
        namePart = PartName.SelectedIndex;
        namePart++;
        Start(namePart);
    }
    private void Start(int id)
    {
        ListProducts.ItemsSource = partnerProducts.Where(s => s.PartnerId == id);
    }

}