<?php
session_start();
include('includes/config.php');
error_reporting(0);

// Allow both Admin and Student to view book details
if (!isset($_SESSION['alogin']) && !isset($_SESSION['login'])) {
    header('location:index.php');
    exit;
}

// Validate book ID
if (!isset($_GET['bookid']) || !is_numeric($_GET['bookid'])) {
    echo "Invalid book ID.";
    exit;
}
$bookid = intval($_GET['bookid']);

// Fetch book details with joins
$sql = "SELECT 
            b.BookName, b.total_count, b.current_count, 
            a.AuthorName, c.CategoryName, b.ISBNNumber, 
            b.BookPrice, b.PurchaseDate, b.QRCode, b.id,
            d.department_name AS Department, 
            e.degree_name AS Degree,
            b.Status
        FROM tblbooks b
        JOIN tblauthors a ON b.AuthorId = a.id
        JOIN tblcategory c ON b.CatId = c.id
        LEFT JOIN tbldepartment d ON d.department_id = b.department_id
        LEFT JOIN tbldegree e ON e.degree_id = b.degree_id
        WHERE b.id = :bookid";

$query = $dbh->prepare($sql);
$query->bindParam(':bookid', $bookid, PDO::PARAM_INT);
$query->execute();
$book = $query->fetch(PDO::FETCH_OBJ);
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>View Book | Library System</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" type="image/x-icon" href="<?php echo $_SESSION['logo']; ?>">

  <!-- Bootstrap + FontAwesome -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">

  <style>
    .main-content {
      margin-left: 180px;
      margin-top: 50px;
      padding: 20px;
    }
    .header-line {
      color: #2258d5;
      font-size: 22px;
      font-weight: 600;
    }
    .book-cover {
      width: 120px;
      height: 120px;
      object-fit: contain;
    }
  </style>
</head>
<body>
<?php include('includes/header.php'); ?>

<div class="main-content container">
  <div class="row mb-4">
    <div class="col-md-12 d-flex justify-content-between align-items-center">
      <h5 class="header-line"><i class="fas fa-book mr-2"></i>Book Details</h5>
      <a href="manage-books.php" class="btn btn-secondary">
        <i class="fas fa-arrow-left"></i> Back
      </a>
    </div>
  </div>

  <?php if ($book): ?>
    <div class="row justify-content-center">
      <div class="col-md-8">
        <div class="card shadow-sm">
          <div class="card-header bg-secondary text-white text-center">
            <i class="fas fa-book-open mr-2"></i>Book Information
          </div>
          <div class="card-body">
            <div class="text-center mb-4">
              <?php
              $qrWebPath = "../" . $book->QRCode;
              if (!empty($book->QRCode) && file_exists($qrWebPath)) {
                echo '<img src="' . htmlentities($qrWebPath) . '" class="book-cover" alt="QR Code">';
              } else {
                echo '<p class="text-muted">QR Code Not Available</p>';
              }
              ?>
              <h5 class="mt-3 text-primary font-weight-bold"><?= htmlentities($book->BookName); ?></h5>
            </div>

            <table class="table table-bordered table-striped">
              <tr><th>Book Name:</th><td><?= htmlentities($book->BookName); ?></td></tr>
              <tr><th>Author:</th><td><?= htmlentities($book->AuthorName); ?></td></tr>
              <tr><th>Category:</th><td><?= htmlentities($book->CategoryName); ?></td></tr>
              <tr><th>Total Count:</th><td><?= htmlentities($book->total_count); ?></td></tr>
              <tr><th>Current Count:</th><td><?= htmlentities($book->current_count); ?></td></tr>
              <tr><th>ISBN:</th><td><?= htmlentities($book->ISBNNumber); ?></td></tr>
              <tr><th>Price:</th><td>&#8377; <?= htmlentities($book->BookPrice); ?></td></tr>
              <tr><th>Purchase Date:</th><td><?= htmlentities($book->PurchaseDate); ?></td></tr>
             <tr><th>Department:</th><td><?= htmlentities($book->Department ?? 'N/A'); ?></td></tr>
<tr><th>Degree:</th><td><?= htmlentities($book->Degree ?? 'N/A'); ?></td></tr>

              <tr>
                <th>Status:</th>
                <td>
                  <?php
                    $statusText = ($book->Status == 1) ? 'Active' : 'Inactive';
                    $statusClass = ($book->Status == 1) ? 'badge-success' : 'badge-danger';
                  ?>
                  <span class="badge <?= $statusClass; ?>"><?= $statusText; ?></span>
                </td>
              </tr>
              <?php if (!empty($book->QRCode) && file_exists($qrWebPath)): ?>
                <tr>
                  <th>QR Code:</th>
                  <td>
                    <a href="<?= htmlentities($qrWebPath); ?>" target="_blank">Open QR</a>
                    <button class="btn btn-sm btn-outline-primary ml-2" onclick="printQR('<?= htmlentities($qrWebPath); ?>')">
                      <i class="fa fa-print"></i> Print QR
                    </button>
                  </td>
                </tr>
              <?php endif; ?>
            </table>
          </div>
        </div>
      </div>
    </div>
  <?php else: ?>
    <div class="alert alert-danger text-center">Book not found.</div>
  <?php endif; ?>
</div>

<?php include('includes/footer.php'); ?>

<script>
function printQR(src) {
  const win = window.open('', '_blank');
  win.document.write('<html><head><title>Print QR</title></head><body>');
  win.document.write('<img src="' + src + '" style="width:200px;height:auto;">');
  win.document.write('</body></html>');
  win.document.close();
  win.print();
}
</script>

<!-- Scripts -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
