<?php include APP_PATH.'/views/partials/report_header.php'; ?>
<div class="rpt-btn-bar no-print">
  <form method="GET" action="<?=APP_URL?>/reports/customers" class="d-flex gap-2 align-items-end filter-form">
    <div class="input-group input-group-sm" style="max-width:280px"><span class="input-group-text"><i class="bi bi-search"></i></span><input type="text" name="search" class="form-control" placeholder="Name or phone…" value="<?=e($search)?>"></div>
    <button class="btn btn-secondary btn-sm">Filter</button>
    <a href="<?=APP_URL?>/reports/customers" class="btn btn-outline-secondary btn-sm">Clear</a>
  </form>
</div>
<div class="row g-2 mb-3 no-break">
  <div class="col-6 col-md-3"><div class="card text-center py-3"><div class="fw-bold fs-5"><?=count($rows)?></div><small class="text-muted">Customers With Due</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($total)?></div><small class="text-muted">Total Outstanding</small></div></div>
  <div class="col-6 col-md-3"><div class="card text-center py-3"><div class="fw-bold"><?=currency($totalCreditLim)?></div><small class="text-muted">Total Credit Limit</small></div></div>
  <div class="col-6 col-md-3"><div class="card text-center py-3 <?=$overLimitCount>0?'border-danger':''?>"><div class="fw-bold <?=$overLimitCount>0?'text-danger':''?>"><?=$overLimitCount?></div><small class="text-muted">Over Credit Limit</small></div></div>
</div>
<div class="card"><div class="card-header fw-semibold small d-flex justify-content-between"><span>Customer Outstanding Balances</span><small class="text-muted fw-normal"><?=count($rows)?> 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">Credit Limit</th><th class="text-end">Outstanding</th><th class="text-end">Open Invoices</th><th>Last Sale</th><th>Status</th></tr></thead>
  <tbody>
    <?php foreach($rows as$i=>$r):$over=$r['credit_limit']>0&&(float)$r['current_balance']>=(float)$r['credit_limit'];?>
    <tr class="<?=$over?'table-danger bg-opacity-5':''?>">
      <td class="text-muted"><?=$i+1?></td><td class="fw-semibold"><?=e($r['customer_name'])?></td><td class="text-muted small"><?=e($r['phone']??'—')?></td>
      <td class="text-end small"><?=$r['credit_limit']>0?currency($r['credit_limit']):'—'?></td>
      <td class="text-end fw-bold text-danger"><?=currency($r['current_balance'])?></td>
      <td class="text-end"><?=number_format($r['open_invoices'])?></td>
      <td class="small"><?=$r['last_sale']?fDate($r['last_sale']):'—'?></td>
      <td><?php if($over):?><span class="badge bg-danger">Over Limit</span><?php else:?><span class="badge bg-warning text-dark">Due</span><?php endif;?></td>
    </tr>
    <?php endforeach; if(empty($rows)):?><tr><td colspan="8" class="text-center text-muted py-4">No customers with outstanding balance.</td></tr><?php endif;?>
  </tbody>
  <tfoot class="table-secondary fw-bold"><tr><td colspan="4">TOTAL (<?=count($rows)?> customers)</td><td class="text-end text-danger"><?=currency($total)?></td><td colspan="3"></td></tr></tfoot>
</table></div></div>