<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
*{box-sizing:border-box;margin:0;padding:0}
body{font-family:'Segoe UI',Arial,sans-serif;font-size:11px;color:#1e293b;background:#f1f5f9;padding:16px}
.page{background:#fff;max-width:680px;margin:0 auto;border-radius:10px;overflow:hidden;box-shadow:0 4px 20px rgba(0,0,0,.12)}
/* Header */
.rc-header{background:linear-gradient(135deg,#1a7f5a,#145f44);color:#fff;padding:14px 20px;display:flex;align-items:center;gap:14px}
.rc-logo{width:54px;height:54px;border-radius:50%;border:2px solid rgba(255,255,255,.4);overflow:hidden;flex-shrink:0;display:flex;align-items:center;justify-content:center;background:rgba(255,255,255,.15);font-size:20px;font-weight:800}
.rc-logo img{width:54px;height:54px;object-fit:contain;border-radius:50%}
.rc-school{flex:1;text-align:center}
.rc-school h1{font-size:14px;font-weight:800;text-transform:uppercase;letter-spacing:.3px;margin-bottom:2px}
.rc-school p{font-size:10px;opacity:.85;line-height:1.5}
.rc-badge{background:rgba(255,255,255,.15);border:1px solid rgba(255,255,255,.3);border-radius:6px;padding:6px 14px;text-align:center;flex-shrink:0}
.rc-badge h2{font-size:10px;font-weight:800;text-transform:uppercase;letter-spacing:.5px}
.rc-badge .rno{font-size:13px;font-weight:800;margin-top:3px;letter-spacing:.5px}
/* Body */
.body{padding:14px 20px}
/* Student info strip */
.student-strip{display:grid;grid-template-columns:repeat(4,1fr);border:1px solid #e2e8f0;border-radius:8px;overflow:hidden;margin-bottom:14px}
.strip-item{padding:8px 12px;border-right:1px solid #e2e8f0}
.strip-item:last-child{border-right:none}
.strip-label{font-size:9px;font-weight:700;text-transform:uppercase;color:#64748b;margin-bottom:2px}
.strip-value{font-size:12px;font-weight:600}
/* This payment highlight */
.this-payment{background:linear-gradient(135deg,#f0fdf4,#dcfce7);border:2px solid #16a34a;border-radius:8px;padding:12px 16px;margin-bottom:14px;display:flex;align-items:center;justify-content:space-between}
.this-payment .label{font-size:10px;font-weight:700;text-transform:uppercase;color:#166534}
.this-payment .amount{font-size:28px;font-weight:800;color:#16a34a}
.this-payment .meta{font-size:10px;color:#166534;margin-top:2px}
/* Payment history table */
.section-title{font-size:10px;font-weight:700;text-transform:uppercase;color:#1a7f5a;letter-spacing:.5px;margin-bottom:6px;padding-bottom:4px;border-bottom:1px solid #e2e8f0}
table{width:100%;border-collapse:collapse;font-size:11px}
thead tr{background:#1a7f5a;color:#fff}
th{padding:6px 10px;text-align:left;font-size:10px;font-weight:600;text-transform:uppercase}
td{padding:6px 10px;border-bottom:1px solid #f1f5f9}
tr:nth-child(even) td{background:#f8fafc}
.current-row td{background:#fef9c3 !important;font-weight:600}
/* Balance summary */
.balance-strip{display:grid;grid-template-columns:1fr 1fr 1fr;border:1px solid #e2e8f0;border-radius:8px;overflow:hidden;margin-top:14px}
.bal-item{padding:10px 14px;text-align:center;border-right:1px solid #e2e8f0}
.bal-item:last-child{border-right:none}
.bal-label{font-size:9px;font-weight:700;text-transform:uppercase;color:#64748b;margin-bottom:3px}
.bal-value{font-size:15px;font-weight:800}
/* Footer */
.footer{padding:10px 20px;background:#f8fafc;border-top:1px solid #e2e8f0;display:flex;justify-content:space-between;align-items:center}
.footer p{font-size:10px;color:#94a3b8}
.print-bar{max-width:680px;margin:0 auto 10px;display:flex;gap:8px;justify-content:flex-end}
@media print{body{background:#fff;padding:0}.print-bar{display:none}.page{box-shadow:none;border-radius:0}}
</style>
</head>
<body>

<div class="print-bar">
  <button onclick="window.print()" style="background:#1a7f5a;color:#fff;border:none;padding:8px 18px;border-radius:6px;cursor:pointer;font-size:12px;font-weight:600">🖨️ Print / Save PDF</button>
  <button onclick="window.close()" style="background:#f1f5f9;color:#1e293b;border:1px solid #e2e8f0;padding:8px 18px;border-radius:6px;cursor:pointer;font-size:12px">✕ Close</button>
</div>

<div class="page">
  <!-- Header -->
  <div class="rc-header">
    <div class="rc-logo">
      <?php if(!empty($school['logo'])): ?>
        <img src="<?= \Core\View::url('storage/uploads/logos/'.$school['logo']) ?>">
      <?php else: ?>
        <?= strtoupper(substr($school['name'] ?? 'E', 0, 1)) ?>
      <?php endif; ?>
    </div>
    <div class="rc-school">
      <h1><?= htmlspecialchars($school['name'] ?? 'School') ?></h1>
      <p>
        <?= htmlspecialchars($school['address'] ?? '') ?>
        <?php if($school['phone'] ?? ''): ?> &nbsp;|&nbsp; Tel: <?= htmlspecialchars($school['phone']) ?><?php endif; ?>
        <?php if($school['email'] ?? ''): ?><br><?= htmlspecialchars($school['email']) ?><?php endif; ?>
      </p>
    </div>
    <div class="rc-badge">
      <h2>Official Receipt</h2>
      <div class="rno"><?= htmlspecialchars($payment['receipt_no']) ?></div>
    </div>
  </div>

  <div class="body">
    <!-- Student Info -->
    <div class="student-strip">
      <div class="strip-item">
        <div class="strip-label">Student Name</div>
        <div class="strip-value"><?= htmlspecialchars($payment['student_name']) ?></div>
      </div>
      <div class="strip-item">
        <div class="strip-label">Assessment No</div>
        <div class="strip-value"><?= htmlspecialchars($payment['assessment_no']) ?></div>
      </div>
      <div class="strip-item">
        <div class="strip-label">Term</div>
        <div class="strip-value"><?= htmlspecialchars($payment['term_name']) ?></div>
      </div>
      <div class="strip-item">
        <div class="strip-label">Received By</div>
        <div class="strip-value"><?= htmlspecialchars($payment['received_by_name'] ?? 'System') ?></div>
      </div>
    </div>

    <!-- This Payment Highlight -->
    <div class="this-payment">
      <div>
        <div class="label">Amount Received</div>
        <div class="amount">KES <?= number_format($payment['amount'], 2) ?></div>
        <div class="meta">
          <?= date('d F Y', strtotime($payment['payment_date'])) ?>
          &nbsp;|&nbsp; <?= strtoupper($payment['method']) ?>
          <?php if($payment['transaction_ref'] ?? ''): ?> &nbsp;|&nbsp; Ref: <?= htmlspecialchars($payment['transaction_ref']) ?><?php endif; ?>
        </div>
      </div>
      <div style="text-align:right">
        <?php
        $bal = $payment['total_amount'] - $payment['amount_paid'];
        $status = $bal <= 0 ? 'FULLY PAID' : ($payment['amount_paid'] > 0 ? 'PARTIAL' : 'UNPAID');
        $statusColor = $bal <= 0 ? '#16a34a' : ($payment['amount_paid'] > 0 ? '#d97706' : '#dc2626');
        ?>
        <div style="font-size:9px;font-weight:700;text-transform:uppercase;color:#166534;margin-bottom:4px">Account Status</div>
        <div style="background:<?= $statusColor ?>;color:#fff;padding:4px 14px;border-radius:20px;font-size:11px;font-weight:700"><?= $status ?></div>
      </div>
    </div>

    <!-- Payment History -->
    <div class="section-title">Payment History — All Transactions</div>
    <table>
      <thead>
        <tr>
          <th>#</th>
          <th>Date</th>
          <th>Receipt No</th>
          <th>Method</th>
          <th>Reference</th>
          <th>Amount (KES)</th>
          <th>Running Balance</th>
        </tr>
      </thead>
      <tbody>
        <?php
        $running = (float)$payment['total_amount'];
        foreach ($allPayments as $i => $p):
          $running -= (float)$p['amount'];
          $isCurrent = $p['id'] == $payment['id'];
        ?>
        <tr class="<?= $isCurrent ? 'current-row' : '' ?>">
          <td><?= $i + 1 ?></td>
          <td><?= date('d/m/Y', strtotime($p['payment_date'])) ?></td>
          <td style="color:#1a7f5a;font-weight:<?= $isCurrent?'700':'400' ?>"><?= htmlspecialchars($p['receipt_no']) ?><?= $isCurrent ? ' ◀' : '' ?></td>
          <td><?= strtoupper($p['method']) ?></td>
          <td style="color:#64748b"><?= htmlspecialchars($p['transaction_ref'] ?? '—') ?></td>
          <td style="font-weight:600;color:#16a34a"><?= number_format($p['amount'], 2) ?></td>
          <td style="font-weight:700;color:<?= $running<=0?'#16a34a':'#dc2626' ?>"><?= number_format(max(0,$running), 2) ?></td>
        </tr>
        <?php endforeach; ?>
      </tbody>
    </table>

    <!-- Balance Summary -->
    <div class="balance-strip">
      <div class="bal-item">
        <div class="bal-label">Term Invoice</div>
        <div class="bal-value">KES <?= number_format($payment['total_amount'], 2) ?></div>
      </div>
      <div class="bal-item">
        <div class="bal-label">Total Paid</div>
        <div class="bal-value" style="color:#16a34a">KES <?= number_format($payment['amount_paid'], 2) ?></div>
      </div>
      <div class="bal-item" style="background:<?= $bal<=0?'#f0fdf4':'#fef2f2' ?>">
        <div class="bal-label">Balance Due</div>
        <div class="bal-value" style="color:<?= $bal<=0?'#16a34a':'#dc2626' ?>">KES <?= number_format(max(0,$bal), 2) ?></div>
      </div>
    </div>
  </div>

  <!-- Footer -->
  <div class="footer">
    <p>Generated: <?= date('d F Y, H:i') ?></p>
    <p>This is a computer-generated receipt. Keep for your records.</p>
    <p><?= htmlspecialchars($school['name'] ?? '') ?></p>
  </div>
</div>
</body>
</html>

