123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- 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;
- using PracticaDemoexam.Model;
- using PracticaDemoexam.Model.ItemModels;
- using PracticaDemoexam.Windows;
- namespace PracticaDemoexam.Pages
- {
- /// <summary>
- /// Логика взаимодействия для Agents.xaml
- /// </summary>
- public partial class Agents : Page
- {
- user34Entities entities = new user34Entities();
- bool _sortDir = true;
- int _pageIndex = 0;
- List<AgentModel> _unchangedAgents = new List<AgentModel>();
- List<AgentModel> _changedAgents = new List<AgentModel>();
- List<List<AgentModel>> Pages = new List<List<AgentModel>> { new List<AgentModel>() };
- List<AgentModel> Page
- {
- get
- {
- return Pages[_pageIndex];
- }
- }
- List<string> PageNumbers = new List<string>();
- List<string> sortingParameter = new List<string>
- {
- "без сортировки",
- "наименование",
- "размер скидки",
- "приоритет"
- };
- List<string> filteringParameter = new List<string>
- {
- "Все типы"
- };
- public Agents()
- {
- InitializeComponent();
- LoadThings();
- lbAgents.ItemsSource = Page;
- lbPogination.ItemsSource = PageNumbers;
- cbSort.ItemsSource = sortingParameter;
- cbFilter.ItemsSource = filteringParameter;
- }
- private void LoadThings()
- {
- AgentModel agentModel;
- int i = 0, j = 0;
- foreach (Agent agent in entities.Agent.ToList())
- {
- agentModel = new AgentModel { Agent = agent };
- _unchangedAgents.Add(agentModel);
- _changedAgents.Add(agentModel);
- Pages[i].Add(agentModel);
- j++;
- if (j == 10)
- {
- PageNumbers.Add((i + 1).ToString());
- i++;
- j = 0;
- Pages.Add(new List<AgentModel>());
- }
- }
- if (Pages.Count > PageNumbers.Count)
- {
- PageNumbers.Add((i + 1).ToString());
- }
- foreach (AgentType type in entities.AgentType.ToList())
- {
- filteringParameter.Add(type.Title);
- }
- }
- private void tbSearch_TextChanged(object sender, TextChangedEventArgs e)
- {
- if (string.IsNullOrEmpty(tbSearch.Text) || string.IsNullOrWhiteSpace(tbSearch.Text))
- {
- _changedAgents = _unchangedAgents;
- }
- else
- {
- _changedAgents = _changedAgents.Where(x => (x.Agent.Title + " " + x.Agent.Email + " " + x.Agent.Phone).Contains(tbSearch.Text)).ToList();
- }
- int i = 0, j = 0;
- Pages.Clear();
- foreach (AgentModel agent in _changedAgents)
- {
- Pages[i].Add(agent);
- j++;
- if (j == 10)
- {
- i++;
- j = 0;
- Pages.Add(new List<AgentModel>());
- }
- }
- }
- private void cbSort_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- Sort();
- }
- private void Sort()
- {
- switch (cbFilter.SelectedIndex)
- {
- case 0:
- if (_sortDir)
- {
- _changedAgents.OrderBy(x => x.Agent.ID);
- }
- else
- {
- _changedAgents.OrderByDescending(x => x.Agent.ID);
- }
- break;
- case 1:
- if (_sortDir)
- {
- _changedAgents.OrderBy(x => x.Agent.Title);
- }
- else
- {
- _changedAgents.OrderByDescending(x => x.Agent.Title);
- }
- break;
- case 2:
- if (_sortDir)
- {
- _changedAgents.OrderBy(x => x.Discount);
- }
- else
- {
- _changedAgents.OrderByDescending(x => x.Discount);
- }
- break;
- case 3:
- if (_sortDir)
- {
- _changedAgents.OrderBy(x => x.Agent.Priority);
- }
- else
- {
- _changedAgents.OrderByDescending(x => x.Agent.Priority);
- }
- break;
- }
- int i = 0, j = 0;
- Pages.Clear();
- foreach (AgentModel agent in _changedAgents)
- {
- Pages[i].Add(agent);
- j++;
- if (j == 10)
- {
- i++;
- j = 0;
- Pages.Add(new List<AgentModel>());
- }
- }
- }
- private void cbFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if ((cbFilter.SelectedValue as string) == "Все типы")
- {
- _changedAgents = _unchangedAgents;
- }
- else
- {
- _changedAgents = _changedAgents.Where(x => x.Agent.AgentType.Title == (cbFilter.SelectedValue as string)).ToList();
- }
- int i = 0, j = 0;
- Pages.Clear();
- foreach (AgentModel agent in _changedAgents)
- {
- Pages[i].Add(agent);
- j++;
- if (j == 10)
- {
- i++;
- j = 0;
- Pages.Add(new List<AgentModel>());
- }
- }
- }
- private void bLeft_Click(object sender, RoutedEventArgs e)
- {
- _pageIndex++;
- if (_pageIndex > Convert.ToInt32(PageNumbers.Last()) - 1)
- {
- _pageIndex = 0;
- }
- lbAgents.ItemsSource = Page;
- }
- private void lbPogination_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- _pageIndex = Convert.ToInt32((lbPogination.SelectedValue as string)) - 1;
- lbAgents.ItemsSource = Page;
- }
- private void bRight_Click(object sender, RoutedEventArgs e)
- {
- _pageIndex--;
- if (_pageIndex < Convert.ToInt32(PageNumbers.First()) - 1)
- {
- _pageIndex = 0;
- }
- lbAgents.ItemsSource = Page;
- }
- private void RadioButton_Click(object sender, RoutedEventArgs e)
- {
- if ((sender as RadioButton).Name == "rbUb")
- {
- _sortDir = false;
- }
- else
- {
- _sortDir = true;
- }
- Sort();
- }
- ChangePriority cp;
- private void lbAgents_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (cp == null)
- {
- cp = new ChangePriority();
- cp.Button_click = Button_Click;
- }
- cp.Show();
- }
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- List<AgentModel> ls = new List<AgentModel>();
- foreach (AgentModel am in lbAgents.SelectedItems)
- {
- ls.Add(am);
- }
- pc = new PriorityChanger(ls);
- pc.Button_click = ChangePriority;
- pc.ShowDialog();
- }
- PriorityChanger pc;
- private void ChangePriority(List<AgentModel> ls, int priority)
- {
- foreach(Agent agent in entities.Agent.ToList())
- {
- if(ls.FirstOrDefault(x=> x.Agent.ID == agent.ID) != null)
- {
- agent.Priority = priority;
- }
- }
- pc.Close();
- entities.SaveChanges();
- LoadThings();
- lbAgents.ItemsSource = null;
- lbPogination.ItemsSource = null;
- cbSort.ItemsSource = null;
- cbFilter.ItemsSource = null;
- lbAgents.ItemsSource = Page;
- lbPogination.ItemsSource = PageNumbers;
- cbSort.ItemsSource = sortingParameter;
- cbFilter.ItemsSource = filteringParameter;
- }
- }
- }
|