// Global flags let otpSent = false; let isAdmin = false; async function loadRole() { try { const res = await fetch(`${contextPath}api/get-role`, { method: "POST" }); const data = await res.json(); if (!data.authenticated) { isAdmin = false; return; } isAdmin = data.isAdmin; } catch (e) { // swallow network errors console.error("Error: ", error); } } // Open the login modal function openLoginModal() { if (isAdmin) { // For admin → directly go to the post-verification page window.open(`${contextPath}view-login-det`, '_blank'); return; } const modal = document.getElementById("login-modal"); modal.classList.remove("hide"); modal.classList.add("show"); modal.style.display = "flex"; // make it visible immediately before animation } // Load role when page loads document.addEventListener("DOMContentLoaded", () => { loadRole(); }); document.addEventListener("DOMContentLoaded", function() { const dropdown = document.getElementById("urlDropdown"); const urlStatDropdown = document.getElementById("urlStatDropdown"); const urlLinkInput = document.getElementById("urlDataLink"); const urlNameInput = document.getElementById("urlDataName"); const urlUsrNameInput = document.getElementById("urlDataUsrName"); const urlUsrPassInput = document.getElementById("urlDataUsrPass"); const urlCatNameInput = document.getElementById("urlCatName"); const togglePass = document.getElementById("togglePass"); // safety check if (!dropdown || !togglePass) { return; } // if (!dropdown) return; // --- Dropdown change handler --- dropdown.addEventListener("change", function() { const selected = dropdown.options[dropdown.selectedIndex]; if (!selected.value) { urlLinkInput.value = ""; urlNameInput.value = ""; urlUsrNameInput.value = ""; urlUsrPassInput.value = ""; urlCatNameInput.value = ""; return; } const statId = selected.getAttribute("data-statId"); if (statId) { urlStatDropdown.value = statId; } const link = selected.getAttribute("data-link"); const name = selected.getAttribute("data-name"); const usrname = selected.getAttribute("data-usrname"); const pass = selected.getAttribute("data-pass"); const urlCatName = selected.getAttribute("data-urlCatName"); urlLinkInput.value = link || ""; urlNameInput.value = name || ""; urlUsrNameInput.value = (usrname && usrname !== "-") ? usrname : ""; urlUsrPassInput.value = (pass && pass !== "-") ? pass : ""; urlCatNameInput.value = (urlCatName && urlCatName !== "-") ? urlCatName : ""; // Reset to hidden state urlUsrPassInput.type = "password"; togglePass.classList.add("fa-eye"); togglePass.classList.remove("fa-eye-slash"); }); // --- Toggle Password Visibility --- togglePass.addEventListener("click", function() { const isPassword = urlUsrPassInput.type === "password"; urlUsrPassInput.type = isPassword ? "text" : "password"; togglePass.classList.toggle("fa-eye"); togglePass.classList.toggle("fa-eye-slash"); }); const form = document.getElementById("frm-url-det"); // Existing form submission logic... form.addEventListener("submit", function(e) { e.preventDefault(); // Prevent default form submission const formData = new FormData(form); fetch(form.getAttribute("action"), { method: "POST", body: formData }) .then(response => { if (!response.ok) { throw new Error("Network response was not ok: " + response.statusText); } return response.json(); }) .then(data => { if (data.resStat) { alert("Success: " + data.res); form.reset(); } else { alert("Failed: " + data.res); } }) .catch(error => { console.error("Error updating url:", error); alert("Error updating url. Please try again."); }); }); });