diff --git a/src/main/java/com/cardiff/client_project/controller/admin/CommonAdminHospitalController.java b/src/main/java/com/cardiff/client_project/controller/admin/CommonAdminHospitalController.java index 9e6c8c4ebd38bde7528df3fba4a4d58681f6bf3e..5386a9724b6a2a06890669514efa487661d7369d 100644 --- a/src/main/java/com/cardiff/client_project/controller/admin/CommonAdminHospitalController.java +++ b/src/main/java/com/cardiff/client_project/controller/admin/CommonAdminHospitalController.java @@ -5,6 +5,7 @@ import com.cardiff.client_project.service.CommonAdminHospitalService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.web.bind.annotation.*; import java.util.List; @@ -37,6 +38,18 @@ public class CommonAdminHospitalController { } } + @GetMapping("/currentUser") + public ResponseEntity<String> getCurrentUser() { + try { + + String username = SecurityContextHolder.getContext().getAuthentication().getName(); + return ResponseEntity.ok(username); + } catch (Exception e) { + e.printStackTrace(); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body("Unable to fetch current user."); + } + } @PutMapping("/update") diff --git a/src/main/resources/static/css/admin.css b/src/main/resources/static/css/admin.css index ed362d59406e6f4e8d640f073e6841be53c20b8d..b5acb2260ee53ca8d95a1e508b3eb82b2539ff2e 100644 --- a/src/main/resources/static/css/admin.css +++ b/src/main/resources/static/css/admin.css @@ -15,7 +15,7 @@ body { padding: 1rem; display: flex; flex-direction: column; - justify-content: space-between; + gap: 0.5rem; /* 为按钮之间增加间距 */ } .sidebar .text-lg { font-size: 1.25rem; diff --git a/src/main/resources/static/html/admin.html b/src/main/resources/static/html/admin.html index c75d87eb82930e24d280961a8baa069d7f47d59a..4573c5f702aafe603f2bc584b4938ae61deeb144 100644 --- a/src/main/resources/static/html/admin.html +++ b/src/main/resources/static/html/admin.html @@ -11,9 +11,12 @@ <div class="text-lg font-semibold">System</div> <a href="#" data-content="hospital">Hospital</a> <a href="#" data-content="patient">Nursing staff</a> - <a href="#" data-content="logout">LogOut</a> + <a><div onclick="chooseOut()" >Sign Out</div></a> </nav> <div class="content"> + + <div id="user-info" style="text-align: right; padding: 10px; font-weight: bold;"></div> + <div id="dynamic-content"> <h1>Welcome to the Management System</h1> <p>Select an option from the sidebar to get started.</p> @@ -42,9 +45,36 @@ </div> <script> - const dynamicContent = document.getElementById("dynamic-content"); + // Load the current login user information + function loadCurrentUser() { + fetch('/commonAdmin/hospital/currentUser') + .then(response => { + if (!response.ok) { + throw new Error("Failed to fetch current user."); + } + return response.text(); + }) + .then(username => { + if (!username) { + throw new Error("No username received from server."); + } + document.getElementById('user-info').innerText = `Logged in as: ${username}`; + }) + .catch(error => { + console.error("Error fetching current user:", error); + document.getElementById('user-info').innerText = "Unable to fetch user info."; + }); + } + + function chooseOut(){ + window.location.href="/logout" + } + + // Initialize load user information + loadCurrentUser(); + function loadHospitalInterface() { dynamicContent.innerHTML = ` <div id="toolbar"> @@ -89,7 +119,6 @@ loadHospitals(); } - function openAddHospitalModal() { document.getElementById('modal-title').innerText = "Add New Hospital"; document.getElementById('hospital-modal').style.display = 'flex'; @@ -121,8 +150,6 @@ availableBeds: parseInt(availableBeds, 10), }; - console.log("Sending data to backend:", newHospital); // Print the sent data - fetch('/commonAdmin/hospital/add', { method: 'POST', headers: { 'Content-Type': 'application/json' }, @@ -138,14 +165,11 @@ } }) .catch((error) => { - console.error(error); // Print error message + console.error(error); alert("An error occurred while adding the hospital."); }); } - - - // Load hospital list function loadHospitals(filters = {}) { const query = new URLSearchParams(filters).toString(); fetch(`/commonAdmin/hospital/all?${query}`) @@ -181,69 +205,7 @@ .catch((error) => console.error(error)); } - // Processing editing hospital - function handleEditHospital(event) { - const id = event.target.dataset.id; - - fetch(`/commonAdmin/hospital/all`) - .then((response) => response.json()) - .then((data) => { - const hospital = data.find((h) => h.id === parseInt(id, 10)); - - if (hospital) { - document.getElementById('modal-title').innerText = "Edit Hospital"; - document.getElementById('modal-name').value = hospital.name; - document.getElementById('modal-location').value = hospital.location; - document.getElementById('modal-phone').value = hospital.phone; - document.getElementById('modal-totalBeds').value = hospital.totalBeds; - document.getElementById('modal-availableBeds').value = hospital.availableBeds; - - document.getElementById('hospital-modal').style.display = 'flex'; - - document.getElementById('save-hospital-btn').onclick = () => saveEditedHospital(id); - } - }) - .catch((error) => console.error('Error loading hospital data:', error)); - } - - // Save the edited hospital information - function saveEditedHospital(id) { - const updatedHospital = { - id: parseInt(id, 10), - name: document.getElementById('modal-name').value, - location: document.getElementById('modal-location').value, - phone: document.getElementById('modal-phone').value, - totalBeds: parseInt(document.getElementById('modal-totalBeds').value, 10), - availableBeds: parseInt(document.getElementById('modal-availableBeds').value, 10), - }; - - fetch('/commonAdmin/hospital/update', { - method: 'PUT', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify(updatedHospital), - }) - .then(() => { - alert('Hospital updated successfully!'); - document.getElementById('hospital-modal').style.display = 'none'; - loadHospitals(); - }) - .catch((error) => console.error('Error updating hospital:', error)); - } - - // Delete hospital - function handleDeleteHospital(event) { - const id = event.target.dataset.id; - fetch(`/commonAdmin/hospital/delete/${id}`, { method: 'DELETE' }) - .then(() => loadHospitals()) - .catch((error) => console.error(error)); - } - - // Close the mode box - document.getElementById('close-hospital-modal').addEventListener('click', () => { - document.getElementById('hospital-modal').style.display = 'none'; - }); - - // Initializes sidebar navigation events + // Initializes the loading of the hospital management interface document.querySelectorAll(".sidebar a").forEach(link => { link.addEventListener("click", function (event) { event.preventDefault();