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 WpfApp1.Pages
{
    /// <summary>
    /// Логика взаимодействия для AgentsList.xaml
    /// </summary>
    public partial class AgentsList : Page
    {
        List<Agent> AgentStart = DataBase.DB.Agent.ToList();
        public AgentsList()
        {
            InitializeComponent();
            Agents.ItemsSource = AgentStart;
            CbFilt.Items.Add("Все типы");
            List<AgentType> at = DataBase.DB.AgentType.ToList();
            for (int i = 0; i < at.Count; i++)
            {
                CbFilt.Items.Add(at[i].Title);
            }
            CbFilt.SelectedIndex = 0;
        }
        List<Agent> AgFilter = new List<Agent>();
        List<Agent> AgSearch = new List<Agent>();
        private void CbFilt_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void CbSort_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
        private void FilterSort()
        {
            int filterIndex = CbFilt.SelectedIndex;
            if (TbSearch.Text != string.Empty)
            {
                if (filterIndex != 0)
                {
                AgFilter = AgSearch.Where(x => x.AgentTypeID == filterIndex).ToList();
                }
            else
            {
                AgFilter = AgSearch;
            }
        }
           else
            {
                if (filterIndex !=0)
                {
                    AgFilter = AgentStart.Where(x => x.AgentTypeID == filterIndex).ToList();
                }
                else
                {
                    AgFilter = AgentStart;
                }
            }
        } 

        private void TbSearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (TbSearch.Text != String.Empty)
            {
                AgSearch = AgentStart.Where(x => x.Title.Contains(TbSearch.Text)).ToList();
                FilterSort();
            }
            else
            {
                FilterSort();
            }
        }
    }
}
