<?php include APP_PATH.'/views/partials/report_header.php'; ?>
<div class="rpt-btn-bar no-print">
  <form method="GET" action="<?=APP_URL?>/reports/suppliers" 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="Supplier name…" value="<?=e($search)?>"></div>
    <button class="btn btn-secondary btn-sm">Filter</button>
    <a href="<?=APP_URL?>/reports/suppliers" 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">Suppliers 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 border-warning"><div class="fw-bold text-warning"><?=currency($pendingCheques)?></div><small class="text-muted">Pending Cheques</small></div></div>
  <div class="col-6 col-md-3"><div class="card text-center py-3"><div class="fw-bold"><?=$chequesCount?></div><small class="text-muted">Pending Cheque Count</small></div></div>
</div>
<div class="card"><div class="card-header fw-semibold small d-flex justify-content-between"><span>Supplier Outstanding Balances</span><small class="text-muted fw-normal"><?=count($rows)?> suppliers</small></div>
<div class="table-responsive"><table class="table table-sm table-hover mb-0">
  <thead class="table-dark"><tr><th>#</th><th>Supplier</th><th>Contact</th><th>Phone</th><th class="text-end">Outstanding</th><th class="text-end">Open Orders</th><th>Last Purchase</th><th>Status</th></tr></thead>
  <tbody>
    <?php foreach($rows as$i=>$r):?>
    <tr><td class="text-muted"><?=$i+1?></td><td class="fw-semibold"><?=e($r['supplier_name'])?></td><td class="small text-muted"><?=e($r['contact_person']??'—')?></td><td class="small text-muted"><?=e($r['phone']??'—')?></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_purchase']?fDate($r['last_purchase']):'—'?></td><td><span class="badge bg-danger">Due</span></td></tr>
    <?php endforeach; if(empty($rows)):?><tr><td colspan="8" class="text-center text-muted py-4">No suppliers with outstanding balance.</td></tr><?php endif;?>
  </tbody>
  <tfoot class="table-secondary fw-bold"><tr><td colspan="4">TOTAL</td><td class="text-end text-danger"><?=currency($total)?></td><td colspan="3"></td></tr></tfoot>
</table></div></div>