using System;
using System.Collections;
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 ShabaninDemoEkz
{
    /// <summary>
    /// Логика взаимодействия для AgentPage.xaml
    /// </summary>
    public partial class AgentPage : Page
    {
        VModel VM = new VModel();
        List<Agent> agents;
        public AgentPage()
        {
            InitializeComponent();
            DataContext = VM;
            agents = VM.agent;
            AgentList.ItemsSource = agents;
            SortCMB.SelectedIndex = 0;

            FilterCmb.ItemsSource = BaseConnect.BaseModel.AgentType.ToList();
            FilterCmb.SelectedValuePath = "ID";
            FilterCmb.DisplayMemberPath = "Title";
            FilterCmb.SelectedIndex = 0;

        }

        private void FilterCmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Filter();
        }

        private void SortCMB_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Filter();
        }

        public void Filter()
        {
            agents = VM.agent;
            try
            {
                switch (SortCMB.SelectedIndex)
                {
                    case 0:

                        break;
                    case 1:
                        agents = agents.OrderBy(x => x.Title).ToList();
                        break;
                    case 2:
                        agents = agents.OrderBy(x => x.Title).ToList();
                        agents.Reverse();
                        break;
                    case 3:
                        agents = agents.OrderBy(x => x.skidka).ToList();
                        break;
                    case 4:
                        agents = agents.OrderBy(x => x.skidka).ToList();
                        agents.Reverse();
                        break;
                    case 5:
                        agents = agents.OrderBy(x => x.Priority).ToList();
                        break;
                    case 6:
                        agents = agents.OrderBy(x => x.Priority).ToList();
                        agents.Reverse();
                        break;
                }
            }
            catch
            {

            }

            try
            {
                if (FilterCmb.SelectedIndex != 0)
                    agents = agents.Where(x => x.AgentTypeID == FilterCmb.SelectedIndex).ToList();
            }
            catch { }


            try
            {
                agents = agents.Where(x => x.Title.Contains(searchBar.Text) || x.Email.Contains(searchBar.Text) || x.Phone.Contains(searchBar.Text)).ToList();
            }
            catch { }
            AgentList.ItemsSource = agents;
        }

        private void searchBar_TextChanged(object sender, TextChangedEventArgs e)
        {
            Filter();
        }

        private void AgentList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (AgentList.SelectedItems.Count > 1)
            {
                ChangePrior.Visibility = Visibility.Visible;
                
            }
            else
            {

                ChangePrior.Visibility = Visibility.Collapsed;
            }
        }

        private void ChangePrior_Click(object sender, RoutedEventArgs e)
        {
            IList ls = AgentList.SelectedItems;
            new ChangedPrior(ls).ShowDialog();
        }
    }
}
