From aaa6d1eb5169d266612c641463465ce651711efd Mon Sep 17 00:00:00 2001
From: 65160111 <65160111@go.buu.ac.th>
Date: Mon, 24 Mar 2025 19:27:15 +0700
Subject: [PATCH] =?UTF-8?q?=E0=B9=80=E0=B8=9E=E0=B8=B4=E0=B9=88=E0=B8=A1?=
 =?UTF-8?q?=20dropdown=20editreport=20=E0=B9=81=E0=B8=A5=E0=B9=89=E0=B8=A7?=
 =?UTF-8?q?=E0=B8=81=E0=B9=87=E0=B9=81=E0=B8=81=E0=B9=89=20script.js?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .env                    |  6 ++---
 public/edit_report.html | 24 +++++++++++++++----
 public/script.js        | 53 ++++++++++++++++-------------------------
 3 files changed, 43 insertions(+), 40 deletions(-)

diff --git a/.env b/.env
index 869ed1f..20f2165 100644
--- a/.env
+++ b/.env
@@ -1,6 +1,6 @@
 PORT=8080
-DB_HOST=10.104.21.172
+DB_HOST=localhost
 DB_USER=root
-DB_PASS=MECyve10150
+DB_PASS=
 DB_NAME=repair_system
-JWT_SECRET=chanut
+
diff --git a/public/edit_report.html b/public/edit_report.html
index 0ff0cc9..2604956 100644
--- a/public/edit_report.html
+++ b/public/edit_report.html
@@ -9,7 +9,6 @@
   
   <!-- Custom CSS -->
   <style>
-    /* เอฟเฟกต์กระพริบ */
     @keyframes blinking {
       0% { opacity: 1; }
       50% { opacity: 0; }
@@ -21,7 +20,6 @@
   </style>
 </head>
 <body class="bg-light">
-
   <div class="container d-flex flex-column justify-content-center align-items-center min-vh-100">
     <!-- ข้อความ IF-REPAIRS ด้านนอก Card -->
     <h1 class="text-center blinking mb-4" style="font-size: 2.5rem;">
@@ -31,7 +29,6 @@
     <!-- Card สำหรับฟอร์มแก้ไข -->
     <div class="card p-4 shadow" style="width: 100%; max-width: 500px;">
       <h2 class="card-title text-center mb-4">แก้ไขรายการแจ้งซ่อม</h2>
-
       <form id="editReportForm">
         <input type="hidden" id="reportId">
         
@@ -46,7 +43,26 @@
           <label for="editDescription" class="form-label">รายละเอียด:</label>
           <textarea id="editDescription" class="form-control" rows="3" required></textarea>
         </div>
-
+        
+        <!-- Dropdown เลือกชั้น -->
+        <div class="mb-3">
+          <label for="floor" class="form-label">เลือกชั้น:</label>
+          <select id="floor" class="form-select" onchange="loadRooms();">
+            <option value="">-- กรุณาเลือกชั้น --</option>
+            <option value="2">ชั้น 2</option>
+            <option value="3">ชั้น 3</option>
+            <option value="4">ชั้น 4</option>
+          </select>
+        </div>
+        
+        <!-- Dropdown เลือกห้อง -->
+        <div class="mb-3">
+          <label for="room" class="form-label">เลือกห้อง:</label>
+          <select id="room" class="form-select">
+            <option value="">-- กรุณาเลือกห้อง --</option>
+          </select>
+        </div>
+        
         <!-- ปุ่มยืนยันการแก้ไข / ยกเลิก -->
         <div class="d-grid gap-2">
           <button type="submit" class="btn btn-primary">ยืนยันการแก้ไข</button>
diff --git a/public/script.js b/public/script.js
index 79e945b..0031864 100644
--- a/public/script.js
+++ b/public/script.js
@@ -79,11 +79,12 @@ function goToReportForm() {
 // ====================
 
 // 🔹 โหลดห้องเรียนตามชั้นที่เลือก (สำหรับ report_form.html หรือ dashboard.html)
-function loadRooms() {
+function loadRooms(callback) {
     const floor = document.getElementById("floor").value;
     const roomSelect = document.getElementById("room");
     if (!floor) {
         roomSelect.innerHTML = '<option value="">-- กรุณาเลือกห้อง --</option>';
+        if(callback) callback();
         return;
     }
     fetch(`${API_URL}/reports/rooms/${floor}`, {
@@ -99,10 +100,15 @@ function loadRooms() {
         } else {
             roomSelect.innerHTML = '<option value="">ไม่มีห้องในชั้นนี้</option>';
         }
+        if(callback) callback();
     })
-    .catch(error => console.error("❌ Load Rooms Error:", error));
+    .catch(error => {
+        console.error("❌ Load Rooms Error:", error);
+        if(callback) callback();
+    });
 }
 
+
 // 🔹 โหลดรายการแจ้งซ่อม (กรองตาม search, floor, และ room)
 function loadReports() {
     const searchTerm = document.getElementById("search") ? document.getElementById("search").value.toLowerCase() : "";
@@ -185,40 +191,18 @@ function loadEditReport() {
         document.getElementById("reportId").value = report.id;
         document.getElementById("editTitle").value = report.title;
         document.getElementById("editDescription").value = report.description;
+        // ตั้งค่า dropdown ชั้น และโหลดรายการห้อง จากนั้นตั้งค่าห้องให้ตรงกับ report
+        if(document.getElementById("floor")){
+            document.getElementById("floor").value = report.floor;
+            loadRooms(function(){
+                if(document.getElementById("room"))
+                    document.getElementById("room").value = report.room_id;
+            });
+        }
     })
     .catch(error => console.error("❌ Load Report Error:", error));
 }
 
-document.getElementById("editReportForm")?.addEventListener("submit", function(event) {
-    event.preventDefault();
-
-    const reportId = document.getElementById("reportId").value;
-    const title = document.getElementById("editTitle").value;
-    const description = document.getElementById("editDescription").value;
-
-    fetch(`${API_URL}/reports/${reportId}`, {
-        method: "PUT",
-        headers: { 
-            "Content-Type": "application/json",
-            "Authorization": `Bearer ${localStorage.getItem("token")}`
-        },
-        body: JSON.stringify({ title, description })
-    })
-    .then(res => res.json())
-    .then(data => {
-        alert(data.message);
-        window.location.href = "dashboard.html";
-    })
-    .catch(error => console.error("❌ Update Report Error:", error));
-});
-
-function cancelEdit() {
-    window.location.href = "dashboard.html";
-}
-
-if (window.location.pathname.includes("edit_report.html")) {
-    loadEditReport();
-}
 
 
 // 🔹 ส่งข้อมูลแจ้งซ่อม (สำหรับ report_form.html)
@@ -255,7 +239,10 @@ function submitReport() {
             alert("เกิดข้อผิดพลาด กรุณาลองใหม่");
         }
     })
-    .catch(error => console.error("❌ API Error:", error));
+    .catch(error => {
+        console.error("❌ Load Report Error:", error);
+        alert("เกิดข้อผิดพลาดในการโหลดข้อมูลแจ้งซ่อม");
+    });
 }
 
 // 🔹 ยกเลิกการแจ้งซ่อม (สำหรับ report_form.html)
-- 
GitLab