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

// Redirect if user not logged in
if (strlen($_SESSION['staff_login']) == 0) {
    header('location:index.php');
    exit;
}

// Validate book ID
if (!isset($_GET['bookid']) || !is_numeric($_GET['bookid'])) {
    echo "<script>alert('Invalid Book ID!'); window.location.href='books-list.php';</script>";
    exit;
}

$bookid = intval($_GET['bookid']);

// Fetch book details
$sql = "SELECT b.*, c.CategoryName, p.PublisherName,
        r.rack_no, r.shelf_no, r.floor,
        d.department_name, g.degree_name
        FROM tblbooks b
        LEFT JOIN tblcategory c ON c.id = b.CatId
        LEFT JOIN tblpublishers p ON p.id = b.Publisher
        LEFT JOIN tbl_racks r ON r.id = b.rack_id
        LEFT JOIN tbldepartment d ON d.department_id = b.department_id
        LEFT JOIN tbldegree g ON g.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);

// If book not found
if (!$book) {
    echo "<script>alert('Book not found!'); window.location.href='books-list.php';</script>";
    exit;
}

// Fetch author names from AuthorIds (comma-separated)
function getAuthorNames($dbh, $authorIds) {
    if (empty($authorIds)) return 'N/A';
    $ids = implode(',', array_map('intval', explode(',', $authorIds)));
    $stmt = $dbh->query("SELECT AuthorName FROM tblauthors WHERE id IN ($ids)");
    $names = $stmt->fetchAll(PDO::FETCH_COLUMN);
    return implode(', ', $names);
}

$authors = getAuthorNames($dbh, $book->AuthorIds);
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>View Book Details | 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']; ?>">
<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>
body { background-color: #f8f9fa; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; }
.square-card { background-color: #fff; border: 1px solid #dee2e6; box-shadow: 0 4px 12px rgba(0,0,0,0.08); border-radius: 6px; }
.main-content { margin-left: 180px; margin-top: 50px; padding: 20px; }
.header-line { color: #2258d5; font-size: 20px; font-weight: 600; margin-bottom: 20px; }
.table th { width: 35%; background-color: #f1f3f6; font-weight: 600; color: #333; }
.table td { color: #555; }
.badge-available { background-color: #28a745; padding: 6px 12px; border-radius: 5px; font-weight: 600; color: #fff; }
.badge-notavailable { background-color: #dc3545; padding: 6px 12px; border-radius: 5px; font-weight: 600; color: #fff; }
.btn-back { background-color: #6c757d; color: #fff; font-weight: 500; border-radius: 5px; padding: 6px 15px; transition: 0.3s; }
.btn-back:hover { background-color: #5a6268; }
</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 mb-4">
    <div class="col-md-12">
      <h5 class="header-line"><i class="fas fa-book-open mr-2"></i>View Book Details</h5>
    </div>
  </div>

  <div class="row justify-content-center">
    <div class="col-lg-6 col-md-8 col-sm-10 mx-auto">
      <div class="card square-card">
        <div class="card-header bg-secondary text-white d-flex justify-content-between align-items-center">
          <div><i class="fas fa-info-circle mr-2"></i> Book Information</div>
          <a href="available-books.php" class="btn btn-light btn-sm"><i class="fa fa-arrow-left"></i> Back</a>
        </div>

        <div class="card-body">
          <table class="table table-bordered table-striped">
            <tr><th>Book Name</th><td><?= htmlentities($book->BookName); ?></td></tr>
            <tr><th>Authors</th><td><?= htmlentities($authors); ?></td></tr>
            <tr><th>Category</th><td><?= htmlentities($book->CategoryName ?? 'N/A'); ?></td></tr>
            <tr><th>Department</th><td><?= htmlentities($book->department_name ?? 'N/A'); ?></td></tr>
            <tr><th>Degree</th><td><?= htmlentities($book->degree_name ?? 'N/A'); ?></td></tr>
            <tr><th>Edition</th><td><?= htmlentities($book->Edition ?? 'N/A'); ?></td></tr>
            <tr><th>Volume</th><td><?= htmlentities($book->Volume ?? 'N/A'); ?></td></tr>
            <tr><th>Publication Year</th><td><?= htmlentities($book->PublicationYear ?? 'N/A'); ?></td></tr>
            <tr><th>Publisher</th><td><?= htmlentities($book->PublisherName ?? 'N/A'); ?></td></tr>
            <tr><th>ISBN Number</th><td><?= htmlentities($book->ISBNNumber ?? 'N/A'); ?></td></tr>
            <tr><th>Available Copies</th>
              <td>
                <?php if ($book->current_count > 0): ?>
                  <span class="badge-available"><?= htmlentities($book->current_count); ?> Available</span>
                <?php else: ?>
                  <span class="badge-notavailable">Not Available</span>
                <?php endif; ?>
              </td>
            </tr>
            <tr><th>Rack Info</th>
              <td>Rack: <?= htmlentities($book->rack_no ?? 'N/A'); ?><br>
                  Shelf: <?= htmlentities($book->shelf_no ?? 'N/A'); ?><br>
                  Floor: <?= htmlentities($book->floor ?? 'N/A'); ?></td>
            </tr>
          </table>
        </div>
      </div>
    </div>
  </div>
</div>

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

<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
