// --- Fitur Autentikasi dan Manajemen User (RBAC) --- (function() { window.currentUser = null; // Elements const authWidget = document.getElementById('authWidget'); const loginModal = document.getElementById('loginModal'); const loginUsernameInput = document.getElementById('loginUsername'); const loginPasswordInput = document.getElementById('loginPassword'); const loginSubmitBtn = document.getElementById('loginSubmitBtn'); const loginErrorMsg = document.getElementById('loginErrorMsg'); const closeLoginModal = document.getElementById('closeLoginModal'); // User Management Modal Elements const userManagementModal = document.getElementById('userManagementModal'); const closeUserManagementModal = document.getElementById('closeUserManagementModal'); const menuUsersBtn = document.getElementById('menuUsers'); const userTableBody = document.getElementById('userTableBody'); const btnSaveUser = document.getElementById('btnSaveUser'); const btnCancelUserEdit = document.getElementById('btnCancelUserEdit'); const userFormTitle = document.getElementById('userFormTitle'); const manageUserId = document.getElementById('manageUserId'); const manageUsername = document.getElementById('manageUsername'); const managePassword = document.getElementById('managePassword'); const manageRole = document.getElementById('manageRole'); const manageIbadahId = document.getElementById('manageIbadahId'); const manageIbadahGroup = document.getElementById('manageIbadahGroup'); // Startup Session Check function checkSession() { fetch('api/check_session.php') .then(res => res.json()) .then(data => { if (data.status === 'success' && data.isLoggedIn) { window.currentUser = data.data; } else { window.currentUser = null; } updateAuthUI(); refreshAllLayers(); }) .catch(err => { console.error("Session check failed:", err); window.currentUser = null; updateAuthUI(); }); } // Update UI based on logged-in state function updateAuthUI() { if (!authWidget) return; const dashboardBtn = document.getElementById('menuDashboard'); if (window.currentUser) { // Logged In state const roleLabel = window.currentUser.role === 'admin' ? 'Admin' : 'Pengelola'; const extraLabel = window.currentUser.nama_ibadah ? ` - ${window.currentUser.nama_ibadah}` : ''; authWidget.innerHTML = `
`; document.getElementById('authLogoutBtn').addEventListener('click', logout); // Show Dashboard button for all logged-in users if (dashboardBtn) dashboardBtn.style.display = 'flex'; // Show user management button in sidebar for Admin const adminDivider = document.getElementById('adminNavLabel'); if (window.currentUser.role === 'admin') { if (menuUsersBtn) menuUsersBtn.style.display = 'flex'; if (adminDivider) adminDivider.style.display = 'block'; } else { if (menuUsersBtn) menuUsersBtn.style.display = 'none'; if (adminDivider) adminDivider.style.display = 'none'; } } else { // Logged Out state authWidget.innerHTML = ` `; document.getElementById('authLoginBtn').addEventListener('click', showLoginModal); if (menuUsersBtn) menuUsersBtn.style.display = 'none'; if (dashboardBtn) dashboardBtn.style.display = 'none'; const adminDivider = document.getElementById('adminNavLabel'); if (adminDivider) adminDivider.style.display = 'none'; } } function refreshAllLayers() { if (typeof loadSpbu === 'function') loadSpbu(); if (typeof loadJalan === 'function') loadJalan(); if (typeof loadParsil === 'function') loadParsil(); if (typeof loadRumahIbadah === 'function') loadRumahIbadah(); if (typeof loadPendudukMiskin === 'function') loadPendudukMiskin(); if (window.refreshActivePanel) window.refreshActivePanel(); } // Login Modals logic function showLoginModal() { loginUsernameInput.value = ''; loginPasswordInput.value = ''; loginErrorMsg.style.display = 'none'; loginModal.classList.add('show'); } function hideLoginModal() { loginModal.classList.remove('show'); } if (closeLoginModal) { closeLoginModal.addEventListener('click', hideLoginModal); } loginSubmitBtn.addEventListener('click', function() { const username = loginUsernameInput.value.trim(); const password = loginPasswordInput.value; if (!username || !password) { loginErrorMsg.textContent = 'Username dan password wajib diisi'; loginErrorMsg.style.display = 'block'; return; } fetch('api/login.php', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username, password }) }) .then(res => res.json()) .then(data => { if (data.status === 'success') { window.currentUser = data.data; hideLoginModal(); updateAuthUI(); refreshAllLayers(); showToast('Login berhasil', 'success'); } else { loginErrorMsg.textContent = data.message || 'Login gagal'; loginErrorMsg.style.display = 'block'; } }) .catch(err => { console.error(err); loginErrorMsg.textContent = 'Koneksi ke server terputus'; loginErrorMsg.style.display = 'block'; }); }); function logout() { fetch('api/logout.php') .then(res => res.json()) .then(data => { window.currentUser = null; updateAuthUI(); refreshAllLayers(); showToast('Logout berhasil', 'success'); }) .catch(err => { console.error(err); showToast('Gagal logout', 'error'); }); } // User Management Modal Logic if (menuUsersBtn) { menuUsersBtn.addEventListener('click', function() { userManagementModal.classList.add('show'); loadUsers(); populateIbadahDropdown(); resetUserForm(); }); } // Dashboard Button const menuDashboardBtn = document.getElementById('menuDashboard'); if (menuDashboardBtn) { menuDashboardBtn.addEventListener('click', function() { if (typeof window.openDashboard === 'function') window.openDashboard(); }); } if (closeUserManagementModal) { closeUserManagementModal.addEventListener('click', function() { userManagementModal.classList.remove('show'); }); } // Show/hide ibadah dropdown based on selected role in form if (manageRole) { manageRole.addEventListener('change', function() { if (this.value === 'admin') { manageIbadahGroup.style.display = 'none'; manageIbadahId.value = ''; } else { manageIbadahGroup.style.display = 'flex'; } }); } function populateIbadahDropdown() { if (!manageIbadahId) return; manageIbadahId.innerHTML = ''; if (typeof ibadahDataList !== 'undefined') { ibadahDataList.forEach(ib => { const opt = document.createElement('option'); opt.value = ib.id; opt.textContent = `${ib.jenis || '🕌'} ${ib.nama}`; manageIbadahId.appendChild(opt); }); } } function loadUsers() { if (!userTableBody) return; userTableBody.innerHTML = '