<?php $this->extend("layouts/main"); $this->section("content"); $title = "Reports";
$sym = ["GBP"=>"£","EUR"=>"€","USD"=>"$"][$currency] ?? "£";
?>
<!-- Summary KPIs -->
<div class="row g-3 mb-4">
    <div class="col-md-3"><div class="kpi-card kpi-blue"><div class="kpi-value"><?php echo $sym.number_format($summary["total_invoiced"],2) ?></div><div class="kpi-label">Total Invoiced</div></div></div>
    <div class="col-md-3"><div class="kpi-card kpi-green"><div class="kpi-value"><?php echo $sym.number_format($summary["total_collected"],2) ?></div><div class="kpi-label">Total Collected</div></div></div>
    <div class="col-md-3"><div class="kpi-card kpi-red"><div class="kpi-value"><?php echo $sym.number_format($summary["total_outstanding"],2) ?></div><div class="kpi-label">Outstanding</div></div></div>
    <div class="col-md-3"><div class="kpi-card kpi-purple"><div class="kpi-value"><?php echo $summary["jobs_completed"] ?></div><div class="kpi-label">Jobs Completed</div></div></div>
</div>
<!-- Export Buttons -->
<div class="d-flex gap-2 mb-4">
    <a href="/fieldly/public/index.php/reports/export/invoices" class="btn btn-outline-secondary btn-sm"><i class="bi bi-download me-1"></i> Export Invoices CSV</a>
    <a href="/fieldly/public/index.php/reports/export/time" class="btn btn-outline-secondary btn-sm"><i class="bi bi-download me-1"></i> Export Time CSV</a>
</div>
<div class="row g-4">
    <!-- Revenue Chart -->
    <div class="col-12"><div class="card"><div class="card-header"><h6 class="mb-0">Monthly Revenue (Last 12 Months)</h6></div><div class="card-body"><canvas id="revenueChart" height="80"></canvas></div></div></div>
    <!-- Utilisation -->
    <div class="col-lg-6"><div class="card"><div class="card-header"><h6 class="mb-0">Engineer Utilisation (Last 30 Days)</h6></div><div class="card-body p-0">
    <table class="table table-sm mb-0"><thead><tr><th>Engineer</th><th>Total</th><th>Billable</th><th>Utilisation</th></tr></thead><tbody>
    <?php foreach ($utilisation as $u): $pct = $u["total_mins"]>0?round($u["billable_mins"]/$u["total_mins"]*100):0; ?>
    <tr><td><?php echo esc($u["first_name"]." ".$u["last_name"]) ?></td><td style="font-size:13px"><?php echo floor($u["total_mins"]/60) ?>h <?php echo $u["total_mins"]%60 ?>m</td><td style="font-size:13px"><?php echo floor($u["billable_mins"]/60) ?>h <?php echo $u["billable_mins"]%60 ?>m</td><td><div class="progress" style="height:6px;width:80px"><div class="progress-bar bg-success" style="width:<?php echo $pct ?>%"></div></div><small><?php echo $pct ?>%</small></td></tr>
    <?php endforeach; ?>
    <?php if (empty($utilisation)): ?><tr><td colspan="4" class="text-center text-muted py-3">No data</td></tr><?php endif; ?>
    </tbody></table>
    </div></div></div>
    <!-- Aged Debtors -->
    <div class="col-lg-6"><div class="card"><div class="card-header"><h6 class="mb-0">Aged Debtors</h6></div><div class="card-body p-0">
    <table class="table table-sm mb-0"><thead><tr><th>Invoice</th><th>Customer</th><th>Balance</th><th>Days Overdue</th></tr></thead><tbody>
    <?php foreach ($agedDebtors as $d): ?>
    <tr><td><a href="/fieldly/public/index.php/invoices/<?php echo $d["id"] ?>" class="text-decoration-none"><?php echo esc($d["number"]) ?></a></td><td style="font-size:13px"><?php echo esc($d["company_name"]?:$d["first_name"]." ".$d["last_name"]) ?></td><td class="fw-bold text-danger"><?php echo $sym.number_format((float)$d["balance_due"],2) ?></td><td><span class="badge bg-<?php echo $d["days_overdue"]>60?"danger":($d["days_overdue"]>30?"warning text-dark":"secondary") ?>"><?php echo $d["days_overdue"] > 0 ? $d["days_overdue"]." days" : "Due soon" ?></span></td></tr>
    <?php endforeach; ?>
    <?php if (empty($agedDebtors)): ?><tr><td colspan="4" class="text-center text-muted py-3">No outstanding invoices</td></tr><?php endif; ?>
    </tbody></table>
    </div></div></div>
</div>
<?php $this->section("scripts"); ?>
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.0/dist/chart.umd.min.js"></script>
<script>const ctx=document.getElementById("revenueChart");if(ctx){new Chart(ctx,{type:"bar",data:{labels:<?php echo json_encode(array_column($revenue,"month")) ?>,datasets:[{label:"Revenue",data:<?php echo json_encode(array_map(fn($r)=>(float)$r["total"],$revenue)) ?>,backgroundColor:"rgba(79,70,229,.8)",borderRadius:6}]},options:{responsive:true,plugins:{legend:{display:false}},scales:{y:{beginAtZero:true,ticks:{callback:v=>"<?php echo $sym ?>"+v.toLocaleString()}}}}});}</script>
<?php $this->endSection(); ?>
<?php $this->endSection(); ?>