From 4ff2ea284cb9a1b6e884b4209791ae9e6d542b6d Mon Sep 17 00:00:00 2001
From: 65160024 <65160024@go.buu.ac.th>
Date: Tue, 25 Mar 2025 03:58:32 +0000
Subject: [PATCH] update confirm loan

---
 views/confirm-loan.ejs | 65 ++++++++++++++++++++++++++++++------------
 1 file changed, 46 insertions(+), 19 deletions(-)

diff --git a/views/confirm-loan.ejs b/views/confirm-loan.ejs
index c9a3750..25fcb9c 100644
--- a/views/confirm-loan.ejs
+++ b/views/confirm-loan.ejs
@@ -19,10 +19,10 @@
                     <a href="/" class="text-white hover:text-gray-200">หน้าหลัก</a>
                     <a href="/loans" class="text-white hover:text-gray-200">รวมการยืมอุปกรณ์</a>
                     <a href="/logout" class="text-white hover:text-gray-200">ออกจากระบบ</a>
-                    <% } else { %>
-                        <a href="/login" class="text-white hover:text-gray-200">เข้าสู่ระบบ</a>
-                        <a href="/register" class="text-white hover:text-gray-200">สมัครสมาชิก</a>
-                        <% } %>
+                <% } else { %>
+                    <a href="/login" class="text-white hover:text-gray-200">เข้าสู่ระบบ</a>
+                    <a href="/register" class="text-white hover:text-gray-200">สมัครสมาชิก</a>
+                <% } %>
             </div>
         </div>
     </nav>
@@ -43,27 +43,54 @@
 
             <div class="flex justify-center space-x-4">
                 <!-- ฟอร์มยืนยันการยืม -->
-                <form action="/submit-loan" method="POST" class="flex space-x-2">
-                    <input type="hidden" name="equipment_id" value="<%= equipment.id %>">
-                    <input type="hidden" name="quantity" value="<%= quantity %>">
-                    <button type="submit"
-                        class="bg-blue-600 text-white px-6 py-3 rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-400">ยืนยันการยืม</button>
-                </form>
+                <button id="confirmButton" class="bg-blue-600 text-white px-6 py-3 rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-400">
+                    ยืนยันการยืม
+                </button>
 
                 <!-- ฟอร์มยกเลิกการยืม -->
                 <form action="/cancel-loan" method="GET" class="flex space-x-2">
-                    <button type="submit"
-                        class="bg-gray-300 text-gray-800 px-6 py-3 rounded-md hover:bg-gray-400 focus:outline-none focus:ring-2 focus:ring-gray-400">ยกเลิก</button>
+                    <button type="submit" class="bg-gray-300 text-gray-800 px-6 py-3 rounded-md hover:bg-gray-400 focus:outline-none focus:ring-2 focus:ring-gray-400">ยกเลิก</button>
                 </form>
             </div>
         </div>
-        <% } else { %>
-            <!-- ถ้าไม่ใช่ผู้ใช้ที่ล็อกอิน -->
-            <div class="container mx-auto p-6">
-                <p class="text-xl text-center text-gray-500">คุณต้องเข้าสู่ระบบก่อนทำการยืมอุปกรณ์</p>
-            </div>
-            <% } %>
+    <% } else { %>
+        <!-- ถ้าไม่ใช่ผู้ใช้ที่ล็อกอิน -->
+        <div class="container mx-auto p-6">
+            <p class="text-xl text-center text-gray-500">คุณต้องเข้าสู่ระบบก่อนทำการยืมอุปกรณ์</p>
+        </div>
+    <% } %>
+
+    <script>
+        // เมื่อกดปุ่ม "ยืนยันการยืม" ให้เปลี่ยนเส้นทางไปยังหน้า /loans
+        document.getElementById('confirmButton').addEventListener('click', function() {
+            // ส่งข้อมูลยืมไปยังเซิร์ฟเวอร์ (ในกรณีที่ต้องการให้มีการบันทึกข้อมูลก่อน)
+            const equipment_id = "<%= equipment.id %>";
+            const quantity = "<%= quantity %>";
+
+            fetch('/submit-loan', {
+                method: 'POST',
+                headers: {
+                    'Content-Type': 'application/json',
+                },
+                body: JSON.stringify({
+                    equipment_id: equipment_id,
+                    quantity: quantity
+                })
+            })
+            .then(response => {
+                if (response.ok) {
+                    // หลังจากยืนยันเสร็จแล้วเปลี่ยนเส้นทางไปที่ /loans
+                    window.location.href = '/loans';
+                } else {
+                    alert('การยืมอุปกรณ์ล้มเหลว');
+                }
+            })
+            .catch(error => {
+                alert('เกิดข้อผิดพลาด: ' + error);
+            });
+        });
+    </script>
 
 </body>
 
-</html>
\ No newline at end of file
+</html>
-- 
GitLab