/** * Dashboard View * Shows stats overview, distribution charts, and recent tickets. */ const DashboardView = { async render() { const container = document.getElementById('view-container'); container.innerHTML = '

Caricamento dashboard...

'; try { const stats = await App.api('/api/dashboard/stats'); const maxByState = Math.max(...(stats.by_state || []).map(s => parseInt(s.count)), 1); const maxByPriority = Math.max(...(stats.by_priority || []).map(s => parseInt(s.count)), 1); const maxByQueue = Math.max(...(stats.by_queue || []).map(s => parseInt(s.count)), 1); container.innerHTML = `
Ticket Aperti
${stats.total_open}
Creati Oggi
${stats.created_today}
Creati Settimana
${stats.created_this_week}
Escalated
${stats.escalated}
Per Stato
${(stats.by_state || []).map(s => `
${s.state} ${s.count}
`).join('')} ${(stats.by_state || []).length === 0 ? '

Nessun dato

' : ''}
Per Priorità
${(stats.by_priority || []).map((p, idx) => `
${p.priority} ${p.count}
`).join('')} ${(stats.by_priority || []).length === 0 ? '

Nessun dato

' : ''}
Per Coda (Top 10)
${(stats.by_queue || []).map(q => `
${q.queue} ${q.count}
`).join('')} ${(stats.by_queue || []).length === 0 ? '

Nessun dato

' : ''}
Ticket Recenti
${(stats.recent_tickets || []).length > 0 ? ` ${stats.recent_tickets.map(t => ` `).join('')}
Numero Titolo Stato Priorità Coda Data
${t.tn} ${App.escapeHtml(t.title || '')} ${t.state_name} ${t.priority_name} ${t.queue_name} ${App.formatDate(t.create_time)}
` : `
📭
Nessun ticket recente
`}
`; // Animate bars after render requestAnimationFrame(() => { document.querySelectorAll('.dist-bar-fill').forEach(bar => { const w = bar.style.width; bar.style.width = '0%'; requestAnimationFrame(() => { bar.style.width = w; }); }); }); // Update open ticket count in sidebar badge const badge = document.getElementById('open-ticket-count'); if (badge && stats.total_open > 0) { badge.textContent = stats.total_open; } } catch (err) { container.innerHTML = `
⚠️
Errore caricamento dashboard
${App.escapeHtml(err.message)}
`; } }, };