<?php
$pageTitle = 'Reports';
require_once __DIR__ . '/../../app/includes/header.php'; require_role_access('report:view'); require_admin();
$totals = db()->query("SELECT COUNT(*) total, SUM(status='draft') draft, SUM(status='final') final, SUM(status='accepted') accepted, SUM(status='rejected') rejected, COALESCE(SUM(CASE WHEN status='accepted' THEN grand_total ELSE 0 END),0) sales FROM quotations")->fetch();
$monthly = db()->query("SELECT DATE_FORMAT(created_at, '%Y-%m') ym, COUNT(*) count, COALESCE(SUM(grand_total),0) amount FROM quotations GROUP BY ym ORDER BY ym LIMIT 12")->fetchAll();
?>
<div class="page-head"><div><h1>Reports</h1><p>Quotation status, sales amount, and monthly activity.</p></div></div>
<div class="row g-3 mb-4">
<?php foreach (['total'=>'Total quotations','draft'=>'Draft quotations','final'=>'Final quotations','accepted'=>'Accepted quotations','rejected'=>'Rejected quotations','sales'=>'Total sales amount'] as $k=>$label): ?>
<div class="col-md-2"><div class="metric-card"><span><?= e($label) ?></span><strong><?= $k==='sales' ? money((float)$totals[$k]) : (int)$totals[$k] ?></strong></div></div>
<?php endforeach; ?>
</div>
<div class="card app-card p-4"><canvas id="monthlyChart" height="90" data-labels='<?= e(json_encode(array_column($monthly, 'ym'))) ?>' data-values='<?= e(json_encode(array_map('floatval', array_column($monthly, 'amount')))) ?>'></canvas></div>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<?php require_once __DIR__ . '/../../app/includes/footer.php'; ?>
