<?php
declare(strict_types=1);

require_once __DIR__ . "/../config/db.php";
require_once __DIR__ . "/../lib/auth.php";
require_once __DIR__ . "/../lib/helpers.php";

require_admin();

if (!function_exists('e')) {
  function e($s): string { return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); }
}

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

/* TOGGLE ACTIVE */
if (isset($_GET['toggle'])) {
  $id = (int)$_GET['toggle'];
  if ($id > 0) {
    $pdo->prepare("UPDATE journals SET is_active = IF(is_active=1,0,1) WHERE id=?")->execute([$id]);
  }
  header("Location: journals.php");
  exit;
}

$rows = $pdo->query("
  SELECT id, title, slug, cover, pdf_file, is_active, created_at
  FROM journals
  ORDER BY id DESC
")->fetchAll(PDO::FETCH_ASSOC) ?: [];

?>
<?php include __DIR__ . "/admin_menu.php"; ?>

<div style="max-width:1100px; padding:18px;">
  <div style="display:flex; align-items:center; justify-content:space-between; gap:12px; margin-bottom:14px;">
    <h2 style="margin:0;">Online Jurnal</h2>
    <a href="journal_form.php?new=1" style="display:inline-block; padding:10px 14px; border-radius:10px; background:#0069b4; color:#fff; text-decoration:none;">
      + Yeni jurnal
    </a>
  </div>

  <div style="overflow:auto; border:1px solid rgba(0,0,0,.08); border-radius:12px; background:#fff;">
    <table style="width:100%; border-collapse:collapse; min-width:860px;">
      <thead>
        <tr style="background:rgba(0,0,0,.03);">
          <th style="text-align:left; padding:12px 10px; border-bottom:1px solid rgba(0,0,0,.08);">Başlıq</th>
          <th style="text-align:left; padding:12px 10px; border-bottom:1px solid rgba(0,0,0,.08);">Slug</th>
          <th style="text-align:left; padding:12px 10px; border-bottom:1px solid rgba(0,0,0,.08);">Cover</th>
          <th style="text-align:left; padding:12px 10px; border-bottom:1px solid rgba(0,0,0,.08);">PDF</th>
          <th style="text-align:left; padding:12px 10px; border-bottom:1px solid rgba(0,0,0,.08);">Status</th>
          <th style="text-align:left; padding:12px 10px; border-bottom:1px solid rgba(0,0,0,.08);">Tarix</th>
          <th style="text-align:left; padding:12px 10px; border-bottom:1px solid rgba(0,0,0,.08); width:220px;">Əməliyyat</th>
        </tr>
      </thead>

      <tbody>
        <?php if (!$rows): ?>
          <tr>
            <td colspan="7" style="padding:14px 10px;">Hələ jurnal əlavə edilməyib.</td>
          </tr>
        <?php else: ?>
          <?php foreach ($rows as $r): ?>
            <?php
              $id = (int)($r['id'] ?? 0);
              $isActive = (int)($r['is_active'] ?? 0) === 1;
              $created = (string)($r['created_at'] ?? '');
              $createdFmt = $created ? date('d.m.Y', strtotime($created)) : '-';
              $hasCover = trim((string)($r['cover'] ?? '')) !== '';
              $hasPdf   = trim((string)($r['pdf_file'] ?? '')) !== '';
            ?>
            <tr>
              <td style="padding:12px 10px; border-bottom:1px solid rgba(0,0,0,.06);">
                <strong><?= e($r['title'] ?? '') ?></strong>
              </td>

              <td style="padding:12px 10px; border-bottom:1px solid rgba(0,0,0,.06); color:#6b7a90;">
                <?= e($r['slug'] ?? '') ?>
              </td>

              <td style="padding:12px 10px; border-bottom:1px solid rgba(0,0,0,.06);">
                <?= $hasCover ? '✔ var' : '—' ?>
              </td>

              <td style="padding:12px 10px; border-bottom:1px solid rgba(0,0,0,.06);">
                <?= $hasPdf ? '✔ var' : '—' ?>
              </td>

              <td style="padding:12px 10px; border-bottom:1px solid rgba(0,0,0,.06);">
                <?php if ($isActive): ?>
                  <span style="display:inline-block; padding:4px 10px; border-radius:999px; background:rgba(99,160,4,.12); color:#3b6a02; font-size:12px; font-weight:700;">
                    Aktiv
                  </span>
                <?php else: ?>
                  <span style="display:inline-block; padding:4px 10px; border-radius:999px; background:rgba(0,0,0,.08); color:#4b5563; font-size:12px; font-weight:700;">
                    Deaktiv
                  </span>
                <?php endif; ?>
              </td>

              <td style="padding:12px 10px; border-bottom:1px solid rgba(0,0,0,.06);">
                <?= e($createdFmt) ?>
              </td>

              <td style="padding:12px 10px; border-bottom:1px solid rgba(0,0,0,.06);">
                <a href="journal_form.php?edit=<?= $id ?>" style="margin-right:10px;">Redaktə</a>

                <a href="journals.php?toggle=<?= $id ?>" style="margin-right:10px;">
                  <?= $isActive ? 'Deaktiv et' : 'Aktiv et' ?>
                </a>

                <a href="journals.php?delete=<?= $id ?>" onclick="return confirm('Silinsin?')" style="color:#b42318;">
                  Sil
                </a>
              </td>
            </tr>
          <?php endforeach; ?>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>