<?php
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();

if (isset($_GET['delete'])) {
  $id = (int)$_GET['delete'];
  if ($id > 0) $pdo->prepare("DELETE FROM journal_issues WHERE id=?")->execute([$id]);
  header("Location: journal_issues.php"); exit;
}

$rows = $pdo->query("SELECT id,title,issue_no,year,slug,is_active,sort_order FROM journal_issues ORDER BY sort_order ASC, id DESC")->fetchAll();
?>
<h2>Online Jurnal — Nömrələr</h2>
<p><a href="journal_issue_form.php?new=1">+ Yeni nömrə</a></p>

<table border="1" cellpadding="10">
  <tr><th>№/İl</th><th>Başlıq</th><th>Slug</th><th>Status</th><th></th></tr>
  <?php foreach($rows as $r): ?>
    <tr>
      <td><?= e($r['issue_no'] ?? '') ?> / <?= e($r['year'] ?? '') ?></td>
      <td><?= e($r['title']) ?></td>
      <td><?= e($r['slug']) ?></td>
      <td><?= (int)$r['is_active']===1 ? 'Aktiv' : 'Passiv' ?></td>
      <td>
        <a href="journal_issue_form.php?edit=<?= (int)$r['id'] ?>">Redaktə</a> |
        <a href="journal_issues.php?delete=<?= (int)$r['id'] ?>" onclick="return confirm('Silinsin?')">Sil</a>
      </td>
    </tr>
  <?php endforeach; ?>
</table>