From a5dd9c346c24e53a9f4f334931c7b9eda01cf06d Mon Sep 17 00:00:00 2001
From: 65160270 <65160270@go.buu.ac.th>
Date: Sat, 22 Mar 2025 21:32:22 +0700
Subject: [PATCH] update-address

---
 shop-routes/order.js | 15 +++++++------
 views/checkout.ejs   | 52 +++++++++++++++++++++++++++++++++++---------
 2 files changed, 50 insertions(+), 17 deletions(-)

diff --git a/shop-routes/order.js b/shop-routes/order.js
index 674b975..a409736 100644
--- a/shop-routes/order.js
+++ b/shop-routes/order.js
@@ -45,9 +45,11 @@ router.get('/history', isAuthenticated, async (req, res) => {
 // ดูรายละเอียดออเดอร์ (เฉพาะผู้ที่ Login)
 router.get('/detail/:orderId', isAuthenticated, async (req, res) => {
     try {
+        const sessionId = req.session.id;
+
         const [orders] = await pool.query(
-            'SELECT * FROM orders WHERE id = ? AND session_id = ?',
-            [req.params.orderId, req.session.id]
+            `SELECT * FROM orders WHERE id = ? AND session_id = ?`,
+            [req.params.orderId, sessionId]
         );
 
         if (orders.length === 0) {
@@ -57,11 +59,10 @@ router.get('/detail/:orderId', isAuthenticated, async (req, res) => {
         const order = orders[0];
 
         const [items] = await pool.query(
-            `SELECT 
-                order_items.*, products.name, products.image_url
-            FROM order_items
-            JOIN products ON order_items.product_id = products.id
-            WHERE order_items.order_id = ?`,
+            `SELECT order_items.*, products.name, products.image_url
+             FROM order_items
+             JOIN products ON order_items.product_id = products.id
+             WHERE order_items.order_id = ?`,
             [req.params.orderId]
         );
 
diff --git a/views/checkout.ejs b/views/checkout.ejs
index 3b0a712..b143dc0 100644
--- a/views/checkout.ejs
+++ b/views/checkout.ejs
@@ -8,16 +8,20 @@
     <% } else { %>
         <div class="order-summary">
             <h3>Order Summary</h3>
-            <% cartItems.forEach(item => { %>
-                <div class="order-item">
-                    <span><%= item.name %> x <%= item.quantity %></span>
-                    <span>฿<%= (item.price * item.quantity).toLocaleString() %></span>
+            <% if (cartItems.length > 0) { %>
+                <% cartItems.forEach(item => { %>
+                    <div class="order-item">
+                        <span><%= item.name %> x <%= item.quantity %></span>
+                        <span>฿<%= (item.price * item.quantity).toLocaleString() %></span>
+                    </div>
+                <% }); %>
+                <div class="total">
+                    <strong>Total:</strong>
+                    <strong>฿<%= total.toLocaleString() %></strong>
                 </div>
-            <% }); %>
-            <div class="total">
-                <strong>Total:</strong>
-                <strong>฿<%= total.toLocaleString() %></strong>
-            </div>
+            <% } else { %>
+                <p>Your cart is empty.</p>
+            <% } %>
         </div>
         
         <form action="/order/create" method="POST" class="checkout-form">
@@ -25,8 +29,36 @@
                 <label for="address">Shipping Address:</label>
                 <textarea name="address" required></textarea>
             </div>
-            <button type="submit" style="margin: 0; padding: 0.5rem 1rem;" > Place Order</button>
+            <button type="submit" style="margin: 0; padding: 0.5rem 1rem;">Place Order</button>
         </form>
+
+        <h3>Your Orders</h3>
+        <% if (orders.length > 0) { %>
+            <table border="1">
+                <thead>
+                    <tr>
+                        <th>Order ID</th>
+                        <th>Total Amount</th>
+                        <th>Status</th>
+                        <th>Shipping Address</th>
+                        <th>Created At</th>
+                    </tr>
+                </thead>
+                <tbody>
+                    <% orders.forEach(order => { %>
+                        <tr>
+                            <td><%= order.id %></td>
+                            <td>฿<%= order.total_amount.toLocaleString() %></td>
+                            <td><%= order.status %></td>
+                            <td><%= order.shipping_address %></td>
+                            <td><%= order.created_at %></td>
+                        </tr>
+                    <% }) %>
+                </tbody>
+            </table>
+        <% } else { %>
+            <p>No orders found.</p>
+        <% } %>
     <% } %>
 </div>
 
-- 
GitLab