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 WhiteList
{
    /// <summary>
    /// Логика взаимодействия для MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        List<Agent> _listAgents;
        public MainWindow()
        {
            InitializeComponent();
            Filt();
        }

        public void Filt() //фильтрация данных
        {
            
            _listAgents = DB.dbCon.Agent.ToList();
            if(TextBoxSearch.Text.Length>0) //по поисковику
            { 
                string searchStroke = TextBoxSearch.Text.ToLower();
                List<Agent> newList = new List<Agent>();
                foreach(Agent a in _listAgents)
                {
                    if(a.Title.ToLower().Contains(searchStroke)||a.Email.ToLower().Contains(searchStroke)||a.Phone.ToLower().Contains(searchStroke)) //содержится в почте, названии или номере телефона
                    {
                        newList.Add(a);
                    }
                }
                _listAgents = newList;
            }


            ListViewDataBase.Items.Clear();
            foreach (Agent a in _listAgents)
            {
                ListViewDataBase.Items.Add(a);
            }

        }

        private void TextBoxSearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            Filt();
        }

        private void ComboBoxFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Filt();
        }

        private void ChangePriority_Click(object sender, RoutedEventArgs e)
        {
            List<Agent> selected = new List<Agent>();
            foreach(Agent a in ListViewDataBase.SelectedItems) //получаем из выделенных итемов обьекты таблицы
            {
                selected.Add(a);
            }
            ChangePriority w = new ChangePriority(selected);
            w.Owner = this;
            w.Visibility = Visibility.Visible;
            w.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            w.ShowDialog();
            ListViewDataBase.Items.Refresh(); //перезагрузка

        }

        private void ListViewDataBase_SelectionChanged(object sender, SelectionChangedEventArgs e) //для видимости кнопки
        {
            if(ListViewDataBase.SelectedItems.Count>0)
            {
                BtnChangePriority.Visibility = Visibility.Visible;
            }
            else
            {
                BtnChangePriority.Visibility = Visibility.Hidden;
            }
        }
    }
}
