<?php
require_once __DIR__ . '/../../admin/_nav.php';
$pageTitle  = $student['name'];
$sidebarNav = adminNav();
$u = fn($p) => \Core\View::url($p);
?>
<div class="page-header">
  <div style="display:flex;align-items:center;gap:16px">
    <div class="avatar" style="width:52px;height:52px;font-size:20px"><?= strtoupper(substr($student['name'],0,1)) ?></div>
    <div><h1><?= htmlspecialchars($student['name']) ?></h1><p><?= htmlspecialchars($student['class_name'].' '.$student['stream_name']) ?> &bull; Asmt: <?= htmlspecialchars($student['assessment_no']) ?></p></div>
  </div>
  <div style="display:flex;gap:8px">
    <a href="<?= $u('admin/students/'.$student['id'].'/edit') ?>" class="btn btn-outline"><i class="bi bi-pencil"></i> Edit</a>
    <a href="<?= $u('admin/students') ?>" class="btn btn-secondary"><i class="bi bi-arrow-left"></i> Back</a>
  </div>
</div>
<div class="grid g2">
  <div class="card">
    <div class="card-header"><h5>Recent Marks</h5></div>
    <div class="card-body" style="padding:0">
      <?php foreach($marks as $m): ?>
      <div style="padding:10px 20px;border-bottom:1px solid var(--border);display:flex;justify-content:space-between">
        <div><div style="font-weight:500;font-size:13px"><?= htmlspecialchars($m['subject_name']) ?></div><div style="font-size:12px;color:var(--gray)"><?= htmlspecialchars($m['exam_name']) ?></div></div>
        <div style="text-align:right"><span style="font-weight:700"><?= $m['score'] ?>%</span> <span class="badge badge-info"><?= htmlspecialchars($m['grade']) ?></span></div>
      </div>
      <?php endforeach; ?>
      <?php if(empty($marks)): ?><div style="padding:20px;text-align:center;color:var(--gray)">No marks recorded</div><?php endif; ?>
    </div>
  </div>
  <div class="card">
    <div class="card-header"><h5>Fee Invoices</h5></div>
    <div class="card-body" style="padding:0">
      <?php foreach($invoices as $inv): ?>
      <?php $bal = $inv['total_amount'] - $inv['amount_paid']; ?>
      <div style="padding:10px 20px;border-bottom:1px solid var(--border);display:flex;justify-content:space-between;align-items:center">
        <div><div style="font-weight:500;font-size:13px"><?= htmlspecialchars($inv['term_name']) ?></div><div style="font-size:12px;color:var(--gray)">Total: KES <?= number_format($inv['total_amount']) ?></div></div>
        <div style="text-align:right">
          <div style="font-size:13px;color:<?= $bal>0?'var(--danger)':'var(--success)' ?>">Bal: KES <?= number_format($bal) ?></div>
          <span class="badge <?= $inv['status']==='paid'?'badge-success':($inv['status']==='partial'?'badge-warning':'badge-danger') ?>"><?= ucfirst($inv['status']) ?></span>
        </div>
      </div>
      <?php endforeach; ?>
      <?php if(empty($invoices)): ?><div style="padding:20px;text-align:center;color:var(--gray)">No invoices</div><?php endif; ?>
    </div>
  </div>
</div>

