<?php
ini_set('display_errors','1');
ini_set('display_startup_errors','1');
error_reporting(E_ALL);
require_once __DIR__ . "/../config/db.php";
require_once __DIR__ . "/../lib/auth.php";
require_once __DIR__ . "/../lib/helpers.php";
require_once __DIR__ . "/../admin/admin_menu.php";

require_admin();

$rows = $pdo->query("SELECT id,title,slug,is_active,created_at FROM letters ORDER BY id DESC")->fetchAll();
?>
<!doctype html>
<html lang="az">
<head>
  <meta charset="utf-8">
  <title>Məktublar</title>
  <style>
    body{font-family:system-ui;margin:20px}
    table{border-collapse:collapse;width:100%}
    th,td{border:1px solid #ddd;padding:10px;text-align:left}
    .btn{display:inline-block;padding:8px 12px;border:1px solid #111;text-decoration:none;border-radius:8px}
    .muted{opacity:.65}
  </style>
</head>
<body>
<h2>Məktublar</h2>
<p><a class="btn" href="letter_form.php?new=1">+ Yeni məktub</a></p>

<table>
  <thead>
    <tr>
      <th>Tarix</th>
      <th>Başlıq</th>
      <th>Slug</th>
      <th>Status</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <?php foreach($rows as $r): ?>
      <tr>
        <td class="muted"><?= !empty($r['created_at']) ? e(date('d.m.Y', strtotime($r['created_at']))) : '-' ?></td>
        <td><?= e($r['title']) ?></td>
        <td class="muted"><?= e($r['slug']) ?></td>
        <td><?= (int)$r['is_active']===1 ? 'Aktiv' : 'Passiv' ?></td>
        <td>
          <a class="btn" href="letter_form.php?edit=<?= (int)$r['id'] ?>">Redaktə</a>
          <a class="btn" href="letter_delete.php?id=<?= (int)$r['id'] ?>" onclick="return confirm('Bu məktubu silmək istəyirsiniz?')">Sil</a>
        </td>
      </tr>
    <?php endforeach; ?>
  </tbody>
</table>
</body>
</html>