ClientChangeOrAddPage.xaml.cs 4.6 KB

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