DynamicIssuesEditor.xaml.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using InABox.Wpf;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Input;
  8. using InABox.Clients;
  9. using InABox.Core;
  10. namespace InABox.DynamicGrid
  11. {
  12. /// <summary>
  13. /// Interaction logic for ProductIssuesWindow.xaml
  14. /// </summary>
  15. public partial class DynamicIssuesEditor : ThemableWindow
  16. {
  17. private readonly IIssues[] _items;
  18. private readonly List<string> notes = new();
  19. public DynamicIssuesEditor(IIssues[] issues, bool allowclear = false)
  20. {
  21. _items = issues;
  22. InitializeComponent();
  23. ReloadHistory();
  24. ClearIssues.Visibility = allowclear ? Visibility.Visible : Visibility.Collapsed;
  25. }
  26. public void SetCustom(FrameworkElement custom)
  27. {
  28. Custom.Children.Clear();
  29. Custom.Children.Add(custom);
  30. }
  31. private void ReloadHistory()
  32. {
  33. var issues = _items.Select(x => x.Issues).Where(x => !string.IsNullOrWhiteSpace(x)).Distinct().ToArray();
  34. var history = new List<string>();
  35. if (issues.Length == 1)
  36. history.Add(issues[0]);
  37. else if (issues.Length > 1)
  38. history.Add("(Multiple Issues Noted)");
  39. if (issues.Length > 0 && notes.Any())
  40. history.Add("=========================================");
  41. if (notes.Any())
  42. history.AddRange(notes);
  43. History.Text = string.Join("\n", history);
  44. }
  45. private void ClearIssues_Click(object sender, RoutedEventArgs e)
  46. {
  47. if (MessageBox.Show("Are you sure you wish to clear these notes?", "Confirm Clear", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
  48. return;
  49. foreach (var item in _items)
  50. item.Issues = "";
  51. DialogResult = true;
  52. }
  53. private void OK_Click(object sender, RoutedEventArgs e)
  54. {
  55. if (!notes.Any())
  56. {
  57. DialogResult = false;
  58. }
  59. else
  60. {
  61. foreach (var item in _items)
  62. item.Issues = (string.IsNullOrWhiteSpace(item.Issues) ? "" : item.Issues + "\n") + string.Format(
  63. "{0:dd MMM yy hh\\:mm\\:ss} {1} {2}",
  64. DateTime.Now, ClientFactory.UserID, string.Join("\n", notes));
  65. DialogResult = true;
  66. }
  67. }
  68. private void Cancel_Click(object sender, RoutedEventArgs e)
  69. {
  70. DialogResult = false;
  71. }
  72. private void AddNote_Click(object sender, RoutedEventArgs e)
  73. {
  74. DoAddNote();
  75. }
  76. private void DoAddNote()
  77. {
  78. notes.Add(NewNote.Text);
  79. NewNote.Text = "";
  80. NewNote.Focus();
  81. ReloadHistory();
  82. }
  83. private void NewNote_TextChanged(object sender, TextChangedEventArgs e)
  84. {
  85. AddNote.IsEnabled = !string.IsNullOrWhiteSpace(NewNote.Text);
  86. }
  87. private void AddNote_PreviewKeyDown(object sender, KeyEventArgs e)
  88. {
  89. if (e.Key == Key.Enter || e.Key == Key.Return)
  90. DoAddNote();
  91. }
  92. }
  93. }