import { apiCall } from './auth.js'; import { updateDarkModeGradients } from './darkmode.js'; import { getCurrentLanguage } from './i18n.js'; async function loadDashboard() { const authResult = await apiCall('/api/check-auth'); if (!authResult.success || !authResult.data.authenticated) { alert(getCurrentLanguage() === 'az' ? 'Zəhmət olmasa əvvəlcə daxil olun' : 'Please sign in first to access the dashboard'); // Use global navigateTo function (defined in main.js) if (typeof window.navigateTo === 'function') { window.navigateTo('login', { preventDefault: () => {} }); } return; } const imagesResult = await apiCall('/api/my-images'); if (imagesResult.success && imagesResult.data.images) { updateDashboardWithImages(imagesResult.data.images); } } function updateDashboardWithImages(images) { const dashboardContainer = document.querySelector('#dashboard-page .space-y-6'); if (!dashboardContainer) return; const existingItems = dashboardContainer.querySelectorAll('.card-hover'); existingItems.forEach(item => item.remove()); images.forEach((img, index) => { if (index >= 3) return; const isHealthy = img.prediction && img.prediction.toLowerCase().includes('healthy'); const bgColor = isHealthy ? 'from-green-50 to-green-100' : 'from-red-50 to-orange-50'; const borderColor = isHealthy ? 'border-green-200' : 'border-red-200'; const statusBadge = isHealthy ? '✅ Healthy' : '⚠️ Disease Detected'; const itemHTML = `
Uploaded image

Image Analysis

Scanned on ${new Date(img.created_at || Date.now()).toLocaleString()}

${statusBadge}
Prediction
${img.prediction || 'Unknown'}
`; dashboardContainer.insertAdjacentHTML('beforeend', itemHTML); }); const totalScans = images.length; const healthyCount = images.filter(img => img.prediction && img.prediction.toLowerCase().includes('healthy')).length; const issuesCount = totalScans - healthyCount; const avgHealth = totalScans > 0 ? Math.round((healthyCount / totalScans) * 100) : 0; const statsElements = document.querySelectorAll('#dashboard-page .text-4xl.font-bold.gradient-text, #dashboard-page .text-4xl.font-bold.text-green-600, #dashboard-page .text-4xl.font-bold.text-orange-600'); if (statsElements.length >= 4) { statsElements[0].textContent = totalScans; statsElements[1].textContent = healthyCount; statsElements[2].textContent = issuesCount; statsElements[3].textContent = avgHealth + '%'; } const isDark = document.body.classList.contains('dark-mode'); if (isDark) { updateDarkModeGradients(true); } } export { loadDashboard, updateDashboardWithImages };