diff --git a/shop-routes/order.js b/shop-routes/order.js index e49a609162f923b69b90b5c3726f1f5062a7f0a1..df0b4817f5beb99471dfdadc784267c528162758 100644 --- a/shop-routes/order.js +++ b/shop-routes/order.js @@ -42,44 +42,39 @@ router.get('/history', isAuthenticated, async (req, res) => { } }); -// ดูรายละเอียดออเดอร์ (เฉพาะผู้ที่ Login) -router.get('/detail/:orderId', async (req, res) => { +// อัปเดตที่อยู่จัดส่งของคำสั่งซื้อ +router.put('/update/:orderId', async (req, res) => { try { - console.log("Checking Order Details..."); - console.log("Session ID:", req.session.id); - console.log("Order ID:", req.params.orderId); + const { orderId } = req.params; + const { shipping_address } = req.body; - const [orders] = await pool.query( - 'SELECT * FROM orders WHERE id = ? AND session_id = ?', - [req.params.orderId, req.session.id] - ); - - console.log("Orders Result:", orders); + console.log("Updating Shipping Address..."); + console.log("Session ID:", req.session.id); + console.log("Order ID:", orderId); + console.log("New Address:", shipping_address); - if (orders.length === 0) { - return res.status(404).json({ message: 'Order not found' }); + if (!shipping_address) { + return res.status(400).json({ message: "กรุณากรอกที่อยู่จัดส่ง" }); } - 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 = ?`, - [req.params.orderId] + // อัปเดตที่อยู่เฉพาะของคำสั่งซื้อที่เป็นของ session นั้นๆ + const [result] = await pool.query( + "UPDATE orders SET shipping_address = ? WHERE id = ? AND session_id = ?", + [shipping_address, orderId, req.session.id] ); - console.log("Order Items:", items); + if (result.affectedRows === 0) { + return res.status(404).json({ message: "ไม่พบคำสั่งซื้อนี้ หรือไม่มีสิทธิ์แก้ไข" }); + } - res.json({ order, items }); + res.json({ message: "อัปเดตที่อยู่สำเร็จ!" }); } catch (error) { console.error("ERROR:", error); - res.status(500).json({ message: 'Something went wrong.', error: error.message }); + res.status(500).json({ message: "เกิดข้อผิดพลาด", error: error.message }); } }); + // ป้องกันไม่ให้เข้า Checkout ถ้าไม่ได้ Login router.get('/checkout', isAuthenticated, async (req, res) => { try { diff --git a/views/order-details.ejs b/views/order-details.ejs index d35b58627bc886272fcbd4493f131a346a5faafe..c9ebb3751f3806b3b52bb3040f2421c0decc2f89 100644 --- a/views/order-details.ejs +++ b/views/order-details.ejs @@ -11,7 +11,12 @@ <div class="info-section"> <h3>Shipping Address</h3> - <p><%= order.shipping_address %></p> + <p> + <span id="shipping-address"><%= order.shipping_address %></span> + <input type="text" id="new-address" value="<%= order.shipping_address %>" style="display:none;"> + </p> + <button id="edit-btn">แก้ไขที่อยู่</button> + <button id="save-btn" style="display:none;">บันทึก</button> </div> </div> @@ -37,4 +42,38 @@ <a href="/" class="continue-shopping">Continue Shopping</a> </div> </div> -<%- include('partials/footer') %> \ No newline at end of file +<%- include('partials/footer') %> + +<script> +document.getElementById("edit-btn").addEventListener("click", function() { + document.getElementById("new-address").style.display = "inline"; + document.getElementById("save-btn").style.display = "inline"; + document.getElementById("edit-btn").style.display = "none"; // ซ่อนปุ่มแก้ไข +}); + +document.getElementById("save-btn").addEventListener("click", async function() { + const newAddress = document.getElementById("new-address").value.trim(); + if (!newAddress) { + alert("กรุณากรอกที่อยู่ใหม่"); + return; + } + + const orderId = "<%= order.id %>"; + + const response = await fetch(`/order/update/${orderId}`, { + method: "PUT", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ shipping_address: newAddress }) + }); + + if (response.ok) { + document.getElementById("shipping-address").innerText = newAddress; + document.getElementById("new-address").style.display = "none"; + document.getElementById("save-btn").style.display = "none"; + document.getElementById("edit-btn").style.display = "inline"; // แสดงปุ่มแก้ไขกลับมา + alert("อัปเดตที่อยู่สำเร็จ!"); + } else { + alert("เกิดข้อผิดพลาด!"); + } +}); +</script> \ No newline at end of file