ReservationForm.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using RestaurantApp.DAL;
  2. using RestaurantApp.Models;
  3. using System;
  4. using System.Linq;
  5. using System.Windows.Forms;
  6. namespace RestaurantApp
  7. {
  8. public partial class ReservationForm : Form
  9. {
  10. private ReservationRepository repo = new ReservationRepository();
  11. public ReservationForm()
  12. {
  13. InitializeComponent();
  14. cmbStatusFilter.SelectedIndex = 0;
  15. LoadReservations();
  16. btnSearch.Click += (s, e) => LoadReservations();
  17. btnRefresh.Click += (s, e) => LoadReservations();
  18. btnAdd.Click += BtnAdd_Click;
  19. btnEdit.Click += BtnEdit_Click;
  20. btnDelete.Click += BtnDelete_Click;
  21. dgvReservations.SelectionChanged += DgvReservations_SelectionChanged;
  22. }
  23. private void LoadReservations()
  24. {
  25. try
  26. {
  27. string status = cmbStatusFilter.SelectedIndex > 0 ? cmbStatusFilter.Text : "";
  28. // Если выбран "Все" - передаём пустую строку или null
  29. string statusParam = (status == "Все" || string.IsNullOrEmpty(status)) ? null : status;
  30. var reservations = repo.GetAll(statusParam, null, null);
  31. if (!string.IsNullOrWhiteSpace(txtSearch.Text))
  32. {
  33. var search = txtSearch.Text.ToLower();
  34. reservations = reservations.FindAll(r =>
  35. r.ClientName.ToLower().Contains(search) ||
  36. r.TableInfo.ToLower().Contains(search));
  37. }
  38. dgvReservations.DataSource = null;
  39. dgvReservations.DataSource = reservations;
  40. // ... остальное
  41. }
  42. catch (Exception ex)
  43. {
  44. MessageBox.Show($"Ошибка загрузки: {ex.Message}");
  45. }
  46. }
  47. private void DgvReservations_SelectionChanged(object sender, EventArgs e)
  48. {
  49. if (dgvReservations.CurrentRow != null)
  50. {
  51. var res = (Reservation)dgvReservations.CurrentRow.DataBoundItem;
  52. lblTable.Text = $"Стол: {res.TableInfo}";
  53. lblClient.Text = $"Клиент: {res.ClientName}";
  54. lblPhone.Text = $"Телефон: {(string.IsNullOrEmpty(res.ClientName) ? "-" : "скрыт")}";
  55. lblGuests.Text = $"Кол-во гостей: {res.GuestsCount}";
  56. lblDateTime.Text = $"Дата/время: {res.ReservedAt:dd.MM.yyyy HH:mm}";
  57. lblDuration.Text = $"Длительность: {res.DurationMin} мин";
  58. lblNotes.Text = $"Примечание: {res.Notes}";
  59. }
  60. }
  61. private void BtnAdd_Click(object sender, EventArgs e)
  62. {
  63. var form = new ReservationEditForm();
  64. if (form.ShowDialog() == DialogResult.OK)
  65. {
  66. LoadReservations();
  67. }
  68. }
  69. private void BtnEdit_Click(object sender, EventArgs e)
  70. {
  71. if (dgvReservations.CurrentRow == null)
  72. {
  73. MessageBox.Show("Выберите бронирование");
  74. return;
  75. }
  76. var res = (Reservation)dgvReservations.CurrentRow.DataBoundItem;
  77. var form = new ReservationEditForm(res);
  78. if (form.ShowDialog() == DialogResult.OK)
  79. {
  80. LoadReservations();
  81. }
  82. }
  83. private void BtnDelete_Click(object sender, EventArgs e)
  84. {
  85. if (dgvReservations.CurrentRow == null)
  86. {
  87. MessageBox.Show("Выберите бронирование");
  88. return;
  89. }
  90. var res = (Reservation)dgvReservations.CurrentRow.DataBoundItem;
  91. if (MessageBox.Show($"Удалить бронирование для {res.ClientName} на {res.ReservedAt:dd.MM.yyyy HH:mm}?",
  92. "Подтверждение", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  93. {
  94. try
  95. {
  96. repo.Delete(res.ReservationId);
  97. LoadReservations();
  98. lblTable.Text = "Стол: ";
  99. lblClient.Text = "Клиент: ";
  100. lblPhone.Text = "Телефон: ";
  101. lblGuests.Text = "Кол-во гостей: ";
  102. lblDateTime.Text = "Дата/время: ";
  103. lblDuration.Text = "Длительность: ";
  104. lblNotes.Text = "Примечание: ";
  105. MessageBox.Show("Бронирование удалено", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
  106. }
  107. catch (Exception ex)
  108. {
  109. MessageBox.Show($"Ошибка: {ex.Message}");
  110. }
  111. }
  112. }
  113. }
  114. }