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 ProbnikDemo
{
    /// <summary>
    /// Логика взаимодействия для PgListAgent.xaml
    /// </summary>
    public partial class PgListAgent : Page
    {
        Agent agent = new Agent();
        List<Agent> BufListAgent = new List<Agent>();
        


        public PgListAgent()
        {
            InitializeComponent();
            ListAgents.ItemsSource = agent.Agents();

            BufListAgent = agent.Agents();
            ComboFilter.ItemsSource = BaseConnect.BaseModel.AgentType.ToList();
            ComboFilter.SelectedValuePath = "ID";
            ComboFilter.DisplayMemberPath = "Title";
            ComboFilter.SelectedIndex = 0;
        }

        private void Filter(object sender, RoutedEventArgs e)
        {
            try
            {
                BufListAgent = agent.Agents();
                if (TBFtind.Text != "")
                {
                    BufListAgent = BufListAgent.Where(x => x.Email.Contains(TBFtind.Text) || x.Phone.Contains(TBFtind.Text)).ToList();
                }
                else BufListAgent = agent.Agents();

                switch (ComboSort.SelectedIndex)
                {
                    case 0:
                        BufListAgent = BufListAgent.OrderBy(x => x.Title).ToList();
                        break;
                    case 1:
                        BufListAgent = BufListAgent.OrderBy(x => x.Title).ToList();
                        BufListAgent.Reverse();
                        break;
                    case 2:
                        BufListAgent = BufListAgent.OrderBy(x => x.Priority).ToList();
                        break;
                    case 3:
                        BufListAgent = BufListAgent.OrderBy(x => x.Priority).ToList();
                        BufListAgent.Reverse();
                        break;
                }


               
                if (Convert.ToInt32(ComboFilter.SelectedValue) == 0)
                {
                    BufListAgent = agent.Agents();
                }
                else BufListAgent = BufListAgent.Where(x => x.AgentTypeID == Convert.ToInt32(ComboFilter.SelectedValue)).ToList();


                ListAgents.ItemsSource = BufListAgent;
                ListAgents.Items.Refresh();
            }
            catch
            {

            }

            }

        private void BtnGoAdd_Click(object sender, RoutedEventArgs e)
        {
            new AddAgent().ShowDialog();
        }

        private void StackPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            StackPanel stackPanel = (StackPanel)sender;
            int id = Convert.ToInt32(stackPanel.Uid);
            new PgChange(BaseConnect.BaseModel.Agent.FirstOrDefault(x => x.ID == id)).ShowDialog() ;


               
        }
    }
    }
