/* ── HiTech Technologies — admin.js ─────────────── */ // ── Theme ───────────────────────────────────────── const savedTheme = localStorage.getItem('ht-admin-theme') || 'light'; document.documentElement.setAttribute('data-theme', savedTheme); updateThemeBtn(savedTheme); document.getElementById('adminThemeToggle').addEventListener('click', () => { const cur = document.documentElement.getAttribute('data-theme'); const next = cur === 'dark' ? 'light' : 'dark'; document.documentElement.setAttribute('data-theme', next); localStorage.setItem('ht-admin-theme', next); updateThemeBtn(next); }); function updateThemeBtn(t) { const i = document.querySelector('#adminThemeToggle i'); if (i) i.className = t === 'dark' ? 'fa fa-sun' : 'fa fa-moon'; } // ── Auth ────────────────────────────────────────── let token = localStorage.getItem('ht-admin-token'); let currentUsername = localStorage.getItem('ht-admin-user') || 'Admin'; function getHeaders() { return { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` }; } function checkAuth() { if (token) { document.getElementById('login-page').style.display = 'none'; document.getElementById('admin-app').style.display = 'block'; const u = currentUsername; document.getElementById('sidebarName').textContent = u; document.getElementById('sidebarAvatar').textContent = u.charAt(0).toUpperCase(); initAdmin(); } } async function doLogin() { const username = document.getElementById('login-user').value.trim(); const password = document.getElementById('login-pass').value; const btn = document.getElementById('login-btn'); const err = document.getElementById('login-err'); err.style.display = 'none'; if (!username || !password) { showLoginErr('Please enter username and password.'); return; } btn.disabled = true; btn.innerHTML = '
Signing In...'; try { const res = await fetch('/api/auth/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username, password }) }); const data = await res.json(); if (res.ok) { token = data.token; currentUsername = data.username; localStorage.setItem('ht-admin-token', token); localStorage.setItem('ht-admin-user', currentUsername); checkAuth(); } else { showLoginErr(data.error || 'Invalid credentials'); } } catch { showLoginErr('Server error. Please try again.'); } finally { btn.disabled = false; btn.innerHTML = ' Sign In'; } } function showLoginErr(msg) { const err = document.getElementById('login-err'); document.getElementById('login-err-msg').textContent = msg; err.style.display = 'block'; } function doLogout() { token = null; localStorage.removeItem('ht-admin-token'); localStorage.removeItem('ht-admin-user'); document.getElementById('admin-app').style.display = 'none'; document.getElementById('login-page').style.display = 'flex'; document.getElementById('login-user').value = ''; document.getElementById('login-pass').value = ''; } // ── Panel switching ─────────────────────────────── const panelTitles = { dashboard:'Dashboard', services:'Manage Services', products:'Manage Products', posts:'Blog Posts', messages:'Contact Messages', settings:'Settings' }; function showPanel(name) { document.querySelectorAll('.panel').forEach(p => p.classList.remove('active')); document.querySelectorAll('.sidebar-link').forEach(l => l.classList.remove('active')); document.getElementById(`panel-${name}`).classList.add('active'); document.querySelector(`[data-panel="${name}"]`).classList.add('active'); document.getElementById('topbarTitle').textContent = panelTitles[name] || name; if (name === 'dashboard') loadDashboard(); if (name === 'services') loadServices(); if (name === 'products') loadProducts(); if (name === 'posts') loadPosts(); if (name === 'messages') loadMessages(); if (name === 'settings') loadSettings(); } // ── API helpers ─────────────────────────────────── async function api(method, path, body) { const opts = { method, headers: getHeaders() }; if (body && !(body instanceof FormData)) { opts.body = JSON.stringify(body); } else if (body instanceof FormData) { opts.headers = { 'Authorization': `Bearer ${token}` }; opts.body = body; } const res = await fetch(path, opts); const data = await res.json().catch(() => ({})); if (res.status === 401) { doLogout(); return; } if (!res.ok) throw new Error(data.error || 'Request failed'); return data; } // ── Dashboard ───────────────────────────────────── async function loadDashboard() { try { const stats = await api('GET', '/api/admin/stats'); document.getElementById('stat-services').textContent = stats.services; document.getElementById('stat-products').textContent = stats.products; document.getElementById('stat-posts').textContent = stats.posts; document.getElementById('stat-contacts').textContent = stats.contacts; document.getElementById('stat-unread').textContent = stats.unread; if (stats.unread > 0) { document.getElementById('unreadBadge').textContent = stats.unread; document.getElementById('unreadBadge').style.display = 'inline'; } else document.getElementById('unreadBadge').style.display = 'none'; loadDashMsgs(); } catch (e) { showToast('Failed to load stats', 'error'); } } async function loadDashMsgs() { try { const msgs = await api('GET', '/api/contacts'); const el = document.getElementById('dashMsgs'); if (!msgs.length) { el.innerHTML = 'No messages yet.
Failed to load.
No services yet. Add your first service.
Failed to load.
No products yet.
Failed to load.
No posts yet.
Failed to load messages.
No messages found.