-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookingManagementForm.cs
More file actions
597 lines (531 loc) · 23.4 KB
/
Copy pathBookingManagementForm.cs
File metadata and controls
597 lines (531 loc) · 23.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace DB_Project
{
public partial class BookingManagementForm : Form
{
private readonly string connectionString = @"Data Source=ABDULSABOOR190\SQLEXPRESS;Initial Catalog=sabbbb;Integrated Security=True;Encrypt=False";
public BookingManagementForm()
{
InitializeComponent();
SetupForm();
}
private void SetupForm()
{
// Set active button appearance
HighlightActiveButton(button2); // Highlight the booking management button
}
// Helper method to highlight the active sidebar button
private void HighlightActiveButton(Button activeButton)
{
// Reset all buttons
foreach (Control c in panelSidebar.Controls)
{
if (c is Button btn)
{
btn.BackColor = Color.FromArgb(52, 73, 94);
btn.ForeColor = Color.White;
}
}
// Highlight active button
activeButton.BackColor = Color.FromArgb(41, 128, 185);
activeButton.ForeColor = Color.White;
}
private void BookingManagementForm_Load(object sender, EventArgs e)
{
// Clear existing columns that were created in designer
dgvBookings.Columns.Clear();
// Configure DataGridView columns programmatically
ConfigureDataGridView();
// Load data from database
LoadBookings();
}
private void ConfigureDataGridView()
{
// Add ID column
DataGridViewTextBoxColumn bookingIdColumn = new DataGridViewTextBoxColumn
{
Name = "BookingId",
HeaderText = "Booking ID",
Width = 80,
ReadOnly = true
};
dgvBookings.Columns.Add(bookingIdColumn);
// Add Customer Name column
DataGridViewTextBoxColumn customerNameColumn = new DataGridViewTextBoxColumn
{
Name = "CustomerName",
HeaderText = "Customer Name",
Width = 150,
ReadOnly = true
};
dgvBookings.Columns.Add(customerNameColumn);
// Add Service/Trip Name column
DataGridViewTextBoxColumn serviceNameColumn = new DataGridViewTextBoxColumn
{
Name = "ServiceName",
HeaderText = "Service/Trip Name",
Width = 150,
ReadOnly = true
};
dgvBookings.Columns.Add(serviceNameColumn);
// Add Booking Date column
DataGridViewTextBoxColumn bookingDateColumn = new DataGridViewTextBoxColumn
{
Name = "BookingDate",
HeaderText = "Booking Date",
Width = 110,
ReadOnly = true,
DefaultCellStyle = new DataGridViewCellStyle { Format = "dd/MM/yyyy" }
};
dgvBookings.Columns.Add(bookingDateColumn);
// Add Participants column
DataGridViewTextBoxColumn participantsColumn = new DataGridViewTextBoxColumn
{
Name = "Participants",
HeaderText = "Participants",
Width = 90,
ReadOnly = true
};
dgvBookings.Columns.Add(participantsColumn);
// Add Amount column
DataGridViewTextBoxColumn amountColumn = new DataGridViewTextBoxColumn
{
Name = "Amount",
HeaderText = "Amount",
Width = 100,
ReadOnly = true,
DefaultCellStyle = new DataGridViewCellStyle { Format = "C2" }
};
dgvBookings.Columns.Add(amountColumn);
// Add Status ComboBox column
DataGridViewComboBoxColumn statusColumn = new DataGridViewComboBoxColumn
{
Name = "Status",
HeaderText = "Status",
Width = 95
};
statusColumn.Items.AddRange(new object[] { "pending", "confirmed", "canceled" });
dgvBookings.Columns.Add(statusColumn);
}
private void LoadBookings()
{
dgvBookings.Rows.Clear();
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
// Get a list of all bookings with traveler info and trip info
string query = @"
SELECT b.booking_id, CONCAT(tp.first_name, ' ', tp.last_name) AS customer_name,
t.title AS trip_name, b.booking_date, b.participants, b.total_amount, b.status
FROM Booking b
INNER JOIN TravelerProfile tp ON b.traveler_id = tp.user_id
INNER JOIN Trip t ON b.trip_id = t.trip_id
ORDER BY b.booking_date DESC";
using (SqlCommand command = new SqlCommand(query, connection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
int rowIndex = dgvBookings.Rows.Add();
DataGridViewRow row = dgvBookings.Rows[rowIndex];
// Populate each cell in the row
row.Cells["BookingId"].Value = reader["booking_id"].ToString();
row.Cells["CustomerName"].Value = reader["customer_name"].ToString();
row.Cells["ServiceName"].Value = reader["trip_name"].ToString();
row.Cells["BookingDate"].Value = Convert.ToDateTime(reader["booking_date"]);
row.Cells["Participants"].Value = reader["participants"].ToString();
row.Cells["Amount"].Value = Convert.ToDecimal(reader["total_amount"]);
row.Cells["Status"].Value = reader["status"].ToString();
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Error loading bookings: {ex.Message}", "Database Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnUpdateStatus_Click(object sender, EventArgs e)
{
if (dgvBookings.SelectedRows.Count == 0 && dgvBookings.CurrentRow == null)
{
MessageBox.Show("Please select a booking to update.", "No Selection",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
try
{
int rowIndex = dgvBookings.CurrentRow.Index;
string bookingId = dgvBookings.Rows[rowIndex].Cells["BookingId"].Value.ToString();
// Create and show the booking edit form
using (BookingEditForm editForm = new BookingEditForm(bookingId, connectionString))
{
if (editForm.ShowDialog() == DialogResult.OK)
{
// If user clicked OK in the edit form, refresh the bookings
LoadBookings();
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Error opening booking editor: {ex.Message}", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
// Form class for editing booking details
private class BookingEditForm : Form
{
private readonly string connectionString;
private readonly string bookingId;
private TextBox txtCustomerName;
private TextBox txtServiceName;
private DateTimePicker dtpBookingDate;
private NumericUpDown nudParticipants;
private NumericUpDown nudAmount;
private ComboBox cboStatus;
private TextBox txtSpecialRequests;
private Button btnSave;
private Button btnCancel;
private int travelerId;
private int tripId;
public BookingEditForm(string bookingId, string connectionString)
{
this.bookingId = bookingId;
this.connectionString = connectionString;
InitializeEditComponents();
LoadBookingDetails();
}
private void InitializeEditComponents()
{
// Form properties
this.Text = "Edit Booking";
this.Size = new Size(500, 450);
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.StartPosition = FormStartPosition.CenterParent;
this.BackColor = Color.FromArgb(236, 240, 241);
// Create panel for the form fields
Panel panel = new Panel
{
Dock = DockStyle.Fill,
Padding = new Padding(20)
};
this.Controls.Add(panel);
// Customer Name
Label lblCustomerName = new Label
{
Text = "Customer Name:",
Location = new Point(20, 20),
AutoSize = true,
Font = new Font("Segoe UI", 9F)
};
panel.Controls.Add(lblCustomerName);
txtCustomerName = new TextBox
{
Location = new Point(150, 20),
Size = new Size(300, 25),
Font = new Font("Segoe UI", 9F),
ReadOnly = true // Can't edit customer name directly (would need to select from users)
};
panel.Controls.Add(txtCustomerName);
// Service/Trip Name
Label lblServiceName = new Label
{
Text = "Service/Trip Name:",
Location = new Point(20, 60),
AutoSize = true,
Font = new Font("Segoe UI", 9F)
};
panel.Controls.Add(lblServiceName);
txtServiceName = new TextBox
{
Location = new Point(150, 60),
Size = new Size(300, 25),
Font = new Font("Segoe UI", 9F),
ReadOnly = true // Can't edit trip name directly (would need to select from trips)
};
panel.Controls.Add(txtServiceName);
// Booking Date
Label lblBookingDate = new Label
{
Text = "Booking Date:",
Location = new Point(20, 100),
AutoSize = true,
Font = new Font("Segoe UI", 9F)
};
panel.Controls.Add(lblBookingDate);
dtpBookingDate = new DateTimePicker
{
Location = new Point(150, 100),
Size = new Size(300, 25),
Format = DateTimePickerFormat.Short,
Font = new Font("Segoe UI", 9F)
};
panel.Controls.Add(dtpBookingDate);
// Participants
Label lblParticipants = new Label
{
Text = "Participants:",
Location = new Point(20, 140),
AutoSize = true,
Font = new Font("Segoe UI", 9F)
};
panel.Controls.Add(lblParticipants);
nudParticipants = new NumericUpDown
{
Location = new Point(150, 140),
Size = new Size(100, 25),
Minimum = 1,
Maximum = 20,
Value = 1,
Font = new Font("Segoe UI", 9F)
};
panel.Controls.Add(nudParticipants);
// Amount
Label lblAmount = new Label
{
Text = "Total Amount:",
Location = new Point(20, 180),
AutoSize = true,
Font = new Font("Segoe UI", 9F)
};
panel.Controls.Add(lblAmount);
nudAmount = new NumericUpDown
{
Location = new Point(150, 180),
Size = new Size(150, 25),
Minimum = 0,
Maximum = 1000000,
DecimalPlaces = 2,
Increment = 100,
ThousandsSeparator = true,
Font = new Font("Segoe UI", 9F)
};
panel.Controls.Add(nudAmount);
// Status
Label lblStatus = new Label
{
Text = "Status:",
Location = new Point(20, 220),
AutoSize = true,
Font = new Font("Segoe UI", 9F)
};
panel.Controls.Add(lblStatus);
cboStatus = new ComboBox
{
Location = new Point(150, 220),
Size = new Size(150, 25),
DropDownStyle = ComboBoxStyle.DropDownList,
Font = new Font("Segoe UI", 9F)
};
cboStatus.Items.AddRange(new object[] { "pending", "confirmed", "canceled" });
panel.Controls.Add(cboStatus);
// Special Requests
Label lblSpecialRequests = new Label
{
Text = "Special Requests:",
Location = new Point(20, 260),
AutoSize = true,
Font = new Font("Segoe UI", 9F)
};
panel.Controls.Add(lblSpecialRequests);
txtSpecialRequests = new TextBox
{
Location = new Point(150, 260),
Size = new Size(300, 80),
Multiline = true,
Font = new Font("Segoe UI", 9F)
};
panel.Controls.Add(txtSpecialRequests);
// Save button
btnSave = new Button
{
Text = "Save Changes",
Location = new Point(150, 350),
Size = new Size(140, 40),
BackColor = Color.FromArgb(41, 128, 185),
ForeColor = Color.White,
FlatStyle = FlatStyle.Flat,
Font = new Font("Segoe UI", 9F)
};
btnSave.Click += BtnSave_Click;
panel.Controls.Add(btnSave);
// Cancel button
btnCancel = new Button
{
Text = "Cancel",
Location = new Point(310, 350),
Size = new Size(140, 40),
BackColor = Color.FromArgb(231, 76, 60),
ForeColor = Color.White,
FlatStyle = FlatStyle.Flat,
Font = new Font("Segoe UI", 9F)
};
btnCancel.Click += (s, e) => this.DialogResult = DialogResult.Cancel;
panel.Controls.Add(btnCancel);
}
private void LoadBookingDetails()
{
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
// Get the booking details including traveler and trip information
string query = @"
SELECT b.*,
CONCAT(tp.first_name, ' ', tp.last_name) AS customer_name,
t.title AS trip_name
FROM Booking b
INNER JOIN TravelerProfile tp ON b.traveler_id = tp.user_id
INNER JOIN Trip t ON b.trip_id = t.trip_id
WHERE b.booking_id = @BookingId";
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@BookingId", bookingId);
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
// Store IDs for saving
travelerId = Convert.ToInt32(reader["traveler_id"]);
tripId = Convert.ToInt32(reader["trip_id"]);
// Populate form fields
txtCustomerName.Text = reader["customer_name"].ToString();
txtServiceName.Text = reader["trip_name"].ToString();
if (reader["booking_date"] != DBNull.Value)
{
dtpBookingDate.Value = Convert.ToDateTime(reader["booking_date"]);
}
if (reader["participants"] != DBNull.Value)
{
nudParticipants.Value = Convert.ToInt32(reader["participants"]);
}
if (reader["total_amount"] != DBNull.Value)
{
nudAmount.Value = Convert.ToDecimal(reader["total_amount"]);
}
string status = reader["status"].ToString();
cboStatus.SelectedItem = string.IsNullOrEmpty(status) ? "pending" : status;
txtSpecialRequests.Text = reader["special_requests"]?.ToString() ?? "";
}
else
{
MessageBox.Show("Booking not found!", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Close();
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Error loading booking details: {ex.Message}", "Database Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Close();
}
}
private void BtnSave_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
// Update the booking record
string updateQuery = @"
UPDATE Booking
SET booking_date = @BookingDate,
participants = @Participants,
total_amount = @TotalAmount,
status = @Status,
special_requests = @SpecialRequests
WHERE booking_id = @BookingId";
using (SqlCommand command = new SqlCommand(updateQuery, connection))
{
command.Parameters.AddWithValue("@BookingId", bookingId);
command.Parameters.AddWithValue("@BookingDate", dtpBookingDate.Value);
command.Parameters.AddWithValue("@Participants", (int)nudParticipants.Value);
command.Parameters.AddWithValue("@TotalAmount", nudAmount.Value);
command.Parameters.AddWithValue("@Status", cboStatus.SelectedItem.ToString());
if (string.IsNullOrWhiteSpace(txtSpecialRequests.Text))
command.Parameters.AddWithValue("@SpecialRequests", DBNull.Value);
else
command.Parameters.AddWithValue("@SpecialRequests", txtSpecialRequests.Text);
int rowsAffected = command.ExecuteNonQuery();
if (rowsAffected > 0)
{
MessageBox.Show("Booking updated successfully!", "Success",
MessageBoxButtons.OK, MessageBoxIcon.Information);
this.DialogResult = DialogResult.OK;
}
else
{
MessageBox.Show("No changes were made. The booking may have been deleted.",
"Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Error updating booking: {ex.Message}", "Database Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void btnRefresh_Click(object sender, EventArgs e)
{
LoadBookings();
}
private void dgvBookings_CellClick(object sender, DataGridViewCellEventArgs e)
{
// Ensure we're clicking on the Status column and not the header
if (e.ColumnIndex == dgvBookings.Columns["Status"].Index && e.RowIndex >= 0)
{
dgvBookings.BeginEdit(true);
((ComboBox)dgvBookings.EditingControl).DroppedDown = true;
}
}
// Navigation button handlers
private void btnRegistration_Click_1(object sender, EventArgs e)
{
ServiceIntegrationForm abc = new ServiceIntegrationForm();
abc.Show();
}
private void button1_Click(object sender, EventArgs e)
{
ServiceListingForm abc = new ServiceListingForm();
abc.Show();
}
private void button2_Click(object sender, EventArgs e)
{
// We're already on this form, but we could refresh it
LoadBookings();
}
private void button3_Click(object sender, EventArgs e)
{
PerformanceReportForm abc = new PerformanceReportForm();
abc.Show();
}
private void panelSidebar_Paint(object sender, PaintEventArgs e)
{
// This is an empty event handler
}
}
}