<?php
require_once __DIR__ . '/../../admin/_nav.php';
require_once VIEWS . '/accountant/_nav.php';
$pageTitle  = 'Payment History';
$_role = \Core\Session::get('user')['role'] ?? '';
$sidebarNav = $_role === 'accountant' ? accountantNav() : adminNav();
$u = function($p) { return \Core\View::url($p); };
$totalPaid = array_sum(array_column($payments, 'amount'));
?>
<div class="page-header">
  <div>
    <h1><i class="bi bi-clock-history"></i> Payment History</h1>
    <p><?= htmlspecialchars($invoice['student_name'] ?? '') ?> — <?= htmlspecialchars($invoice['term_name'] ?? '') ?></p>
  </div>
  <a href="<?= $u('admin/fees/invoices') ?>" class="btn btn-secondary"><i class="bi bi-arrow-left"></i> Back</a>
</div>

<div class="grid g3" style="margin-bottom:24px">
  <div class="stat-card"><div class="stat-icon si-blue"><i class="bi bi-receipt"></i></div><div class="stat-info"><h3>KES <?= number_format($invoice['total_amount'],2) ?></h3><p>Invoice Total</p></div></div>
  <div class="stat-card"><div class="stat-icon si-green"><i class="bi bi-check-circle"></i></div><div class="stat-info"><h3>KES <?= number_format($totalPaid,2) ?></h3><p>Total Paid</p></div></div>
  <div class="stat-card"><div class="stat-icon si-red"><i class="bi bi-exclamation-circle"></i></div><div class="stat-info"><h3>KES <?= number_format($invoice['total_amount']-$totalPaid,2) ?></h3><p>Balance</p></div></div>
</div>

<div class="card">
  <div class="card-header"><h5>All Payments</h5></div>
  <div class="table-wrap"><table>
    <thead><tr><th>Receipt No</th><th>Date</th><th>Amount</th><th>Method</th><th>Reference</th><th>Notes</th><th>Receipt</th></tr></thead>
    <tbody>
      <?php foreach($payments as $p): ?>
      <tr>
        <td><strong style="color:var(--primary)"><?= htmlspecialchars($p['receipt_no']) ?></strong></td>
        <td><?= $p['payment_date'] ?></td>
        <td><strong>KES <?= number_format($p['amount'],2) ?></strong></td>
        <td><span class="badge badge-info"><?= strtoupper($p['method']) ?></span></td>
        <td style="font-size:12px"><?= htmlspecialchars($p['transaction_ref'] ?? '—') ?></td>
        <td style="font-size:12px;color:var(--gray)"><?= htmlspecialchars($p['notes'] ?? '—') ?></td>
        <td><a href="<?= $u('admin/fees/receipt/'.$p['id']) ?>" class="btn btn-sm btn-secondary" target="_blank"><i class="bi bi-receipt"></i></a></td>
      </tr>
      <?php endforeach; ?>
      <?php if(empty($payments)): ?><tr><td colspan="7" style="text-align:center;padding:40px;color:var(--gray)">No payments recorded yet</td></tr><?php endif; ?>
    </tbody>
  </table></div>
</div>

