-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm9.cs
More file actions
364 lines (313 loc) · 14.3 KB
/
Copy pathForm9.cs
File metadata and controls
364 lines (313 loc) · 14.3 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
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
namespace H1
{
public partial class Form9 : Form
{
private string connectionString = @"Data Source=ABDULSABOOR190\SQLEXPRESS;Initial Catalog=sabbbb;Integrated Security=True;Encrypt=False";
// Colors based on the provided specs
private Color navbarColor = Color.FromArgb(44, 62, 80);
private Color headerColor = Color.FromArgb(52, 152, 219);
private Color whiteColor = Color.White;
private Color lightGrayColor = Color.FromArgb(240, 240, 240);
// Current selected trip for booking
private DataRow selectedTrip = null;
public Form9()
{
InitializeComponent();
LoadInitialData();
}
private void LoadInitialData()
{
// Load destinations from database
LoadDestinationsFromDB();
// Load categories from database
LoadCategoriesFromDB();
// Load trips from database
LoadTripsFromDB();
// Payment methods remain hardcoded
string[] paymentMethods = { "Credit Card", "Debit Card", "PayPal", "Bank Transfer" };
cmbPaymentMethod.Items.AddRange(paymentMethods);
}
private void LoadDestinationsFromDB()
{
try
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
string query = "SELECT DISTINCT destination FROM Trip WHERE is_active = 1";
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
cmbDestination.Items.Add(reader["destination"].ToString());
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error loading destinations: " + ex.Message);
// Fallback to hardcoded values
string[] destinations = { "Paris", "London", "Tokyo", "New York" };
cmbDestination.Items.AddRange(destinations);
}
}
private void LoadCategoriesFromDB()
{
try
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
string query = "SELECT name FROM TourCategory";
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
cmbCategory.Items.Add(reader["name"].ToString());
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error loading categories: " + ex.Message);
// Fallback to hardcoded values
string[] categories = { "Adventure", "Cultural", "Beach" };
cmbCategory.Items.AddRange(categories);
}
}
private void LoadTripsFromDB()
{
try
{
DataTable trips = new DataTable();
using (SqlConnection conn = new SqlConnection(connectionString))
{
string query = @"
SELECT
t.trip_id AS ID,
t.title AS Title,
t.destination AS Destination,
FORMAT(t.start_date, 'dd-MMM-yyyy') AS StartDate,
FORMAT(t.end_date, 'dd-MMM-yyyy') AS EndDate,
'$' + CONVERT(VARCHAR, t.price) AS Price,
t.max_capacity AS Available,
cat.name AS Category
FROM Trip t
LEFT JOIN TripCategory tc ON t.trip_id = tc.trip_id
LEFT JOIN TourCategory cat ON tc.category_id = cat.category_id
WHERE t.is_active = 1";
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(trips);
}
dgvSearchResults.DataSource = trips;
FormatSearchResultsGrid();
}
catch (Exception ex)
{
MessageBox.Show("Error loading trips: " + ex.Message);
LoadSampleTrips(); // Fallback to your original sample data method
}
}
private void LoadSampleTrips()
{
DataTable trips = new DataTable();
trips.Columns.Add("ID", typeof(int));
trips.Columns.Add("Title", typeof(string));
trips.Columns.Add("Destination", typeof(string));
trips.Columns.Add("StartDate", typeof(string));
trips.Columns.Add("EndDate", typeof(string));
trips.Columns.Add("Price", typeof(string));
trips.Columns.Add("Available", typeof(int));
trips.Columns.Add("Category", typeof(string));
// Add sample trips
trips.Rows.Add(1, "Paris City Tour", "Paris", "15-May-2025", "20-May-2025", "$1200", 25, "City Tour");
trips.Rows.Add(2, "London Explorer", "London", "01-Jun-2025", "07-Jun-2025", "$1500", 15, "Cultural");
trips.Rows.Add(3, "Tokyo Adventure", "Tokyo", "10-Jul-2025", "20-Jul-2025", "$2500", 10, "Adventure");
trips.Rows.Add(4, "Rome History Tour", "Rome", "05-Aug-2025", "12-Aug-2025", "$1800", 20, "Historical");
dgvSearchResults.DataSource = trips;
FormatSearchResultsGrid();
}
private void FormatSearchResultsGrid()
{
dgvSearchResults.Columns["ID"].Visible = false;
dgvSearchResults.Columns["Category"].Visible = false;
dgvSearchResults.Columns["Price"].HeaderText = "Price (per person)";
dgvSearchResults.Columns["Available"].HeaderText = "Available Places";
dgvSearchResults.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dgvSearchResults.RowHeadersVisible = false;
dgvSearchResults.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
}
private void btnSearch_Click(object sender, EventArgs e)
{
try
{
DataTable trips = new DataTable();
using (SqlConnection conn = new SqlConnection(connectionString))
{
string query = @"
SELECT
t.trip_id AS ID,
t.title AS Title,
t.destination AS Destination,
FORMAT(t.start_date, 'dd-MMM-yyyy') AS StartDate,
FORMAT(t.end_date, 'dd-MMM-yyyy') AS EndDate,
'$' + CONVERT(VARCHAR, t.price) AS Price,
t.max_capacity AS Available,
cat.name AS Category
FROM Trip t
LEFT JOIN TripCategory tc ON t.trip_id = tc.trip_id
LEFT JOIN TourCategory cat ON tc.category_id = cat.category_id
WHERE t.is_active = 1";
// Dynamic WHERE clause
List<string> conditions = new List<string>();
List<SqlParameter> parameters = new List<SqlParameter>();
if (cmbDestination.SelectedItem != null)
{
conditions.Add("t.destination = @destination");
parameters.Add(new SqlParameter("@destination", cmbDestination.SelectedItem.ToString()));
}
if (cmbCategory.SelectedItem != null)
{
conditions.Add("cat.name = @category");
parameters.Add(new SqlParameter("@category", cmbCategory.SelectedItem.ToString()));
}
if (dtpStartDate.Checked)
{
conditions.Add("t.start_date >= @startDate");
parameters.Add(new SqlParameter("@startDate", dtpStartDate.Value));
}
if (dtpEndDate.Checked)
{
conditions.Add("t.end_date <= @endDate");
parameters.Add(new SqlParameter("@endDate", dtpEndDate.Value));
}
if (conditions.Count > 0)
{
query += " AND " + string.Join(" AND ", conditions);
}
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.AddRange(parameters.ToArray());
conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(trips);
}
dgvSearchResults.DataSource = trips;
}
catch (Exception ex)
{
MessageBox.Show("Error searching trips: " + ex.Message);
}
}
private void dgvSearchResults_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
// Get selected trip data
DataGridViewRow row = dgvSearchResults.Rows[e.RowIndex];
selectedTrip = ((DataRowView)row.DataBoundItem).Row;
// Update booking tab with selected trip
lblSelectedTrip.Text = selectedTrip["Title"].ToString();
lblTripDetails.Text = $"Destination: {selectedTrip["Destination"]}\n" +
$"Dates: {selectedTrip["StartDate"]} to {selectedTrip["EndDate"]}\n" +
$"Price per person: {selectedTrip["Price"]}\n" +
$"Category: {selectedTrip["Category"]}";
// Calculate initial price
UpdateTotalPrice();
// Enable booking controls
nudBookingParticipants.Enabled = true;
cmbPaymentMethod.Enabled = true;
btnConfirmBooking.Enabled = true;
// Switch to booking tab
tabControl.SelectedTab = tabBooking;
}
}
private void UpdateTotalPrice()
{
if (selectedTrip != null)
{
string priceStr = selectedTrip["Price"].ToString().Replace("$", "");
if (decimal.TryParse(priceStr, out decimal pricePerPerson))
{
decimal totalPrice = pricePerPerson * nudBookingParticipants.Value;
txtTotalPrice.Text = $"${totalPrice}";
}
}
}
private void nudBookingParticipants_ValueChanged(object sender, EventArgs e)
{
UpdateTotalPrice();
}
private void btnConfirmBooking_Click(object sender, EventArgs e)
{
if (cmbPaymentMethod.SelectedItem == null)
{
MessageBox.Show("Please select a payment method", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
try
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
// 1. Create booking
string bookingQuery = @"
INSERT INTO Booking
(traveler_id, trip_id, participants, total_amount, status)
VALUES (@travelerId, @tripId, @participants, @totalAmount, 'confirmed');
SELECT SCOPE_IDENTITY();";
decimal totalPrice = decimal.Parse(txtTotalPrice.Text.Replace("$", ""));
SqlCommand bookingCmd = new SqlCommand(bookingQuery, conn);
bookingCmd.Parameters.AddWithValue("@travelerId", 1); // Replace with actual user ID
bookingCmd.Parameters.AddWithValue("@tripId", selectedTrip["ID"]);
bookingCmd.Parameters.AddWithValue("@participants", nudBookingParticipants.Value);
bookingCmd.Parameters.AddWithValue("@totalAmount", totalPrice);
int bookingId = Convert.ToInt32(bookingCmd.ExecuteScalar());
// 2. Create payment record
string paymentQuery = @"
INSERT INTO Payment
(booking_id, amount, payment_method, transaction_id, status)
VALUES (@bookingId, @amount, @method, NEWID(), 'completed')";
string paymentMethod = cmbPaymentMethod.SelectedItem.ToString()
.ToLower()
.Replace(" ", "_");
SqlCommand paymentCmd = new SqlCommand(paymentQuery, conn);
paymentCmd.Parameters.AddWithValue("@bookingId", bookingId);
paymentCmd.Parameters.AddWithValue("@amount", totalPrice);
paymentCmd.Parameters.AddWithValue("@method", paymentMethod);
paymentCmd.ExecuteNonQuery();
MessageBox.Show($"Booking #{bookingId} confirmed!\nTotal: {txtTotalPrice.Text}",
"Success",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
ResetBookingForm();
tabControl.SelectedTab = tabSearch;
LoadTripsFromDB(); // Refresh the trip list
}
}
catch (Exception ex)
{
MessageBox.Show("Error saving booking: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ResetBookingForm()
{
lblSelectedTrip.Text = "(No trip selected)";
lblTripDetails.Text = "Please select a trip from the Search tab first";
nudBookingParticipants.Value = 1;
txtTotalPrice.Text = "";
cmbPaymentMethod.SelectedIndex = -1;
nudBookingParticipants.Enabled = false;
cmbPaymentMethod.Enabled = false;
btnConfirmBooking.Enabled = false;
selectedTrip = null;
}
}
}