<?php
declare(strict_types=1);

require_once __DIR__ . "/config/db.php";
function e($s): string { return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); }

function file_url(string $base, ?string $name): string {
  $name = trim((string)$name);
  if ($name === '') return '';
  if (preg_match('~^(https?://|/)~i', $name)) return $name;
  return rtrim($base, '/') . '/' . ltrim($name, '/');
}

$COVER_BASE = "/v2/uploads/journals";  // lazım olsa dəyiş
$PDF_BASE   = "/v2/uploads/journals";  // lazım olsa dəyiş

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($id <= 0) { http_response_code(404); die("Jurnal tapılmadı."); }

$stmt = $pdo->prepare("
  SELECT id, title, slug, cover, pdf_file, is_active, created_at
  FROM journals
  WHERE id=? LIMIT 1
");
$stmt->execute([$id]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$row) { http_response_code(404); die("Jurnal tapılmadı."); }
if ((int)($row['is_active'] ?? 0) !== 1) { http_response_code(404); die("Jurnal deaktivdir."); }

$title = (string)($row['title'] ?? 'Jurnal');
$cover = file_url($COVER_BASE, $row['cover'] ?? '');
$pdf   = file_url($PDF_BASE, $row['pdf_file'] ?? '');
?>

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

<!-- PAGE HEADER -->
<section class="page-header" style="background-image:url('/v2/img/ortho-bg.jpg');">
  <div class="page-header__overlay"></div>

  <div class="container page-header__content">
    <h1 class="page-title"><?= e($title) ?></h1>

    <nav class="breadcrumb" aria-label="breadcrumb">
      <a class="breadcrumb__link" href="/v2/">Ana səhifə</a>
      <span class="breadcrumb__sep">/</span>
      <a class="breadcrumb__link" href="/v2/jurnal.php">Jurnal</a>
      <span class="breadcrumb__sep">/</span>
      <span class="breadcrumb__current"><?= e($title) ?></span>
    </nav>
  </div>
</section>

<section class="container" style="max-width:960px; padding:40px 0;">

  <div style="display:flex; gap:18px; align-items:flex-start; flex-wrap:wrap; margin-bottom:18px;">
    <?php if ($cover !== ''): ?>
      <img src="<?= e($cover) ?>" alt="<?= e($title) ?>" style="width:220px; max-width:100%; border-radius:16px; border:1px solid rgba(0,0,0,.08);">
    <?php endif; ?>

    <div style="flex:1; min-width:260px;">
      <div style="font-weight:700; font-size:18px; margin-bottom:10px;"><?= e($title) ?></div>

      <?php if ($pdf !== ''): ?>
        <div style="display:flex; gap:10px; flex-wrap:wrap;">
          <a class="btn" href="<?= e($pdf) ?>" target="_blank" rel="noopener">PDF aç</a>
          <a class="btn" href="<?= e($pdf) ?>" download>PDF endir</a>
        </div>
      <?php else: ?>
        <div class="content-area">PDF faylı tapılmadı.</div>
      <?php endif; ?>
    </div>
  </div>

  <?php if ($pdf !== ''): ?>
    <div style="border:1px solid rgba(0,0,0,.08); border-radius:16px; overflow:hidden;">
      <iframe
        src="<?= e($pdf) ?>"
        style="width:100%; height:900px; border:0; display:block;"
        title="<?= e($title) ?>"
        loading="lazy"
      ></iframe>
    </div>
  <?php endif; ?>

</section>

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