diff --git a/controllers/tourController.js b/controllers/tourController.js index e46c6dcdfb7507376002a7335de1ccd3b153b722..ada92137e58974fe4f76bf109dcb3c2beade1b06 100644 --- a/controllers/tourController.js +++ b/controllers/tourController.js @@ -283,29 +283,29 @@ exports.getAllTours = async (req, res) => { } }; +// ดึงข้อมูลการจองพร้อมชื่อผู้จอง exports.getUserBookings = async (req, res) => { - const userId = req.session.userId; // ตรวจสอบการใช้งาน session สำหรับ userId + const userId = req.session.userId; // สมมติว่าใช้ session สำหรับจัดการผู้ใช้ - // ตรวจสอบว่า userId ถูกต้องหรือไม่ if (!userId) { - return res.status(400).send('User not logged in.'); + return res.redirect('/login'); // ถ้าไม่ได้ล็อกอิน ให้ไปหน้า login } try { - // ดึงข้อมูลการจองของผู้ใช้จากฐานข้อมูล const query = ` - SELECT b.id, t.name AS tour_name, t.price, b.booking_date + SELECT b.id AS booking_id, t.name AS tour_name, b.status, u.name AS user_name FROM bookings b JOIN tours t ON b.tour_id = t.id + JOIN users u ON b.user_id = u.id WHERE b.user_id = ? ORDER BY b.booking_date DESC `; const [bookings] = await pool.query(query, [userId]); - // ส่งข้อมูลการจองกลับไปยังผู้ใช้ - res.render('bookinglist', { bookings }); + // ส่งข้อมูลการจองไปยังหน้า EJS + res.render('myBookings', { bookings: bookings }); } catch (error) { - console.error('Error fetching user bookings:', error.message); + console.error('Error fetching bookings:', error.message); res.status(500).send('เกิดข้อผิดพลาดในการดึงข้อมูลการจอง'); } }; diff --git a/public/css/booklist.css b/public/css/booklist.css new file mode 100644 index 0000000000000000000000000000000000000000..b3224fe68357d90c8e6826f1e4a35044ef18fdbc --- /dev/null +++ b/public/css/booklist.css @@ -0,0 +1,55 @@ +/* booking.css */ +body { + font-family: Arial, sans-serif; + background-color: #f4f4f9; + margin: 20px; + padding: 0; + text-align: center; +} + +h1 { + color: #333; + margin-bottom: 20px; +} + +table { + width: 80%; + margin: 0 auto; + border-collapse: collapse; + background-color: white; + box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1); + border-radius: 8px; + overflow: hidden; +} + +th, td { + padding: 12px; + border: 1px solid #ddd; + text-align: center; +} + +th { + background-color: #007bff; + color: white; +} + +tr:nth-child(even) { + background-color: #f2f2f2; +} + +tr:hover { + background-color: #ddd; + cursor: pointer; +} + +/* Responsive */ +@media (max-width: 768px) { + table { + width: 100%; + } + + th, td { + padding: 8px; + font-size: 14px; + } +} diff --git a/views/bookinglist.ejs b/views/bookinglist.ejs index e03ac494a9dff32c14792584d39e954848afa433..794511b81a342c300baa615124c2134eb8e52f5f 100644 --- a/views/bookinglist.ejs +++ b/views/bookinglist.ejs @@ -4,6 +4,7 @@ <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>My Bookings</title> + <link rel="stylesheet" href="/css/booklist.css"> </head> <body> <h1>Your Bookings</h1> @@ -11,16 +12,16 @@ <thead> <tr> <th>Booking ID</th> + <th>Booking User</th> <th>Tour Title</th> - <th>Status</th> </tr> </thead> <tbody> <% bookings.forEach(booking => { %> <tr> - <td><%= booking.id %></td> <!-- booking_id --> + <td><%= booking.id %></td> <!-- booking_id --> + <td><%= booking.user_name %></td> <!-- ชื่อผู้จอง --> <td><%= booking.tour_name %></td> <!-- tour_title --> - <td><%= booking.status %></td> </tr> <% }) %> </tbody>