Agents.xaml.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. using PracticaDemoexam.Model;
  18. using PracticaDemoexam.Model.ItemModels;
  19. using PracticaDemoexam.Windows;
  20. namespace PracticaDemoexam.Pages
  21. {
  22. /// <summary>
  23. /// Логика взаимодействия для Agents.xaml
  24. /// </summary>
  25. public partial class Agents : Page
  26. {
  27. user34Entities entities = new user34Entities();
  28. bool _sortDir = true;
  29. int _pageIndex = 0;
  30. List<AgentModel> _unchangedAgents = new List<AgentModel>();
  31. List<AgentModel> _changedAgents = new List<AgentModel>();
  32. List<List<AgentModel>> Pages = new List<List<AgentModel>> { new List<AgentModel>() };
  33. List<AgentModel> Page
  34. {
  35. get
  36. {
  37. return Pages[_pageIndex];
  38. }
  39. }
  40. List<string> PageNumbers = new List<string>();
  41. List<string> sortingParameter = new List<string>
  42. {
  43. "без сортировки",
  44. "наименование",
  45. "размер скидки",
  46. "приоритет"
  47. };
  48. List<string> filteringParameter = new List<string>
  49. {
  50. "Все типы"
  51. };
  52. public Agents()
  53. {
  54. InitializeComponent();
  55. LoadThings();
  56. lbAgents.ItemsSource = Page;
  57. lbPogination.ItemsSource = PageNumbers;
  58. cbSort.ItemsSource = sortingParameter;
  59. cbFilter.ItemsSource = filteringParameter;
  60. }
  61. private void LoadThings()
  62. {
  63. AgentModel agentModel;
  64. int i = 0, j = 0;
  65. foreach (Agent agent in entities.Agent.ToList())
  66. {
  67. agentModel = new AgentModel { Agent = agent };
  68. _unchangedAgents.Add(agentModel);
  69. _changedAgents.Add(agentModel);
  70. Pages[i].Add(agentModel);
  71. j++;
  72. if (j == 10)
  73. {
  74. PageNumbers.Add((i + 1).ToString());
  75. i++;
  76. j = 0;
  77. Pages.Add(new List<AgentModel>());
  78. }
  79. }
  80. if (Pages.Count > PageNumbers.Count)
  81. {
  82. PageNumbers.Add((i + 1).ToString());
  83. }
  84. foreach (AgentType type in entities.AgentType.ToList())
  85. {
  86. filteringParameter.Add(type.Title);
  87. }
  88. }
  89. private void tbSearch_TextChanged(object sender, TextChangedEventArgs e)
  90. {
  91. if (string.IsNullOrEmpty(tbSearch.Text) || string.IsNullOrWhiteSpace(tbSearch.Text))
  92. {
  93. _changedAgents = _unchangedAgents;
  94. }
  95. else
  96. {
  97. _changedAgents = _changedAgents.Where(x => (x.Agent.Title + " " + x.Agent.Email + " " + x.Agent.Phone).Contains(tbSearch.Text)).ToList();
  98. }
  99. int i = 0, j = 0;
  100. Pages.Clear();
  101. foreach (AgentModel agent in _changedAgents)
  102. {
  103. Pages[i].Add(agent);
  104. j++;
  105. if (j == 10)
  106. {
  107. i++;
  108. j = 0;
  109. Pages.Add(new List<AgentModel>());
  110. }
  111. }
  112. }
  113. private void cbSort_SelectionChanged(object sender, SelectionChangedEventArgs e)
  114. {
  115. Sort();
  116. }
  117. private void Sort()
  118. {
  119. switch (cbFilter.SelectedIndex)
  120. {
  121. case 0:
  122. if (_sortDir)
  123. {
  124. _changedAgents.OrderBy(x => x.Agent.ID);
  125. }
  126. else
  127. {
  128. _changedAgents.OrderByDescending(x => x.Agent.ID);
  129. }
  130. break;
  131. case 1:
  132. if (_sortDir)
  133. {
  134. _changedAgents.OrderBy(x => x.Agent.Title);
  135. }
  136. else
  137. {
  138. _changedAgents.OrderByDescending(x => x.Agent.Title);
  139. }
  140. break;
  141. case 2:
  142. if (_sortDir)
  143. {
  144. _changedAgents.OrderBy(x => x.Discount);
  145. }
  146. else
  147. {
  148. _changedAgents.OrderByDescending(x => x.Discount);
  149. }
  150. break;
  151. case 3:
  152. if (_sortDir)
  153. {
  154. _changedAgents.OrderBy(x => x.Agent.Priority);
  155. }
  156. else
  157. {
  158. _changedAgents.OrderByDescending(x => x.Agent.Priority);
  159. }
  160. break;
  161. }
  162. int i = 0, j = 0;
  163. Pages.Clear();
  164. foreach (AgentModel agent in _changedAgents)
  165. {
  166. Pages[i].Add(agent);
  167. j++;
  168. if (j == 10)
  169. {
  170. i++;
  171. j = 0;
  172. Pages.Add(new List<AgentModel>());
  173. }
  174. }
  175. }
  176. private void cbFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
  177. {
  178. if ((cbFilter.SelectedValue as string) == "Все типы")
  179. {
  180. _changedAgents = _unchangedAgents;
  181. }
  182. else
  183. {
  184. _changedAgents = _changedAgents.Where(x => x.Agent.AgentType.Title == (cbFilter.SelectedValue as string)).ToList();
  185. }
  186. int i = 0, j = 0;
  187. Pages.Clear();
  188. foreach (AgentModel agent in _changedAgents)
  189. {
  190. Pages[i].Add(agent);
  191. j++;
  192. if (j == 10)
  193. {
  194. i++;
  195. j = 0;
  196. Pages.Add(new List<AgentModel>());
  197. }
  198. }
  199. }
  200. private void bLeft_Click(object sender, RoutedEventArgs e)
  201. {
  202. _pageIndex++;
  203. if (_pageIndex > Convert.ToInt32(PageNumbers.Last()) - 1)
  204. {
  205. _pageIndex = 0;
  206. }
  207. lbAgents.ItemsSource = Page;
  208. }
  209. private void lbPogination_SelectionChanged(object sender, SelectionChangedEventArgs e)
  210. {
  211. _pageIndex = Convert.ToInt32((lbPogination.SelectedValue as string)) - 1;
  212. lbAgents.ItemsSource = Page;
  213. }
  214. private void bRight_Click(object sender, RoutedEventArgs e)
  215. {
  216. _pageIndex--;
  217. if (_pageIndex < Convert.ToInt32(PageNumbers.First()) - 1)
  218. {
  219. _pageIndex = 0;
  220. }
  221. lbAgents.ItemsSource = Page;
  222. }
  223. private void RadioButton_Click(object sender, RoutedEventArgs e)
  224. {
  225. if ((sender as RadioButton).Name == "rbUb")
  226. {
  227. _sortDir = false;
  228. }
  229. else
  230. {
  231. _sortDir = true;
  232. }
  233. Sort();
  234. }
  235. ChangePriority cp;
  236. private void lbAgents_SelectionChanged(object sender, SelectionChangedEventArgs e)
  237. {
  238. if (cp == null)
  239. {
  240. cp = new ChangePriority();
  241. cp.Button_click = Button_Click;
  242. }
  243. cp.Show();
  244. }
  245. private void Button_Click(object sender, RoutedEventArgs e)
  246. {
  247. List<AgentModel> ls = new List<AgentModel>();
  248. foreach (AgentModel am in lbAgents.SelectedItems)
  249. {
  250. ls.Add(am);
  251. }
  252. pc = new PriorityChanger(ls);
  253. pc.Button_click = ChangePriority;
  254. pc.ShowDialog();
  255. }
  256. PriorityChanger pc;
  257. private void ChangePriority(List<AgentModel> ls, int priority)
  258. {
  259. foreach(Agent agent in entities.Agent.ToList())
  260. {
  261. if(ls.FirstOrDefault(x=> x.Agent.ID == agent.ID) != null)
  262. {
  263. agent.Priority = priority;
  264. }
  265. }
  266. pc.Close();
  267. entities.SaveChanges();
  268. LoadThings();
  269. lbAgents.ItemsSource = null;
  270. lbPogination.ItemsSource = null;
  271. cbSort.ItemsSource = null;
  272. cbFilter.ItemsSource = null;
  273. lbAgents.ItemsSource = Page;
  274. lbPogination.ItemsSource = PageNumbers;
  275. cbSort.ItemsSource = sortingParameter;
  276. cbFilter.ItemsSource = filteringParameter;
  277. }
  278. }
  279. }