import { api } from '../api';
import { renderNavbar, toast, formatBRL } from '../utils';
export async function renderDashboard(container: HTMLElement) {
container.innerHTML = `
${renderNavbar('/dashboard')}
Vendas Hoje
R$ 0,00
0 comandas
Vendas Semana
R$ 0,00
0 comandas
Estoque Baixo
0
0 produtos ativos
`;
// Set default dates
const hoje = new Date().toISOString().split('T')[0];
const inicioMes = new Date(new Date().getFullYear(), new Date().getMonth(), 1).toISOString().split('T')[0];
(document.getElementById('rel-inicio') as HTMLInputElement).value = inicioMes;
(document.getElementById('rel-fim') as HTMLInputElement).value = hoje;
const loadDashboard = async () => {
try {
const data = await api.get('/relatorios/dashboard');
document.getElementById('kpi-vendas-hoje')!.textContent = formatBRL(data.vendasHoje.total);
document.getElementById('kpi-comandas-hoje')!.textContent = `${data.vendasHoje.comandas} comandas`;
document.getElementById('kpi-vendas-semana')!.textContent = formatBRL(data.vendasSemana.total);
document.getElementById('kpi-comandas-semana')!.textContent = `${data.vendasSemana.comandas} comandas`;
document.getElementById('kpi-comandas-abertas')!.textContent = `${data.comandasAbertas}`;
document.getElementById('kpi-estoque-baixo')!.textContent = `${data.produtosEstoqueBaixo}`;
document.getElementById('kpi-total-produtos')!.textContent = `${data.totalProdutos} produtos ativos`;
} catch (err: any) {
toast(err.message, 'error');
}
};
const loadRelatorio = async () => {
const inicio = (document.getElementById('rel-inicio') as HTMLInputElement).value;
const fim = (document.getElementById('rel-fim') as HTMLInputElement).value;
try {
const params = new URLSearchParams();
if (inicio) params.set('inicio', inicio);
if (fim) params.set('fim', fim);
const data = await api.get(`/relatorios/vendas?${params.toString()}`);
// Resumo
document.getElementById('relatorio-resumo')!.innerHTML = `
Total Vendido
${formatBRL(data.resumo.totalVendas)}
Comandas
${data.resumo.totalComandas}
Ticket Médio
${formatBRL(data.resumo.ticketMedio)}
Descontos
${formatBRL(data.resumo.totalDescontos)}
Acréscimos
${formatBRL(data.resumo.totalAcrescimos)}
`;
// Top produtos
if (data.topProdutos.length > 0) {
document.getElementById('relatorio-top-produtos')!.innerHTML = `
🏆 Mais Vendidos
| # | Produto | Qtd | Total |
${data.topProdutos.map((p: any, i: number) => `
| ${i + 1}º |
${p.nome} |
${p.quantidade} |
${formatBRL(p.total)} |
`).join('')}
`;
} else {
document.getElementById('relatorio-top-produtos')!.innerHTML = '';
}
// Vendas por dia
if (data.vendasPorDia.length > 0) {
document.getElementById('relatorio-vendas-dia')!.innerHTML = `
📅 Vendas por Dia
| Data | Comandas | Total |
${data.vendasPorDia.map((d: any) => `
| ${new Date(d.data + 'T12:00:00').toLocaleDateString('pt-BR')} |
${d.comandas} |
${formatBRL(d.vendas)} |
`).join('')}
`;
} else {
document.getElementById('relatorio-vendas-dia')!.innerHTML = '';
}
// Vendas por pagamento
if (data.vendasPorPagamento.length > 0) {
const pagLabels: Record = {
DINHEIRO: '💵 Dinheiro', CARTAO_CREDITO: '💳 Cartão Crédito',
CARTAO_DEBITO: '💳 Cartão Débito', PIX: '📱 PIX', NAO_INFORMADO: '❓ Não informado',
};
document.getElementById('relatorio-pagamento')!.innerHTML = `
💰 Por Forma de Pagamento
| Forma | Total |
${data.vendasPorPagamento.map((p: any) => `
| ${pagLabels[p.forma] ?? p.forma} |
${formatBRL(p.total)} |
`).join('')}
`;
} else {
document.getElementById('relatorio-pagamento')!.innerHTML = '';
}
} catch (err: any) {
toast(err.message, 'error');
}
};
document.getElementById('btn-refresh-dashboard')?.addEventListener('click', () => {
loadDashboard();
loadRelatorio();
});
document.getElementById('btn-gerar-relatorio')?.addEventListener('click', loadRelatorio);
await Promise.all([loadDashboard(), loadRelatorio()]);
}