using RestaurantApp.DAL; using RestaurantApp.Models; using System; using System.Linq; using System.Windows.Forms; namespace RestaurantApp { public partial class ReservationForm : Form { private ReservationRepository repo = new ReservationRepository(); public ReservationForm() { InitializeComponent(); cmbStatusFilter.SelectedIndex = 0; LoadReservations(); btnSearch.Click += (s, e) => LoadReservations(); btnRefresh.Click += (s, e) => LoadReservations(); btnAdd.Click += BtnAdd_Click; btnEdit.Click += BtnEdit_Click; btnDelete.Click += BtnDelete_Click; dgvReservations.SelectionChanged += DgvReservations_SelectionChanged; } private void LoadReservations() { try { string status = cmbStatusFilter.SelectedIndex > 0 ? cmbStatusFilter.Text : ""; // Если выбран "Все" - передаём пустую строку или null string statusParam = (status == "Все" || string.IsNullOrEmpty(status)) ? null : status; var reservations = repo.GetAll(statusParam, null, null); if (!string.IsNullOrWhiteSpace(txtSearch.Text)) { var search = txtSearch.Text.ToLower(); reservations = reservations.FindAll(r => r.ClientName.ToLower().Contains(search) || r.TableInfo.ToLower().Contains(search)); } dgvReservations.DataSource = null; dgvReservations.DataSource = reservations; // ... остальное } catch (Exception ex) { MessageBox.Show($"Ошибка загрузки: {ex.Message}"); } } private void DgvReservations_SelectionChanged(object sender, EventArgs e) { if (dgvReservations.CurrentRow != null) { var res = (Reservation)dgvReservations.CurrentRow.DataBoundItem; lblTable.Text = $"Стол: {res.TableInfo}"; lblClient.Text = $"Клиент: {res.ClientName}"; lblPhone.Text = $"Телефон: {(string.IsNullOrEmpty(res.ClientName) ? "-" : "скрыт")}"; lblGuests.Text = $"Кол-во гостей: {res.GuestsCount}"; lblDateTime.Text = $"Дата/время: {res.ReservedAt:dd.MM.yyyy HH:mm}"; lblDuration.Text = $"Длительность: {res.DurationMin} мин"; lblNotes.Text = $"Примечание: {res.Notes}"; } } private void BtnAdd_Click(object sender, EventArgs e) { var form = new ReservationEditForm(); if (form.ShowDialog() == DialogResult.OK) { LoadReservations(); } } private void BtnEdit_Click(object sender, EventArgs e) { if (dgvReservations.CurrentRow == null) { MessageBox.Show("Выберите бронирование"); return; } var res = (Reservation)dgvReservations.CurrentRow.DataBoundItem; var form = new ReservationEditForm(res); if (form.ShowDialog() == DialogResult.OK) { LoadReservations(); } } private void BtnDelete_Click(object sender, EventArgs e) { if (dgvReservations.CurrentRow == null) { MessageBox.Show("Выберите бронирование"); return; } var res = (Reservation)dgvReservations.CurrentRow.DataBoundItem; if (MessageBox.Show($"Удалить бронирование для {res.ClientName} на {res.ReservedAt:dd.MM.yyyy HH:mm}?", "Подтверждение", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { try { repo.Delete(res.ReservationId); LoadReservations(); lblTable.Text = "Стол: "; lblClient.Text = "Клиент: "; lblPhone.Text = "Телефон: "; lblGuests.Text = "Кол-во гостей: "; lblDateTime.Text = "Дата/время: "; lblDuration.Text = "Длительность: "; lblNotes.Text = "Примечание: "; MessageBox.Show("Бронирование удалено", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show($"Ошибка: {ex.Message}"); } } } } }