NotificationForm.xaml.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Media;
  7. using Comal.Classes;
  8. using InABox.Clients;
  9. using InABox.Core;
  10. using InABox.Wpf;
  11. using InABox.WPF;
  12. using Syncfusion.Windows.Tools.Controls;
  13. namespace PRSDesktop
  14. {
  15. /// <summary>
  16. /// Interaction logic for NotificationForm.xaml
  17. /// </summary>
  18. public partial class NotificationForm : ThemableWindow
  19. {
  20. private CoreTable Employees;
  21. private CoreTable Jobs;
  22. private CoreTable Teams;
  23. public NotificationForm()
  24. {
  25. InitializeComponent();
  26. ContentEdit.SetColor(Colors.LightYellow);
  27. }
  28. public string Subject
  29. {
  30. get => TitleEdit.Text;
  31. set => TitleEdit.Text = value;
  32. }
  33. public string Description
  34. {
  35. get => ContentEdit.Text;
  36. set => ContentEdit.Text = value;
  37. }
  38. public Guid Recipient { get; set; }
  39. public Guid JobID { get; set; }
  40. public void OnLoaded(object sender, RoutedEventArgs e)
  41. {
  42. new Client<Employee>().Query(
  43. LookupFactory.DefineFilter<Employee>(),
  44. LookupFactory.DefineColumns<Employee>(),
  45. LookupFactory.DefineSort<Employee>(),
  46. (table, exception) =>
  47. {
  48. Employees = table;
  49. Dispatcher.Invoke(() => { ReloadEmployees(Guid.Empty); });
  50. }
  51. );
  52. new Client<EmployeeTeam>().Query(
  53. null,
  54. null,
  55. new SortOrder<EmployeeTeam>(x => x.TeamLink.Name),
  56. (table, exception) =>
  57. {
  58. Teams = table;
  59. Dispatcher.Invoke(() => { ReloadTeams(); });
  60. }
  61. );
  62. new Client<Job>().Query(
  63. LookupFactory.DefineFilter<Job>(),
  64. LookupFactory.DefineColumns<Job>(),
  65. LookupFactory.DefineSort<Job>(),
  66. (table, exception) =>
  67. {
  68. Jobs = table;
  69. Dispatcher.Invoke(() => { ReloadJobs(); });
  70. }
  71. );
  72. }
  73. private void ReloadTeams()
  74. {
  75. var lookups = new Dictionary<Guid, string>
  76. {
  77. { Guid.Empty, "" }
  78. };
  79. foreach (var row in Teams.Rows)
  80. lookups[row.Get<EmployeeTeam, Guid>(x => x.TeamLink.ID)] = row.Get<EmployeeTeam, string>(x => x.TeamLink.Name);
  81. TeamCombo.ItemsSource = null;
  82. TeamCombo.ItemsSource = lookups;
  83. }
  84. private void ReloadEmployees(Guid teamid)
  85. {
  86. var lookups = new Dictionary<Guid, string>();
  87. foreach (var row in Employees.Rows)
  88. {
  89. var bInTeam = Teams == null || teamid == Guid.Empty || Teams.Rows.Any(r =>
  90. r.Get<EmployeeTeam, Guid>(x => x.TeamLink.ID).Equals(teamid) &&
  91. r.Get<EmployeeTeam, Guid>(x => x.EmployeeLink.ID).Equals(row.Get<Employee, Guid>(x => x.ID)));
  92. if (bInTeam)
  93. lookups[row.Get<Employee, Guid>(x => x.ID)] = row.Get<Employee, string>(x => x.Name);
  94. }
  95. EmployeeChecks.ItemsSource = null;
  96. EmployeeChecks.ItemsSource = lookups;
  97. if (Recipient != Guid.Empty)
  98. foreach (KeyValuePair<Guid, string> pair in EmployeeChecks.Items)
  99. if (pair.Key == Recipient)
  100. {
  101. EmployeeChecks.SelectedItem = pair;
  102. EmployeeChecks.SelectedItems.Add(pair);
  103. ReloadNames();
  104. break;
  105. }
  106. }
  107. private void ReloadJobs()
  108. {
  109. var lookups = new Dictionary<Guid, string> { { Guid.Empty, "(No Job Selected)" } };
  110. foreach (var row in Jobs.Rows)
  111. lookups[row.Get<Job, Guid>(x => x.ID)] =
  112. string.Format("{0}: {1}", row.Get<Job, string>(x => x.JobNumber), row.Get<Job, string>(x => x.Name));
  113. JobDetails.ItemsSource = null;
  114. JobDetails.ItemsSource = lookups;
  115. foreach (KeyValuePair<Guid, string> pair in JobDetails.Items)
  116. if (pair.Key == JobID)
  117. {
  118. JobDetails.SelectedItem = pair;
  119. break;
  120. }
  121. }
  122. // private void CheckOKButton(object sender, EventArgs e)
  123. // {
  124. // OK.IsEnabled = (EmployeeChecks.SelectedItems.Count > 0) && !String.IsNullOrWhiteSpace(TitleEdit.Text) && !String.IsNullOrWhiteSpace(ContentEdit.Text);
  125. // }
  126. private void CheckOKButton(object sender, SelectionChangedEventArgs e)
  127. {
  128. CheckOK(sender);
  129. ReloadNames();
  130. }
  131. // private void CheckOKButton(object sender, TextChangedEventArgs e)
  132. // {
  133. // OK.IsEnabled = (EmployeeChecks.SelectedItems.Count > 0) && !String.IsNullOrWhiteSpace(TitleEdit.Text) && !String.IsNullOrWhiteSpace(ContentEdit.Text);
  134. // }
  135. private void CheckOK(object sender)
  136. {
  137. OK.IsEnabled = EmployeeChecks.SelectedItems.Count > 0 && !string.IsNullOrWhiteSpace(TitleEdit.Text) &&
  138. !string.IsNullOrWhiteSpace(ContentEdit.Text);
  139. }
  140. private void EmployeeChecks_ItemChecked(object sender, ItemCheckedEventArgs e)
  141. {
  142. OK.IsEnabled = EmployeeChecks.SelectedItems.Count > 0 && !string.IsNullOrWhiteSpace(TitleEdit.Text) &&
  143. !string.IsNullOrWhiteSpace(ContentEdit.Text);
  144. ReloadNames();
  145. }
  146. private void ReloadNames()
  147. {
  148. var names = new List<string>();
  149. foreach (KeyValuePair<Guid, string> pair in EmployeeChecks.SelectedItems)
  150. names.Add(pair.Value);
  151. Recipients.Text = string.Join("; ", names);
  152. }
  153. private void OK_Click(object sender, RoutedEventArgs e)
  154. {
  155. using (new WaitCursor())
  156. {
  157. var me = new Client<Employee>().Load(new Filter<Employee>(x => x.UserID).IsEqualTo(ClientFactory.UserGuid)).FirstOrDefault();
  158. var updates = new List<Notification>();
  159. foreach (KeyValuePair<Guid, string> item in EmployeeChecks.SelectedItems)
  160. {
  161. var notification = new Notification { Title = TitleEdit.Text, Description = ContentEdit.Text };
  162. notification.Sender.ID = me != null ? me.ID : Guid.Empty;
  163. notification.Employee.ID = item.Key;
  164. if (JobDetails.SelectedItem != null)
  165. {
  166. var job = (KeyValuePair<Guid, string>)JobDetails.SelectedItem;
  167. notification.Job.ID = job.Key;
  168. }
  169. updates.Add(notification);
  170. }
  171. new Client<Notification>().Save(updates, "Sent Notification");
  172. }
  173. DialogResult = true;
  174. Close();
  175. }
  176. private void Cancel_Click(object sender, RoutedEventArgs e)
  177. {
  178. DialogResult = false;
  179. Close();
  180. }
  181. private void TeamCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
  182. {
  183. var pair = (KeyValuePair<Guid, string>)TeamCombo.SelectedItem;
  184. ReloadEmployees(pair.Key);
  185. }
  186. private void SelectAll_Checked(object sender, RoutedEventArgs e)
  187. {
  188. EmployeeChecks.SelectedItems.Clear();
  189. foreach (var item in EmployeeChecks.Items)
  190. EmployeeChecks.SelectedItems.Add(item);
  191. }
  192. private void SelectAll_Unchecked(object sender, RoutedEventArgs e)
  193. {
  194. EmployeeChecks.SelectedItems.Clear();
  195. }
  196. private void TitleEdit_LostFocus(object sender, RoutedEventArgs e)
  197. {
  198. }
  199. private void LostFocus(object sender, RoutedEventArgs e)
  200. {
  201. CheckOK(sender);
  202. }
  203. }
  204. }