/* ========================================================================== Siro Admin Web Portal - Interactive Application Engine ========================================================================== */ document.addEventListener('DOMContentLoaded', () => { // DOM Elements const authWrapper = document.getElementById('authWrapper'); const loginForm = document.getElementById('loginForm'); const navItems = document.querySelectorAll('.nav-item[data-view]'); const pageViews = document.querySelectorAll('.page-view'); const logoutBtn = document.getElementById('logoutBtn'); const sidebar = document.getElementById('sidebar'); const toggleSidebarBtn = document.getElementById('toggleSidebar'); // Sample State & Datasets let currentUser = JSON.parse(localStorage.getItem('siro_admin_user')) || null; const mockRides = [ { id: 'SR-9842', rider: 'Ahmad Al-Mansoor', driver: 'Khalid Hassan', pickup: 'Abdoun, Amman', dropoff: '7th Circle', fare: '$14.50', status: 'Completed', time: '10 mins ago' }, { id: 'SR-9843', rider: 'Sarah Mahmoud', driver: 'Omar Farooq', pickup: 'Jabal Amman', dropoff: 'Queen Alia Airport', fare: '$28.00', status: 'In Progress', time: 'Just now' }, { id: 'SR-9844', rider: 'Tariq Ziyad', driver: 'Youssef Ali', pickup: 'Sweifieh Mall', dropoff: 'University of Jordan', fare: '$9.20', status: 'Completed', time: '25 mins ago' }, { id: 'SR-9845', rider: 'Reem Kanaan', driver: 'Hassan Nabil', pickup: 'Mecca Street', dropoff: 'Tabarbour', fare: '$11.80', status: 'Cancelled', time: '40 mins ago' }, { id: 'SR-9846', rider: 'Fadi Jarrah', driver: 'Bilal Mustafa', pickup: 'Khalda', dropoff: 'Shmeisani', fare: '$16.00', status: 'In Progress', time: '5 mins ago' }, ]; const mockDrivers = [ { id: 'DRV-101', name: 'Khalid Hassan', phone: '+962 7 9123 4567', vehicle: 'Toyota Camry 2022', rating: '4.95', trips: 1420, status: 'Online', verified: true }, { id: 'DRV-102', name: 'Omar Farooq', phone: '+962 7 8876 5432', vehicle: 'Hyundai Elantra 2021', rating: '4.88', trips: 980, status: 'In Trip', verified: true }, { id: 'DRV-103', name: 'Youssef Ali', phone: '+962 7 7654 3210', vehicle: 'Kia Optima 2023', rating: '4.90', trips: 1150, status: 'Online', verified: true }, { id: 'DRV-104', name: 'Hassan Nabil', phone: '+962 7 9988 7766', vehicle: 'Nissan Sentra 2020', rating: '4.72', trips: 620, status: 'Offline', verified: false }, ]; const mockPassengers = [ { id: 'PSG-501', name: 'Ahmad Al-Mansoor', email: 'ahmad@example.com', trips: 45, rating: '4.9', joined: 'Jan 2025', status: 'Active' }, { id: 'PSG-502', name: 'Sarah Mahmoud', email: 'sarah.m@example.com', trips: 89, rating: '5.0', joined: 'Nov 2024', status: 'Active' }, { id: 'PSG-503', name: 'Reem Kanaan', email: 'reem.k@example.com', trips: 12, rating: '4.6', joined: 'Mar 2025', status: 'Suspended' }, ]; // Initialize App function initApp() { checkAuth(); setupNavigation(); renderRidesTable(); renderDriversTable(); renderPassengersTable(); initCharts(); setupEvents(); } // Web Device Fingerprint Generator function getWebFingerprint() { let fp = localStorage.getItem('siro_web_fp'); if (!fp) { fp = 'web_' + Math.random().toString(36).substring(2) + Date.now().toString(36); localStorage.setItem('siro_web_fp', fp); } return fp; } const deviceFingerprint = getWebFingerprint(); // Mode State (Live vs Demo) let isLiveMode = true; const modeLiveBtn = document.getElementById('modeLiveBtn'); const modeDemoBtn = document.getElementById('modeDemoBtn'); modeLiveBtn?.addEventListener('click', () => { isLiveMode = true; modeLiveBtn.style.background = 'var(--primary)'; modeLiveBtn.style.color = '#fff'; modeDemoBtn.style.background = 'transparent'; modeDemoBtn.style.color = 'var(--text-muted)'; }); modeDemoBtn?.addEventListener('click', () => { isLiveMode = false; modeDemoBtn.style.background = 'var(--primary)'; modeDemoBtn.style.color = '#fff'; modeLiveBtn.style.background = 'transparent'; modeLiveBtn.style.color = 'var(--text-muted)'; }); // Auth Functions function checkAuth() { if (currentUser && currentUser.isLive && currentUser.jwt) { authWrapper.classList.add('hidden'); fetchLiveDashboardData(); } else { // Clear stale/demo session to force fresh Live Backend Login localStorage.removeItem('siro_admin_user'); currentUser = null; authWrapper.classList.remove('hidden'); } } let pendingOtpPhone = ''; let pendingOtpPassword = ''; loginForm?.addEventListener('submit', async (e) => { e.preventDefault(); const phone = document.getElementById('loginEmail').value.trim(); const password = document.getElementById('loginPass').value.trim(); if (isLiveMode) { showNotification('Authenticating credentials with server...', 'info'); try { const formData = new FormData(); formData.append('phone', phone); formData.append('id', phone); // send both phone and id for maximum compatibility formData.append('password', password); formData.append('fingerprint', deviceFingerprint); formData.append('aud', 'admin'); // Try Admin auth login endpoint let response = await fetch('/backend/Admin/auth/login.php', { method: 'POST', body: formData }); // Fallback to loginAdmin.php if 404 if (response.status === 404) { response = await fetch('/backend/loginAdmin.php', { method: 'POST', body: formData }); } const resText = await response.text(); console.log('Server Raw Response:', resText); let res; try { res = JSON.parse(resText); } catch (jsonErr) { showNotification(`Server Error (${response.status}): ${resText.substring(0, 100)}`, 'danger'); return; } console.log('Parsed API Response:', res); // Check if login succeeded and JWT was returned const jwtToken = res.jwt || res.data?.jwt || (typeof res.message === 'object' ? res.message?.jwt : null); const adminInfo = res.admin || res.data?.admin || (typeof res.message === 'object' ? res.message?.admin : {}) || {}; if (response.ok && (res.status === 'success' || jwtToken)) { if (jwtToken) { currentUser = { name: adminInfo.name || 'Admin', email: adminInfo.email || phone, role: adminInfo.role || 'Administrator', jwt: jwtToken, isLive: true }; localStorage.setItem('siro_admin_user', JSON.stringify(currentUser)); authWrapper.classList.add('hidden'); showNotification('Access Granted! Welcome to Admin Portal.', 'success'); fetchLiveDashboardData(); } else if (res.status === 'otp_required' || (res.message && (res.message.status === 'otp_required' || res.message === 'otp_required'))) { pendingOtpPhone = phone; pendingOtpPassword = password; const masked = res.phone || (typeof res.message === 'object' ? res.message.phone : null) || phone; document.getElementById('otpPhoneText').textContent = `Verification code sent to WhatsApp (${masked})`; document.getElementById('otpModal').classList.add('active'); } else { showNotification('Login successful, loading dashboard...', 'success'); currentUser = { name: adminInfo.name || 'Admin', email: phone, role: 'Administrator', isLive: true }; localStorage.setItem('siro_admin_user', JSON.stringify(currentUser)); authWrapper.classList.add('hidden'); fetchLiveDashboardData(); } } else if (res.status === 'otp_required' || (res.message && (res.message.status === 'otp_required' || res.message === 'otp_required'))) { pendingOtpPhone = phone; pendingOtpPassword = password; const masked = res.phone || (typeof res.message === 'object' ? res.message.phone : null) || phone; document.getElementById('otpPhoneText').textContent = `Verification code sent to WhatsApp (${masked})`; document.getElementById('otpModal').classList.add('active'); } else { // Show REAL backend API error message const errorMsg = (typeof res.message === 'string' ? res.message : null) || res.error || 'Invalid credentials or user not found.'; showNotification(`Login Error: ${errorMsg}`, 'danger'); } } catch (err) { console.error('Login Exception:', err); showNotification(`Network Error: ${err.message || 'Could not connect to backend'}`, 'danger'); } } else { currentUser = { name: 'Super Admin (Demo)', email: phone, role: 'Administrator', isLive: false }; localStorage.setItem('siro_admin_user', JSON.stringify(currentUser)); authWrapper.classList.add('hidden'); showNotification('Welcome to Interactive Demo Mode!', 'success'); } }); // Submit OTP Code document.getElementById('submitOtpBtn')?.addEventListener('click', async () => { const otp = document.getElementById('otpInput').value.trim(); if (!otp || otp.length < 3) { showNotification('Please enter the 3-digit OTP code', 'warning'); return; } try { const formData = new FormData(); formData.append('otp', otp); formData.append('fingerprint', deviceFingerprint); formData.append('aud', 'admin'); const response = await fetch('/backend/Admin/auth/verify_login.php', { method: 'POST', body: formData }); const res = await response.json(); if (response.ok && res.status === 'success') { currentUser = { name: res.admin?.name || 'Admin', email: res.admin?.email || pendingOtpPhone, role: res.admin?.role || 'Administrator', jwt: res.jwt, isLive: true }; localStorage.setItem('siro_admin_user', JSON.stringify(currentUser)); closeOtpModal(); authWrapper.classList.add('hidden'); showNotification('OTP Verified! Welcome back, Admin.', 'success'); fetchLiveDashboardData(); } else { showNotification(res.message || 'Invalid OTP code', 'danger'); } } catch (err) { showNotification('OTP verification error', 'danger'); } }); window.closeOtpModal = function() { document.getElementById('otpModal')?.classList.remove('active'); }; async function fetchLiveDashboardData() { try { showNotification('Fetching live fleet metrics from database...', 'info'); const response = await fetch('/backend/Admin/dashbord.php', { headers: { 'Authorization': `Bearer ${currentUser?.jwt || ''}` } }); const data = await response.json(); let stats = null; if (data && data.status === 'success') { stats = Array.isArray(data.data) ? data.data[0] : (data.data?.data ? data.data.data[0] : data.data); } else if (data && data[0]) { stats = data[0]; } if (stats) { // Update Revenue & Stats Cards const statCards = document.querySelectorAll('.stat-card'); statCards.forEach(card => { const title = card.querySelector('.stat-title')?.textContent.trim(); const valEl = card.querySelector('.stat-value'); if (!valEl) return; if (title?.includes('Revenue')) { const rev = stats.total_driver_earnings || stats.total_revenue || '42,850'; valEl.textContent = typeof rev === 'number' || !isNaN(rev) ? `$${parseFloat(rev).toLocaleString()}` : rev; } else if (title?.includes('Rides') || title?.includes('Trips')) { valEl.textContent = (stats.countRide || stats.total_rides || '1,482').toLocaleString(); } else if (title?.includes('Captains') || title?.includes('Drivers')) { valEl.textContent = (stats.countDriver || stats.num_Driver || '324').toLocaleString(); } else if (title?.includes('Passengers') || title?.includes('Satisfaction')) { valEl.textContent = (stats.countPassengers || '8,920').toLocaleString(); } }); // Update system health metrics if present const totalDistanceEl = document.getElementById('totalDistanceMetric'); if (totalDistanceEl && stats.total_distance) { totalDistanceEl.textContent = `${stats.total_distance} km`; } showNotification('Live Database Stats Synchronized!', 'success'); // Fetch Live Detailed Tables fetchLiveRides(); fetchLivePassengers(); fetchLivePendingStaff(); } } catch (err) { console.log('Using local fallback metrics for display:', err); } } async function fetchLiveRides() { try { const response = await fetch('/backend/Admin/rides/get_rides_by_status.php?status=All', { headers: { 'Authorization': `Bearer ${currentUser?.jwt || ''}` } }); const data = await response.json(); const rides = Array.isArray(data) ? data : (data.data || []); if (rides.length > 0) { const tbody = document.getElementById('ridesTableBody'); if (tbody) { tbody.innerHTML = rides.slice(0, 15).map(r => `
Pickup: Abdoun Circle, Amman
Dropoff: Queen Alia International Airport
Fare Breakdown: Base Fare ($4.00) + Distance ($20.00) + Peak Surge ($4.00)
Payment Method: Credit Card (Visa **** 4921)