<?php
declare(strict_types=1);

require __DIR__ . '/auth.php';
require __DIR__ . '/db.php';

$stmt = $pdo->query("SELECT id, title, type FROM files WHERE is_active = 1 ORDER BY id DESC");
$files = $stmt->fetchAll();
?>
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Library</title>
  <style>
    body { font-family: Arial, sans-serif; margin: 24px; }
    .top { display:flex; justify-content: space-between; align-items:center; }
    .btn { padding: 7px 12px; border:1px solid #333; text-decoration:none; border-radius: 8px; }
    li { margin: 10px 0; }
    .tag { font-size: 12px; color: #444; padding: 2px 8px; border:1px solid #ddd; border-radius: 999px; margin-left: 8px; }
  </style>
</head>
<body>
  <div class="top">
    <h2>Your Library</h2>
    <a class="btn" href="logout.php">Logout</a>
  </div>

  <ul>
    <?php foreach ($files as $f): ?>
      <li>
        <?= htmlspecialchars($f['title']) ?>
        <span class="tag"><?= htmlspecialchars($f['type']) ?></span>
        <a class="btn" target="_blank" href="viewer.php?file_id=<?= (int)$f['id'] ?>">Open</a>
      </li>
    <?php endforeach; ?>
  </ul>
</body>
</html>
