ClientChangeOrAddPage.xaml.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. namespace Hotel_Course_Project
  17. {
  18. /// <summary>
  19. /// Логика взаимодействия для ClientChangeOrAddPage.xaml
  20. /// </summary>
  21. public partial class ClientChangeOrAddPage : Page
  22. {
  23. public Client _client;
  24. public int idClient;
  25. public byte[] PassportImage;
  26. public string FilePath;
  27. public ClientChangeOrAddPage(Client client)
  28. {
  29. InitializeComponent();
  30. DataContext = client;
  31. _client = client;
  32. if (DataContext != null)
  33. {
  34. ClientChangeOrAddBtn.Content = "Изменить данные";
  35. this.Title = "Информация о клиенте";
  36. idClient = client.Id;
  37. ClientDeleteOrRestoreBtn.Visibility = Visibility.Visible;
  38. if (client.Id_PersStatus == 1)
  39. {
  40. ClientDeleteOrRestoreBtn.Content = "Удалить клиента";
  41. }
  42. else
  43. {
  44. ClientDeleteOrRestoreBtn.Content = "Востановить клиента";
  45. }
  46. }
  47. else
  48. {
  49. ClientChangeOrAddBtn.Content = "Добавить клиента";
  50. this.Title = "Окно добавления клиента";
  51. ClientDeleteOrRestoreBtn.Visibility = Visibility.Collapsed;
  52. }
  53. if (client == null || client.Passport == null)
  54. {
  55. CPassportPhoto.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/PersDefaultPhoto.png"));
  56. }
  57. }
  58. private void ClientChangeOrAddBtn_Click(object sender, RoutedEventArgs e)
  59. {
  60. if (DataContext == null)
  61. {
  62. }
  63. if (CLName.Text == null || CFName.Text == null)
  64. {
  65. MessageBox.Show("Ключевые поля не были заполнены!");
  66. return;
  67. }
  68. else if (CPassportPhoto == null)
  69. {
  70. MessageBox.Show("Фотография паспорта не была загружена!");
  71. return;
  72. }
  73. else
  74. {
  75. if (DataContext == null)
  76. {
  77. Client client = new Client()
  78. {
  79. LName = CLName.Text,
  80. FName = CFName.Text,
  81. MName = CMName.Text,
  82. Passport = PassportImage,
  83. Id_PersStatus = 1
  84. };
  85. DataBase.db.Client.Add(client);
  86. var lastId = DataBase.db.Client.OrderByDescending(item => item.Id).FirstOrDefault().Id;
  87. DataBase.db.SaveChanges();
  88. MessageBox.Show("Клиент добавлен в базу.");
  89. }
  90. else
  91. {
  92. MessageBox.Show("Данные по клиенту изменены.");
  93. _client.Passport = PassportImage;
  94. DataBase.db.SaveChanges();
  95. }
  96. PChanger.MainFrame.GoBack();
  97. }
  98. }
  99. private void ClientDeleteOrRestoreBtn_Click(object sender, RoutedEventArgs e)
  100. {
  101. if (_client.Id_PersStatus == 1)
  102. {
  103. _client.Id_PersStatus = 2;
  104. MessageBox.Show("Статус пользователя изменился на <удалён>");
  105. }
  106. else
  107. {
  108. _client.Id_PersStatus = 1;
  109. MessageBox.Show("Статус пользователя восстановлен");
  110. }
  111. DataBase.db.SaveChanges();
  112. PChanger.MainFrame.GoBack();
  113. }
  114. private void CPassportPhoto_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  115. {
  116. OpenFileDialog openFileDialog = new OpenFileDialog();
  117. if (openFileDialog.ShowDialog() == true)
  118. {
  119. FilePath = openFileDialog.FileName;
  120. string uriString = $"{FilePath}";
  121. BitmapImage bitimage = new BitmapImage(new Uri(@uriString));
  122. CPassportPhoto.Source = bitimage;
  123. PassportImage = SomeMethods.ConvertBitmapImageToByte(bitimage);
  124. }
  125. else
  126. {
  127. FilePath = string.Empty;
  128. }
  129. }
  130. }
  131. }