TicketWindow.xaml.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using Practics.Model;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Shapes;
  17. using System.Xaml.Permissions;
  18. using System.Windows.Markup;
  19. using System.Text.RegularExpressions;
  20. namespace Practics
  21. {
  22. /// <summary>
  23. /// Interaction logic for TicketWindow.xaml
  24. /// </summary>
  25. public partial class TicketWindow : Window
  26. {
  27. User user;
  28. Model.Model model = Model.Model.ModelInstance;
  29. public TicketWindow(User user)
  30. {
  31. InitializeComponent();
  32. tbUserName.Text = user.ShortName;
  33. this.user = user;
  34. //model.InitDataTableProblemTypes();
  35. cbProblemType.ItemsSource = model.DataTableProblemTypes.DefaultView;
  36. cbProblemType.DisplayMemberPath = "NameProblemType";
  37. cbProblemType.SelectedIndex = 0;
  38. cbProblemType.SelectedValue = 0;
  39. cbProblemType.SelectedValuePath = "ID";
  40. //model.InitDataTableStatuses();
  41. model.InitProblemsByUserID(this.user.Id);
  42. dgUserTickets.ItemsSource = model.DataTableProblemsByID.DefaultView;
  43. dgcbcStatus.ItemsSource = model.DataTableStatuses.DefaultView;
  44. dgcbcStatus.DisplayMemberPath = "name";
  45. dgcbcStatus.SelectedValuePath = "Id";
  46. //tbProblemDescribe2.DataContext = model.dataTable.DefaultView;
  47. }
  48. private void btnSendProblem_Click(object sender, RoutedEventArgs e)
  49. {
  50. string pcNamePattern = @"^[a-zA-Z]{3,4}-[a-zA-Z]{2,3}\d{2,3}$";
  51. if (tbProblemName.Text == "")
  52. {
  53. MessageBox.Show("Заполните поле наименования проблемы");
  54. return;
  55. }
  56. if (tbPCName.Text == "")
  57. {
  58. MessageBox.Show("Заполните поле имя компьютера");
  59. return;
  60. }
  61. if (!Regex.IsMatch(tbPCName.Text, pcNamePattern))
  62. {
  63. MessageBox.Show("Имя компьютера не соответствует требуемому формату (пример: ABC-DE12)");
  64. return;
  65. }
  66. if (tbProblemDescribe.Text == "")
  67. {
  68. MessageBox.Show("Заполните поле подробного описания проблемы");
  69. return;
  70. }
  71. model.AddProblemToDB(user.Id, tbProblemName.Text, tbPCName.Text, tbProblemDescribe.Text, Convert.ToInt32(cbProblemType.SelectedValue.ToString()));
  72. clearWindowField();
  73. MessageBox.Show("Сообщение отправлено!");
  74. }
  75. void clearWindowField()
  76. {
  77. tbPCName.Text = string.Empty;
  78. tbProblemName.Text = string.Empty;
  79. tbProblemDescribe.Text = string.Empty;
  80. }
  81. private void dgUserTickets_Selected(object sender, RoutedEventArgs e)
  82. {
  83. }
  84. int currentID=-1;
  85. DataRow currentRow = null;
  86. private void dgUserTickets_SelectionChanged(object sender, SelectionChangedEventArgs e)
  87. {
  88. if (dgUserTickets.SelectedItems != null)
  89. {
  90. DataGridRow row = (dgUserTickets.SelectedItem as DataGridRow);
  91. DataRowView selectedRow = (DataRowView)dgUserTickets.SelectedItem;
  92. if (selectedRow != null)
  93. {
  94. //DataRow dataTableRow = selectedRow.Row;
  95. currentRow = selectedRow.Row;
  96. currentID = Convert.ToInt32(currentRow["ID"]);
  97. rtbChat.Document.Blocks.Clear();
  98. /*
  99. using (MemoryStream memoryStream = new MemoryStream())
  100. {
  101. using (StreamWriter writer = new StreamWriter(memoryStream))
  102. {
  103. {
  104. writer.Write(currentRow["Chat"]);
  105. writer.Flush();
  106. memoryStream.Position = 0;
  107. TextRange textRange = new TextRange(rtbChat.Document.ContentStart, rtbChat.Document.ContentEnd);
  108. textRange.Load(memoryStream, DataFormats.Xaml);
  109. }
  110. }
  111. }*/
  112. model.LoadChatToRichTextBox(currentRow, rtbChat);
  113. }
  114. }
  115. }
  116. private void btnSendMessage_Click(object sender, RoutedEventArgs e)
  117. {
  118. if (currentRow == null) return;
  119. string currentTime = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss");
  120. Paragraph paragraph = new Paragraph();
  121. Run timeRun = new Run(currentRow["LastName"].ToString()+":"+currentTime)
  122. {
  123. FontWeight = FontWeights.Bold,
  124. Foreground=new SolidColorBrush(Colors.Red)
  125. };
  126. paragraph.Inlines.Add(timeRun);
  127. Run message = new Run("\r\n"+tbMessage.Text);
  128. paragraph.Inlines.Add(message);
  129. rtbChat.Document.Blocks.Add(paragraph);
  130. string documentContent= model.RichTextBoxContentToString(rtbChat);
  131. currentRow["Chat"] = documentContent;
  132. model.ChatUpdate(currentID, documentContent);
  133. }
  134. private void btnRefillProblems_Click(object sender, RoutedEventArgs e)
  135. {
  136. model.RefillProblemsById();
  137. }
  138. private void btnExit_Click(object sender, RoutedEventArgs e)
  139. {
  140. MainWindow mainWindow = new MainWindow();
  141. mainWindow.Show();
  142. this.Close();
  143. }
  144. private void TabItem_GotFocus(object sender, RoutedEventArgs e)
  145. {
  146. model.RefillProblemsById();
  147. }
  148. }
  149. }