// js/app.js - SPA loader + theme toggle + history + admin lock document.addEventListener('DOMContentLoaded', () => { const content = document.getElementById('content'); const navBtns = document.querySelectorAll('.nav-btn'); const themeBtn = document.getElementById('themeToggle'); const searchInput = document.getElementById('searchInput'); const defaultPage = content.dataset.defaultPage || 'gallery'; // --- Theme init --- const savedTheme = localStorage.getItem('theme'); if (savedTheme === 'light') document.body.classList.add('light-mode'); function updateThemeLabel() { themeBtn.textContent = document.body.classList.contains('light-mode') ? 'β˜€' : 'πŸŒ™'; } updateThemeLabel(); themeBtn.addEventListener('click', () => { const isLight = document.body.classList.toggle('light-mode'); localStorage.setItem('theme', isLight ? 'light' : 'dark'); updateThemeLabel(); }); // --- Admin Lock Button --- (async function adminLockInit() { const lockBtn = document.getElementById('adminLock'); if (!lockBtn) return; async function checkAdmin() { try { const res = await fetch('includes/admin_auth.php?action=check'); const json = await res.json(); if (json.logged_in) { lockBtn.textContent = 'πŸ”“'; lockBtn.classList.add('unlocked'); lockBtn.classList.remove('locked'); lockBtn.title = 'Click to log out'; } else { lockBtn.textContent = 'πŸ”’'; lockBtn.classList.add('locked'); lockBtn.classList.remove('unlocked'); lockBtn.title = 'Admin Login'; } } catch (err) { console.warn('Admin check failed', err); } } // On click β€” log out if logged in, or navigate to admin page if not lockBtn.addEventListener('click', async () => { if (lockBtn.classList.contains('unlocked')) { const ok = confirm('Log out of admin mode?'); if (!ok) return; await fetch('includes/admin_auth.php', { method: 'POST', body: new URLSearchParams({ action: 'logout' }) }); await checkAdmin(); alert('You have been logged out.'); loadPage(defaultPage); } else { const event = new CustomEvent('navigate', { detail: { page: 'admin' } }); window.dispatchEvent(event); } }); await checkAdmin(); setInterval(checkAdmin, 30000); })(); // --- Page loader --- async function loadPage(page, push = true) { content.innerHTML = '

Loading…

'; try { const res = await fetch(`pages/${page}.php`, { cache: 'no-store' }); if (!res.ok) throw new Error(`${res.status} ${res.statusText}`); const html = await res.text(); // Insert the page HTML content.innerHTML = html; // πŸ”Ή Execute any inline