StaffChangeOrAddPage.xaml.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. namespace Hotel_Course_Project
  18. {
  19. /// <summary>
  20. /// Логика взаимодействия для StaffChangeOrAddPage.xaml
  21. /// </summary>
  22. public partial class StaffChangeOrAddPage : Page
  23. {
  24. public byte[] cotractImage;
  25. public string FilePath;
  26. public Staff _staff;
  27. public StaffChangeOrAddPage(Staff staff)
  28. {
  29. InitializeComponent();
  30. DataContext = staff;
  31. _staff = staff;
  32. if (DataContext != null)
  33. {
  34. SStaffRole.Text = staff.ToString();
  35. StaffChangeOrAddBtn.Content = "Изменить данные";
  36. this.Title = "Информация о сотруднике";
  37. }
  38. else
  39. {
  40. StaffChangeOrAddBtn.Content = "Добавить сотрудника";
  41. this.Title = "Окно добавления сотрудника";
  42. }
  43. SStaffRole.ItemsSource = DataBase.db.StaffRole.ToList();
  44. if (staff == null || staff.PhotoContract == null)
  45. {
  46. SContractPhoto.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/no_picture.jpg"));
  47. }
  48. }
  49. private void StaffChangeOrAddBtn_Click(object sender, RoutedEventArgs e)
  50. {
  51. var existUser = DataBase.db.Staff.SingleOrDefault(item => item.Login == SLogin.Text);
  52. if (existUser != null && DataContext == null)
  53. {
  54. MessageBox.Show("Такой логин уже имеется в базе данных");
  55. return;
  56. }
  57. else if (SLName.Text == null || SFName.Text == null || SLogin.Text == null || SPassword.Text == null || SContractPhoto == null)
  58. {
  59. MessageBox.Show("Ключевые поля не были заполнены");
  60. return;
  61. }
  62. else
  63. {
  64. if( DataContext == null)
  65. {
  66. int numRole = DataBase.db.StaffRole.SingleOrDefault(item => item.Name == SStaffRole.Text).Id;
  67. Staff staff = new Staff()
  68. {
  69. LName = SLName.Text,
  70. FName = SFName.Text,
  71. MName = SMName.Text,
  72. Login = SLogin.Text,
  73. Password = SPassword.Text,
  74. Id_StaffRole = numRole,
  75. PhotoContract = cotractImage,
  76. Id_PersStatus = 1
  77. };
  78. DataBase.db.Staff.Add(staff);
  79. var lastId = DataBase.db.Staff.OrderByDescending(item => item.Id).FirstOrDefault().Id;
  80. DataBase.db.SaveChanges();
  81. }
  82. else
  83. {
  84. }
  85. }
  86. }
  87. private void StaffDeleteOrRestoreBtn_Click(object sender, RoutedEventArgs e)
  88. {
  89. }
  90. private void SContractPhoto_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  91. {
  92. OpenFileDialog openFileDialog = new OpenFileDialog();
  93. if (openFileDialog.ShowDialog() == true)
  94. {
  95. FilePath = openFileDialog.FileName;
  96. string uriString = $"{FilePath}";
  97. BitmapImage bitimage = new BitmapImage(new Uri(@uriString));
  98. SContractPhoto.Source = bitimage;
  99. cotractImage = SomeMethods.ConvertBitmapImageToByte(bitimage);
  100. }
  101. else
  102. {
  103. FilePath = string.Empty;
  104. }
  105. }
  106. }
  107. }