<?php
require_once __DIR__ . '/../admin/_nav.php';
$pageTitle  = 'Library Dashboard';
$sidebarNav = librarianNav();
$u = function($p) { return \Core\View::url($p); };
$hour  = (int)date('H');
$greet = $hour < 12 ? 'Good morning' : ($hour < 17 ? 'Good afternoon' : 'Good evening');
$user  = \Core\Session::get('user');
?>

<div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:24px;flex-wrap:wrap;gap:12px">
  <div>
    <h1 style="font-size:22px;font-weight:700"><?= $greet ?>, <?= htmlspecialchars(explode(' ', $user['name'] ?? '')[0]) ?> 👋</h1>
    <p style="color:var(--gray);font-size:13px;margin-top:2px"><i class="bi bi-book"></i> <?= date('l, d F Y') ?> &bull; Librarian</p>
  </div>
  <div style="display:flex;gap:8px">
    <a href="<?= $u('admin/library') ?>" class="btn btn-primary"><i class="bi bi-plus-lg"></i> Add Book</a>
    <a href="<?= $u('admin/library/borrowings') ?>" class="btn btn-secondary"><i class="bi bi-arrow-left-right"></i> Borrowings</a>
  </div>
</div>

<!-- KPI Cards -->
<div class="grid g4" style="margin-bottom:20px">
  <div class="stat-card">
    <div class="stat-icon si-blue"><i class="bi bi-book"></i></div>
    <div class="stat-info"><h3><?= $stats['total_books'] ?? 0 ?></h3><p>Total Titles</p></div>
  </div>
  <div class="stat-card">
    <div class="stat-icon si-green"><i class="bi bi-stack"></i></div>
    <div class="stat-info"><h3><?= $stats['total_copies'] ?? 0 ?></h3><p>Total Copies</p></div>
  </div>
  <div class="stat-card">
    <div class="stat-icon si-amber"><i class="bi bi-person-check"></i></div>
    <div class="stat-info"><h3><?= $stats['borrowed'] ?? 0 ?></h3><p>Currently Borrowed</p></div>
    <div style="margin-left:auto"><a href="<?= $u('admin/library/borrowings?status=borrowed') ?>" class="btn btn-sm btn-secondary">View</a></div>
  </div>
  <div class="stat-card">
    <div class="stat-icon si-red"><i class="bi bi-exclamation-triangle"></i></div>
    <div class="stat-info"><h3><?= $stats['overdue'] ?? 0 ?></h3><p>Overdue</p></div>
    <?php if(($stats['overdue']??0)>0): ?>
    <div style="margin-left:auto"><a href="<?= $u('admin/library/borrowings?status=borrowed') ?>" class="btn btn-sm btn-danger">View</a></div>
    <?php endif; ?>
  </div>
</div>

<div class="grid g2" style="margin-bottom:20px">

  <!-- Recent Borrowings -->
  <div class="card">
    <div class="card-header">
      <h5><i class="bi bi-clock-history"></i> Recent Borrowings</h5>
      <a href="<?= $u('admin/library/borrowings') ?>" class="btn btn-sm btn-secondary">View All</a>
    </div>
    <div class="card-body" style="padding:0">
      <?php if(empty($recentBorrowings)): ?>
      <div style="padding:30px;text-align:center;color:var(--gray)">No borrowings yet</div>
      <?php else: ?>
      <?php foreach($recentBorrowings as $b):
        $isOverdue = $b['status']==='borrowed' && $b['days_overdue'] > 0; ?>
      <div style="padding:11px 18px;border-bottom:1px solid var(--border);display:flex;align-items:center;gap:10px">
        <div class="stat-icon <?= $b['status']==='returned'?'si-green':($isOverdue?'si-red':'si-amber') ?>" style="width:34px;height:34px;font-size:14px;border-radius:8px;flex-shrink:0">
          <i class="bi bi-<?= $b['status']==='returned'?'check':'book' ?>"></i>
        </div>
        <div style="flex:1;min-width:0">
          <div style="font-weight:500;font-size:13px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap"><?= htmlspecialchars($b['book_title']) ?></div>
          <div style="font-size:11px;color:var(--gray)"><?= htmlspecialchars($b['borrower_name'] ?? '') ?> &bull; Due: <?= $b['due_date'] ?></div>
        </div>
        <span class="badge <?= $b['status']==='returned'?'badge-success':($isOverdue?'badge-danger':'badge-warning') ?>">
          <?= $b['status']==='returned'?'Returned':($isOverdue?'Overdue':'Borrowed') ?>
        </span>
      </div>
      <?php endforeach; ?>
      <?php endif; ?>
    </div>
  </div>

  <!-- Overdue + Top Books -->
  <div style="display:flex;flex-direction:column;gap:16px">

    <!-- Overdue -->
    <div class="card">
      <div class="card-header">
        <h5><i class="bi bi-exclamation-triangle" style="color:var(--danger)"></i> Overdue Books</h5>
      </div>
      <div class="card-body" style="padding:0">
        <?php if(empty($overdueList)): ?>
        <div style="padding:20px;text-align:center;color:var(--gray)"><i class="bi bi-check-circle" style="color:var(--success)"></i> No overdue books</div>
        <?php else: ?>
        <?php foreach($overdueList as $o): ?>
        <div style="padding:10px 18px;border-bottom:1px solid var(--border)">
          <div style="font-weight:500;font-size:13px"><?= htmlspecialchars($o['book_title']) ?></div>
          <div style="font-size:11px;color:var(--gray)"><?= htmlspecialchars($o['borrower_name'] ?? '') ?> &bull; <span style="color:var(--danger);font-weight:600"><?= $o['days_overdue'] ?> days overdue</span></div>
        </div>
        <?php endforeach; ?>
        <?php endif; ?>
      </div>
    </div>

    <!-- Most Borrowed -->
    <div class="card">
      <div class="card-header"><h5><i class="bi bi-trophy"></i> Most Borrowed Books</h5></div>
      <div class="card-body" style="padding:0">
        <?php if(empty($topBooks)): ?>
        <div style="padding:20px;text-align:center;color:var(--gray)">No data yet</div>
        <?php else: ?>
        <?php foreach($topBooks as $i => $t): ?>
        <div style="padding:10px 18px;border-bottom:1px solid var(--border);display:flex;align-items:center;gap:10px">
          <div style="width:24px;height:24px;border-radius:50%;background:<?= $i===0?'#f59e0b':($i===1?'#94a3b8':($i===2?'#b45309':'var(--light)')) ?>;color:<?= $i<3?'#fff':'var(--gray)' ?>;display:flex;align-items:center;justify-content:center;font-size:11px;font-weight:700;flex-shrink:0"><?= $i+1 ?></div>
          <div style="flex:1">
            <div style="font-weight:500;font-size:13px"><?= htmlspecialchars($t['title']) ?></div>
            <div style="font-size:11px;color:var(--gray)"><?= htmlspecialchars($t['author'] ?? '') ?></div>
          </div>
          <span class="badge badge-info"><?= $t['borrow_count'] ?>x</span>
        </div>
        <?php endforeach; ?>
        <?php endif; ?>
      </div>
    </div>

  </div>
</div>

<!-- Quick Actions -->
<div class="card">
  <div class="card-header"><h5><i class="bi bi-lightning-charge"></i> Quick Actions</h5></div>
  <div class="card-body">
    <div style="display:flex;gap:10px;flex-wrap:wrap">
      <a href="<?= $u('admin/library') ?>" class="btn btn-primary"><i class="bi bi-plus-lg"></i> Add Book</a>
      <a href="<?= $u('admin/library/borrowings') ?>" class="btn btn-secondary"><i class="bi bi-arrow-left-right"></i> All Borrowings</a>
      <a href="<?= $u('admin/library/borrowings?status=borrowed') ?>" class="btn btn-secondary"><i class="bi bi-person-check"></i> Currently Borrowed</a>
      <a href="<?= $u('admin/library/borrowings?status=borrowed') ?>" class="btn btn-secondary"><i class="bi bi-exclamation-triangle"></i> Overdue</a>
      <a href="<?= $u('admin/library/borrowings?print=1') ?>" target="_blank" class="btn btn-secondary"><i class="bi bi-printer"></i> Print Report</a>
    </div>
  </div>
</div>
