| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- namespace practiceApplicaiton;
- public partial class BookingEditPage : ContentPage
- {
- private readonly Booking? _booking;
- private List<Client> _clients = [];
- private List<Room> _rooms = [];
- public BookingEditPage(Booking? booking)
- {
- InitializeComponent();
- _booking = booking;
- Title = booking == null ? "Новое бронирование" : "Редактировать бронирование";
- }
- protected override async void OnAppearing()
- {
- base.OnAppearing();
- _clients = await Repository.GetClientsAsync();
- _rooms = await Repository.GetRoomsAsync();
- ClientPicker.ItemsSource = _clients.Select(c => c.FullName).ToList();
- RoomPicker.ItemsSource = _rooms.Select(r => $"{r.RoomNumber} ({r.TypeName})").ToList();
- if (_booking != null)
- {
- var ci = _clients.FindIndex(c => c.ClientId == _booking.ClientId);
- var ri = _rooms.FindIndex(r => r.RoomId == _booking.RoomId);
- ClientPicker.SelectedIndex = ci;
- RoomPicker.SelectedIndex = ri;
- CheckInPicker.Date = _booking.CheckInDate;
- CheckOutPicker.Date = _booking.CheckOutDate;
- PriceEntry.Text = _booking.PricePerNight.ToString();
- }
- else
- {
- CheckInPicker.Date = DateTime.Today;
- CheckOutPicker.Date = DateTime.Today.AddDays(3);
- }
- }
- private async void OnSaveClicked(object sender, EventArgs e)
- {
- if (ClientPicker.SelectedIndex < 0 || RoomPicker.SelectedIndex < 0)
- {
- await DisplayAlert("Ошибка", "Выберите клиента и комнату", "OK");
- return;
- }
- if (!decimal.TryParse(PriceEntry.Text, out var price) || price <= 0)
- {
- await DisplayAlert("Ошибка", "Введите корректную цену", "OK");
- return;
- }
- if (CheckOutPicker.Date <= CheckInPicker.Date)
- {
- await DisplayAlert("Ошибка", "Дата выезда должна быть позже заезда", "OK");
- return;
- }
- try
- {
- var client = _clients[ClientPicker.SelectedIndex];
- var room = _rooms[RoomPicker.SelectedIndex];
- if (_booking == null)
- await Repository.CreateBookingAsync(client.ClientId, room.RoomId,
- (DateTime)CheckInPicker.Date, (DateTime)CheckOutPicker.Date, price);
- else
- await Repository.UpdateBookingAsync(_booking.BookingId,
- (DateTime)CheckInPicker.Date, (DateTime)CheckOutPicker.Date, price);
- await Navigation.PopAsync();
- }
- catch (Exception ex)
- {
- await DisplayAlert("Ошибка", ex.Message, "OK");
- }
- }
- }
|