<?php include APP_PATH.'/views/partials/report_header.php'; ?>
<?php
$totalOutstanding = array_sum(array_column($customers,'outstanding_balance'));
$overdueCount     = count(array_filter($customers, fn($c) => $c['is_overdue'] ?? false));
$totalCustomers   = count($customers);
?>
<div class="rpt-btn-bar no-print">
  <div class="d-flex gap-2 flex-wrap">
    <?php foreach([['This Month',date('Y-m-01'),date('Y-m-d')],['Last Month',date('Y-m-01',strtotime('first day of last month')),date('Y-m-t',strtotime('last month'))],['All Time','2000-01-01',date('Y-m-d')]] as[$l,$f,$t]):$a=$from===$f&&$to===$t;?>
    <a href="?from=<?=$f?>&to=<?=$t?>" class="btn btn-sm <?=$a?'btn-primary':'btn-outline-secondary'?> py-0 px-2"><?=$l?></a>
    <?php endforeach;?>
  </div>
</div>

<div class="row g-2 mb-3 no-break">
  <div class="col-6 col-md-3"><div class="card text-center py-3 border-primary"><div class="fw-bold fs-4 text-primary"><?=$totalCustomers?></div><small class="text-muted">Credit Customers</small></div></div>
  <div class="col-6 col-md-3"><div class="card text-center py-3 border-danger"><div class="fw-bold text-danger"><?=currency($totalOutstanding)?></div><small class="text-muted">Total Outstanding</small></div></div>
  <div class="col-6 col-md-3"><div class="card text-center py-3 <?=$overdueCount>0?'border-danger':''?>"><div class="fw-bold <?=$overdueCount>0?'text-danger':''?>"><?=$overdueCount?></div><small class="text-muted">Overdue</small></div></div>
  <div class="col-6 col-md-3"><div class="card text-center py-3"><div class="fw-bold"><?=$totalInvoices??0?></div><small class="text-muted">Open Invoices</small></div></div>
</div>

<div class="card no-break">
  <div class="card-header fw-semibold small d-flex justify-content-between"><span>Credit Outstanding — Customer Summary</span><small class="text-muted fw-normal"><?=$totalCustomers?> customers</small></div>
  <div class="table-responsive"><table class="table table-sm table-hover mb-0">
    <thead class="table-dark"><tr><th>#</th><th>Customer Name</th><th>Phone</th><th class="text-end">Open Invoices</th><th class="text-end">Total Credit Given</th><th class="text-end">Outstanding</th><th>Status</th></tr></thead>
    <tbody>
      <?php foreach($customers as$i=>$c):
        $isPaid=(float)($c['outstanding_balance']??0)<=0.01;
        $isOverdue=!$isPaid&&($c['is_overdue']??false);
        $rc=$isOverdue?'table-danger bg-opacity-5':($isPaid?'table-success bg-opacity-5':'table-warning bg-opacity-5');
      ?>
      <tr class="<?=$rc?>">
        <td class="text-muted"><?=$i+1?></td>
        <td class="fw-semibold"><?=e($c['customer_name'])?></td>
        <td class="small text-muted"><?=e($c['phone']??'—')?></td>
        <td class="text-end"><?=number_format($c['open_invoices']??0)?></td>
        <td class="text-end"><?=currency($c['total_credit_given']??0)?></td>
        <td class="text-end fw-bold <?=$isPaid?'text-success':($isOverdue?'text-danger':'text-warning')?>"><?=$isPaid?'✓ Cleared':currency($c['outstanding_balance'])?></td>
        <td><?php if($isPaid):?><span class="badge bg-success">✓ Cleared</span><?php elseif($isOverdue):?><span class="badge bg-danger">⚠ Overdue</span><?php else:?><span class="badge bg-warning text-dark">Due</span><?php endif;?></td>
      </tr>
      <?php endforeach; if(empty($customers)):?><tr><td colspan="7" class="text-center text-muted py-4">No outstanding credit customers.</td></tr><?php endif;?>
    </tbody>
    <tfoot class="table-secondary fw-bold"><tr><td colspan="5">TOTAL (<?=$totalCustomers?> customers)</td><td class="text-end text-danger"><?=currency($totalOutstanding)?></td><td></td></tr></tfoot>
  </table></div>
</div>