From 8bfa5ed1b3dac8187f7bd6cca4802befdecbaa58 Mon Sep 17 00:00:00 2001
From: 65160270 <65160270@go.buu.ac.th>
Date: Sat, 22 Mar 2025 23:48:36 +0700
Subject: [PATCH] update-address

---
 views/confirmation.ejs | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)

diff --git a/views/confirmation.ejs b/views/confirmation.ejs
index 0132cf6..9d3caef 100644
--- a/views/confirmation.ejs
+++ b/views/confirmation.ejs
@@ -6,7 +6,7 @@
     <a href="/" class="continue-shopping" >Continue Shopping</a>
 </div>
 <div class="info-section">
-    <h3>edit Address</h3>
+    <h3>Shipping Address</h3>
     <p>
         <span id="shipping-address"><%= order.shipping_address %></span>
         <input type="text" id="new-address" value="<%= order.shipping_address %>" style="display:none;">
@@ -14,4 +14,37 @@
     <button id="edit-btn">แก้ไขที่อยู่</button>
     <button id="save-btn" style="display:none;">บันทึก</button>
 </div>
+<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}`, {  // แก้ให้ตรง API
+            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>
 <%- include('partials/footer') %>
\ No newline at end of file
-- 
GitLab