/**
* Dashboard View
* Shows stats overview, distribution charts, and recent tickets.
*/
const DashboardView = {
async render() {
const container = document.getElementById('view-container');
container.innerHTML = '
';
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 => `
`).join('')}
${(stats.by_state || []).length === 0 ? '
Nessun dato
' : ''}
Per Priorità
${(stats.by_priority || []).map((p, idx) => `
`).join('')}
${(stats.by_priority || []).length === 0 ? '
Nessun dato
' : ''}
Per Coda (Top 10)
${(stats.by_queue || []).map(q => `
`).join('')}
${(stats.by_queue || []).length === 0 ? '
Nessun dato
' : ''}
Ticket Recenti
Mostra:
${(stats.recent_tickets || []).length > 0 ? `
| Numero |
Titolo |
Stato |
Priorità |
Coda |
Data |
${stats.recent_tickets.map(t => `
| ${t.tn} |
${App.escapeHtml(t.title || '')} |
${t.state_name} |
${t.priority_name} |
${t.queue_name} |
${App.formatDate(t.create_time)} |
`).join('')}
` : `
`}
`;
// 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; });
});
});
// Bind preview limit change event
const limitSelect = document.getElementById('dashboard-preview-limit');
if (limitSelect) {
limitSelect.addEventListener('change', async () => {
const newLimit = parseInt(limitSelect.value, 10);
try {
await App.api('/api/dashboard/settings', {
method: 'POST',
body: JSON.stringify({ preview_limit: newLimit }),
});
Toast.success(`Limite anteprima aggiornato a ${newLimit} ticket!`);
this.render();
} catch (err) {
Toast.error('Errore durante il salvataggio dell\'impostazione: ' + err.message);
}
});
}
// Update open ticket count in sidebar badge
const badge = document.getElementById('open-ticket-count');
if (badge) {
badge.textContent = stats.total_open > 0 ? stats.total_open : '';
}
// Update my ticket count in sidebar badge
const myBadge = document.getElementById('my-ticket-count');
if (myBadge) {
myBadge.textContent = stats.total_my_open > 0 ? stats.total_my_open : '';
}
} catch (err) {
container.innerHTML = `
⚠️
Errore caricamento dashboard
${App.escapeHtml(err.message)}
`;
}
},
};