BookingEditPage.xaml.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. namespace practiceApplicaiton;
  2. public partial class BookingEditPage : ContentPage
  3. {
  4. private readonly Booking? _booking;
  5. private List<Client> _clients = [];
  6. private List<Room> _rooms = [];
  7. public BookingEditPage(Booking? booking)
  8. {
  9. InitializeComponent();
  10. _booking = booking;
  11. Title = booking == null ? "Новое бронирование" : "Редактировать бронирование";
  12. }
  13. protected override async void OnAppearing()
  14. {
  15. base.OnAppearing();
  16. _clients = await Repository.GetClientsAsync();
  17. _rooms = await Repository.GetRoomsAsync();
  18. ClientPicker.ItemsSource = _clients.Select(c => c.FullName).ToList();
  19. RoomPicker.ItemsSource = _rooms.Select(r => $"{r.RoomNumber} ({r.TypeName})").ToList();
  20. if (_booking != null)
  21. {
  22. var ci = _clients.FindIndex(c => c.ClientId == _booking.ClientId);
  23. var ri = _rooms.FindIndex(r => r.RoomId == _booking.RoomId);
  24. ClientPicker.SelectedIndex = ci;
  25. RoomPicker.SelectedIndex = ri;
  26. CheckInPicker.Date = _booking.CheckInDate;
  27. CheckOutPicker.Date = _booking.CheckOutDate;
  28. PriceEntry.Text = _booking.PricePerNight.ToString();
  29. }
  30. else
  31. {
  32. CheckInPicker.Date = DateTime.Today;
  33. CheckOutPicker.Date = DateTime.Today.AddDays(3);
  34. }
  35. }
  36. private async void OnSaveClicked(object sender, EventArgs e)
  37. {
  38. if (ClientPicker.SelectedIndex < 0 || RoomPicker.SelectedIndex < 0)
  39. {
  40. await DisplayAlert("Ошибка", "Выберите клиента и комнату", "OK");
  41. return;
  42. }
  43. if (!decimal.TryParse(PriceEntry.Text, out var price) || price <= 0)
  44. {
  45. await DisplayAlert("Ошибка", "Введите корректную цену", "OK");
  46. return;
  47. }
  48. if (CheckOutPicker.Date <= CheckInPicker.Date)
  49. {
  50. await DisplayAlert("Ошибка", "Дата выезда должна быть позже заезда", "OK");
  51. return;
  52. }
  53. try
  54. {
  55. var client = _clients[ClientPicker.SelectedIndex];
  56. var room = _rooms[RoomPicker.SelectedIndex];
  57. if (_booking == null)
  58. await Repository.CreateBookingAsync(client.ClientId, room.RoomId,
  59. (DateTime)CheckInPicker.Date, (DateTime)CheckOutPicker.Date, price);
  60. else
  61. await Repository.UpdateBookingAsync(_booking.BookingId,
  62. (DateTime)CheckInPicker.Date, (DateTime)CheckOutPicker.Date, price);
  63. await Navigation.PopAsync();
  64. }
  65. catch (Exception ex)
  66. {
  67. await DisplayAlert("Ошибка", ex.Message, "OK");
  68. }
  69. }
  70. }