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

if (strlen($_SESSION['alogin']) == 0) {
  header('location:index.php');
  exit;
}

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$sql = "SELECT c.id, c.book_id, c.book_no, c.book_status,
               b.BookName, b.ISBNNumber
        FROM tblbookcopies c
        JOIN tblbooks b ON b.id = c.book_id
        WHERE c.id = :id";
$q = $dbh->prepare($sql);
$q->bindParam(':id', $id, PDO::PARAM_INT);
$q->execute();
$copy = $q->fetch(PDO::FETCH_ASSOC);

if (!$copy) {
  $_SESSION['error'] = "Copy not found";
  header('location:manage-books.php');
  exit;
}
?>

<!DOCTYPE html>
<html lang="en">

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

  <!-- Bootstrap -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" rel="stylesheet">

  <!-- FontAwesome -->
  <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">

  <style>
    .square-card {
      width: 100%;
      background-color: #f8f9fa;
      border: 1px solid #dee2e6;
      box-shadow: 0 4px 8px rgba(0, 0, 0, 0.05);
      border-radius: 5px;
    }
    .card-header {
      font-weight: bold;
    }
    .detail-row {
      margin-bottom: 10px;
    }
    .label {
      font-weight: 600;
      color: #2258d5;
    }
  </style>
</head>

<body>

  <div class="d-flex flex-column min-vh-100">
    <?php include('includes/header.php'); ?>

    <div class="main-content flex-grow-1">
      <div class="row pad-botm mb-4">
        <div class="col-md-12">
          <h5 class="header-line">COPY DETAILS</h5>
        </div>
      </div>

      <div class="container">
        <div class="card square-card mb-4">
          <div class="card-header bg-secondary text-white">
            <i class="fa fa-book-open mr-2"></i> <?= htmlentities($copy['BookName']) ?> (ISBN: <?= htmlentities($copy['ISBNNumber']) ?>)
          </div>
          <div class="card-body">
            <div class="detail-row">
              <span class="label">Book No:</span>
              <?= htmlentities($copy['book_no']) ?>
            </div>
            <div class="detail-row">
              <span class="label">Book Status:</span>
              <?php
              $statusClass = match ($copy['book_status']) {
                'available' => 'success',
                'issued'    => 'warning',
                'damaged'   => 'danger',
                'lost'      => 'dark',
                default     => 'secondary',
              };
              ?>
              <span class="badge badge-<?= $statusClass ?>"><?= ucfirst($copy['book_status']) ?></span>
            </div>
          </div>
          <div class="card-footer">
            <a href="book-copies.php?bookid=<?= (int)$copy['book_id'] ?>" class="btn btn-sm btn-secondary">
              <i class="fa fa-arrow-left"></i> Back to Copies
            </a>
            <a href="copy-edit.php?id=<?= (int)$copy['id'] ?>" class="btn btn-sm btn-primary">
              <i class="fa fa-edit"></i> Edit Copy
            </a>
          </div>
        </div>
      </div>
    </div>

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

  <!-- jQuery -->
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

  <!-- Bootstrap JS -->
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>

</body>
</html>
