<?php
require 'db.php';

$year = $_GET['year'] ?? null;
$volume = $_GET['volume'] ?? null;
$serial = isset($_GET['serial']) ? intval($_GET['serial']) : null;

if (!$year || !$volume || $serial === null) {
    die("<h4 class='text-danger p-4'>❌ Missing parameters.</h4>");
}

// Fetch volume id
$stmt = $pdo->prepare("SELECT id FROM volumes WHERE year = ? AND volume_number = ?");
$stmt->execute([$year, $volume]);
$volumeRow = $stmt->fetch();

if (!$volumeRow) {
    die("<h4 class='text-danger p-4'>❌ Volume not found for " . htmlspecialchars($year) . " / Vol. " . htmlspecialchars($volume) . "</h4>");
}

$volume_id = $volumeRow['id'];

// Fetch published papers in volume ordered by submission_date ASC
$stmt = $pdo->prepare("SELECT * FROM papers WHERE volume_id = ? AND status = 'Published' ORDER BY submission_date ASC");
$stmt->execute([$volume_id]);
$papers = $stmt->fetchAll();

if (!isset($papers[$serial])) {
    die("<h4 class='text-danger p-4'>❌ No paper found at serial " . htmlspecialchars($serial) . "</h4>");
}

$paper = $papers[$serial];
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title><?= htmlspecialchars($paper['title']) ?> | Vidhina Journal</title>
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <!-- Bootstrap CSS -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" />
  <!-- PDF.js -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.min.js"></script>

  <style>
    body {
      background: #f4f8fc;
      font-family: 'Segoe UI', sans-serif;
      color: #002147;
    }
    .header-bar {
      background: #004a99;
      padding: 1rem;
      color: white;
      display: flex;
      align-items: center;
      gap: 1rem;
    }
    .header-bar img {
      height: 50px;
    }
    .header-bar h1 {
      font-size: 1.5rem;
      margin: 0;
    }
    .paper-container {
      max-width: 900px;
      margin: 2rem auto;
      background: #fff;
      padding: 2.5rem;
      border-radius: 1.5rem;
      box-shadow: 0 0 30px rgba(0,0,0,0.05);
    }
    .badge-keyword {
      background-color: #f39c12;
      color: white;
      margin: 0.1rem 0.2rem 0.1rem 0;
      padding: 0.2rem 0.5rem;
      font-size: 0.85rem;
      border-radius: 0.25rem;
      display: inline-block;
    }
    .pdf-viewer-wrapper {
      margin-top: 2rem;
      padding: 1rem;
      background: #eef3fa;
      border-radius: 12px;
      text-align: center;
    }
    canvas {
      border-radius: 10px;
      width: 100% !important;
      height: auto !important;
      box-shadow: 0 0 10px rgba(0,0,0,0.1);
    }
    /* Custom color */
    .text-orange {
      color: #ff6600;
      font-weight: 700;
    }
    .royal-header {
      background: linear-gradient(to right, #002147, #004a99);
      color: white;
      border-bottom: 4px solid #d4af37;
      flex-wrap: wrap;
    }
    .journal-title {
      font-family: 'Merriweather', serif;
      font-size: 1.8rem;
      font-weight: 700;
      letter-spacing: 0.8px;
      color: #fff;
    }
    .journal-subtitle {
      font-size: 0.95rem;
      font-style: italic;
      color: #e0e0e0;
    }
    .issn-text {
      font-size: 0.85rem;
      color: #ffc;
    }
    .gold-underline {
      width: 60px;
      height: 4px;
      background-color: #d4af37;
      border-radius: 3px;
    }
    .footer-bar {
      background-color: #f0f0f0;
      color: #555;
      text-align: center;
      font-size: 0.9rem;
      padding: 1rem;
      margin-top: 3rem;
      border-top: 1px solid #ccc;
    }
  </style>
</head>
<body>
<!-- Royal Header -->
<a href="index.php" class="text-decoration-none text-white">
  <div class="d-flex align-items-center px-4 py-3 shadow-sm royal-header">
    <img src="image/logo.png" alt="Vidhina Logo" class="me-3" style="height: 100px;" />
    <div>
      <h1 class="mb-1 journal-title">Vidhina Journal</h1>
      <div class="journal-subtitle">Publishing Today, Inspiring Tomorrow</div>
      <div class="issn-text mt-1">ISSN (Online): 3049-3986</div>
      <div class="gold-underline mt-2"></div>
    </div>
  </div>
</a>

<!-- Paper Details -->
<div class="paper-container">
  <h2 class="mb-2 text-primary"><?= htmlspecialchars($paper['title']) ?></h2>
  
  <p class="text-muted mb-1"><strong>Authors:</strong>
    <?= htmlspecialchars($paper['registered_author_id']) ?>
    <?php foreach (['other_author1', 'other_author2', 'other_author3'] as $co) {
      if (!empty($paper[$co])) echo ', ' . htmlspecialchars($paper[$co]);
    } ?>
  </p>

  <p class="text-muted">
    <strong>Published:</strong> <?= htmlspecialchars($paper['submission_date']) ?> |
    <strong>Volume:</strong> <?= htmlspecialchars($volume) ?> |
    <strong>Year:</strong> <?= htmlspecialchars($year) ?>
  </p>

  <hr />

  <div class="mb-4">
    <h5 class="text-orange">Abstract</h5>
    <p><?= nl2br(htmlspecialchars($paper['abstract'])) ?></p>
  </div>

  <div class="mb-4">
    <h6>Keywords</h6>
    <?php
      $keywords = explode(',', $paper['keywords']);
      foreach ($keywords as $kw) {
        echo "<span class='badge badge-keyword'>" . htmlspecialchars(trim($kw)) . "</span> ";
      }
    ?>
  </div>

  <!-- Button to reveal PDF -->
  <div class="text-center mt-4">
    <button class="btn btn-warning rounded-pill px-4 py-2 fw-bold" onclick="showPdf()">📄 Read Full Paper</button>
  </div>

  <!-- Hidden PDF viewer -->
  <div class="pdf-viewer-wrapper d-none" id="pdf-viewer-section" tabindex="-1" aria-label="Full paper preview">
    <h5 class="mt-5 mb-3 text-primary">📖 Full Paper Preview</h5>
    <div id="pdf-container"></div>
  </div>
</div>

<!-- Footer -->
<div class="footer-bar">
  &copy; <?= date('Y') ?> Vidhina Journal. All rights reserved.
</div>

<!-- Script to toggle and render PDF -->
<script>
  // Show PDF in canvas using PDF.js
  function showPdf() {
    const container = document.getElementById('pdf-container');
    const viewerSection = document.getElementById('pdf-viewer-section');
    viewerSection.classList.remove('d-none');
    viewerSection.focus();
    container.innerHTML = '';

    const url = <?= json_encode($paper['pdf_path']) ?>; // safer for JS injection
    pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.worker.min.js';

    pdfjsLib.getDocument(url).promise.then(function(pdf) {
      container.innerHTML = ''; // clear container before adding pages
      for (let pageNum = 1; pageNum <= pdf.numPages; pageNum++) {
        pdf.getPage(pageNum).then(function(page) {
          const scale = 1.2;
          const viewport = page.getViewport({ scale: scale });

          const canvas = document.createElement('canvas');
          canvas.classList.add('mb-4');
          const context = canvas.getContext('2d');
          canvas.height = viewport.height;
          canvas.width = viewport.width;

          container.appendChild(canvas);

          const renderContext = {
            canvasContext: context,
            viewport: viewport
          };
          page.render(renderContext);
        });
      }
    }).catch(function(err){
      container.innerHTML = '<p class="text-danger">Error loading PDF: ' + err.message + '</p>';
    });
  }
</script>

</body>
</html>


make its header general header

for /include/header.php

and footer