<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Library Borrowings — <?= htmlspecialchars($school['name'] ?? '') ?></title>
<style>
  *{box-sizing:border-box;margin:0;padding:0}
  body{font-family:'Segoe UI',Arial,sans-serif;font-size:11px;color:#1e293b;background:#fff;padding:16px}
  .print-bar{display:flex;gap:10px;justify-content:flex-end;margin-bottom:12px}
  .print-bar button{background:#1a7f5a;color:#fff;border:none;padding:8px 18px;border-radius:6px;cursor:pointer;font-size:13px}
  .print-bar a{background:#f1f5f9;color:#1e293b;border:1px solid #e2e8f0;padding:8px 18px;border-radius:6px;text-decoration:none;font-size:13px}

  /* Letterhead */
  .letterhead{display:flex;align-items:center;gap:16px;padding-bottom:12px;border-bottom:3px solid #1a7f5a;margin-bottom:16px}
  .lh-logo{width:70px;height:70px;object-fit:contain;border-radius:50%}
  .lh-logo-placeholder{width:70px;height:70px;background:#1a7f5a;border-radius:50%;display:flex;align-items:center;justify-content:center;color:#fff;font-size:28px;font-weight:800;flex-shrink:0}
  .lh-info{flex:1;text-align:center}
  .lh-info h1{font-size:18px;font-weight:800;text-transform:uppercase;color:#1a7f5a;letter-spacing:.5px}
  .lh-info p{font-size:10px;color:#64748b;margin-top:3px}
  .lh-badge{text-align:right;font-size:10px;color:#64748b}
  .lh-badge strong{display:block;font-size:13px;color:#1e293b}

  /* Report title */
  .report-title{text-align:center;margin-bottom:14px}
  .report-title h2{font-size:14px;font-weight:700;text-transform:uppercase;letter-spacing:.5px}
  .report-title p{font-size:11px;color:#64748b;margin-top:3px}

  /* Filters summary */
  .filter-bar{display:flex;gap:16px;background:#f8fafc;padding:8px 12px;border-radius:6px;margin-bottom:14px;font-size:11px;color:#64748b}

  /* Table */
  table{width:100%;border-collapse:collapse;font-size:11px}
  thead tr{background:#1a7f5a;color:#fff}
  th{padding:7px 10px;text-align:left;font-size:10px;font-weight:600;text-transform:uppercase;letter-spacing:.3px}
  td{padding:6px 10px;border-bottom:1px solid #f1f5f9}
  tr:nth-child(even) td{background:#f8fafc}
  .badge{display:inline-flex;padding:2px 7px;border-radius:10px;font-size:10px;font-weight:600}
  .b-success{background:#d1fae5;color:#065f46}
  .b-warning{background:#fef3c7;color:#92400e}
  .b-danger{background:#fee2e2;color:#991b1b}
  .b-info{background:#dbeafe;color:#1e40af}

  /* Summary */
  .summary{display:grid;grid-template-columns:repeat(4,1fr);gap:10px;margin-bottom:16px}
  .sum-box{border:1px solid #e2e8f0;border-radius:8px;padding:10px;text-align:center}
  .sum-box .val{font-size:20px;font-weight:700;color:#1a7f5a}
  .sum-box .lbl{font-size:10px;color:#64748b;margin-top:2px}

  /* Footer */
  .footer{margin-top:20px;padding-top:10px;border-top:1px solid #e2e8f0;text-align:center;font-size:10px;color:#94a3b8}

  @media print{
    body{padding:0}
    .print-bar{display:none}
  }
</style>
</head>
<body>

<div class="print-bar">
  <button onclick="window.print()">🖨️ Print / Save PDF</button>
  <a href="javascript:window.close()">✕ Close</a>
</div>

<!-- Letterhead -->
<div class="letterhead">
  <?php if(!empty($school['logo'])): ?>
  <img src="<?= \Core\View::url('storage/uploads/logos/'.$school['logo']) ?>" class="lh-logo" alt="Logo">
  <?php else: ?>
  <div class="lh-logo-placeholder"><?= strtoupper(substr($school['name'] ?? 'E', 0, 1)) ?></div>
  <?php endif; ?>
  <div class="lh-info">
    <h1><?= htmlspecialchars($school['name'] ?? 'School') ?></h1>
    <p>
      <?= htmlspecialchars($school['address'] ?? '') ?>
      <?php if($school['phone'] ?? ''): ?> | Tel: <?= htmlspecialchars($school['phone']) ?><?php endif; ?>
      <?php if($school['email'] ?? ''): ?> | <?= htmlspecialchars($school['email']) ?><?php endif; ?>
    </p>
    <?php if($school['motto'] ?? ''): ?><p style="font-style:italic;color:#1a7f5a;margin-top:2px">"<?= htmlspecialchars($school['motto']) ?>"</p><?php endif; ?>
  </div>
  <div class="lh-badge">
    <strong>Library Report</strong>
    <?= date('d F Y') ?>
  </div>
</div>

<!-- Report Title -->
<div class="report-title">
  <h2>Library Borrowings Report</h2>
  <p>Generated on <?= date('d F Y, H:i') ?></p>
</div>

<!-- Filter Summary -->
<?php
$filterParts = [];
if ($filters['status'])   $filterParts[] = 'Status: '.ucfirst($filters['status']);
if ($filters['type'])     $filterParts[] = 'Type: '.ucfirst($filters['type']).'s';
if ($filters['streamId']) {
    foreach($streams as $st) { if($st['id']==$filters['streamId']) { $filterParts[] = 'Stream: '.$st['full_name']; break; } }
}
if ($filters['search'])   $filterParts[] = 'Search: "'.$filters['search'].'"';
?>
<?php if(!empty($filterParts)): ?>
<div class="filter-bar">
  <span><strong>Filters:</strong></span>
  <?php foreach($filterParts as $fp): ?><span><?= htmlspecialchars($fp) ?></span><?php endforeach; ?>
</div>
<?php endif; ?>

<!-- Summary Stats -->
<?php
$total    = count($allRows);
$borrowed = count(array_filter($allRows, function($r){ return $r['status']==='borrowed'; }));
$returned = count(array_filter($allRows, function($r){ return $r['status']==='returned'; }));
$overdue  = count(array_filter($allRows, function($r){ return $r['status']==='borrowed' && $r['days_overdue'] > 0; }));
?>
<div class="summary">
  <div class="sum-box"><div class="val"><?= $total ?></div><div class="lbl">Total Records</div></div>
  <div class="sum-box"><div class="val" style="color:#92400e"><?= $borrowed ?></div><div class="lbl">Currently Borrowed</div></div>
  <div class="sum-box"><div class="val" style="color:#065f46"><?= $returned ?></div><div class="lbl">Returned</div></div>
  <div class="sum-box"><div class="val" style="color:#991b1b"><?= $overdue ?></div><div class="lbl">Overdue</div></div>
</div>

<!-- Table -->
<table>
  <thead>
    <tr>
      <th>#</th>
      <th>Borrower</th>
      <th>Type</th>
      <th>Book Title</th>
      <th>Author</th>
      <th>Borrowed</th>
      <th>Due Date</th>
      <th>Status</th>
      <th>Fine (KES)</th>
    </tr>
  </thead>
  <tbody>
    <?php foreach($allRows as $i => $r):
      $isOverdue = $r['status']==='borrowed' && $r['days_overdue'] > 0;
      $fine = $isOverdue ? $r['days_overdue'] * 5 : ($r['fine'] ?? 0);
    ?>
    <tr>
      <td><?= $i+1 ?></td>
      <td>
        <strong><?= htmlspecialchars($r['borrower_name'] ?? '') ?></strong>
        <?php if($r['borrower_ref'] ?? ''): ?><br><span style="color:#64748b"><?= htmlspecialchars($r['borrower_ref']) ?></span><?php endif; ?>
      </td>
      <td><span class="badge <?= $r['borrower_type']==='Teacher'?'b-info':'b-success' ?>"><?= $r['borrower_type'] ?></span></td>
      <td><strong><?= htmlspecialchars($r['book_title']) ?></strong></td>
      <td style="color:#64748b"><?= htmlspecialchars($r['author'] ?? '—') ?></td>
      <td><?= $r['borrow_date'] ?></td>
      <td style="color:<?= $isOverdue?'#dc2626':'inherit' ?>;font-weight:<?= $isOverdue?'600':'400' ?>">
        <?= $r['due_date'] ?>
        <?php if($isOverdue): ?><br><span style="font-size:10px;color:#dc2626"><?= $r['days_overdue'] ?> days overdue</span><?php endif; ?>
      </td>
      <td>
        <span class="badge <?= $r['status']==='returned'?'b-success':($isOverdue?'b-danger':'b-warning') ?>">
          <?= $r['status']==='returned' ? 'Returned' : ($isOverdue ? 'Overdue' : 'Borrowed') ?>
        </span>
      </td>
      <td style="color:<?= $fine>0?'#dc2626':'#64748b' ?>;font-weight:<?= $fine>0?'600':'400' ?>">
        <?= $fine > 0 ? number_format($fine, 2) : '—' ?>
      </td>
    </tr>
    <?php endforeach; ?>
    <?php if(empty($allRows)): ?>
    <tr><td colspan="9" style="text-align:center;padding:30px;color:#64748b">No records found</td></tr>
    <?php endif; ?>
  </tbody>
</table>

<!-- Footer -->
<div class="footer">
  <?= htmlspecialchars($school['name'] ?? '') ?> &mdash; Library Management System &mdash; Printed <?= date('d F Y, H:i') ?>
  <?php if($school['letterhead_footer'] ?? ''): ?><br><?= htmlspecialchars($school['letterhead_footer']) ?><?php endif; ?>
</div>

</body>
</html>
