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

// always define variables
$result = null;
$copies = [];

if(isset($_GET['bookid'])) {
    $bookid = intval($_GET['bookid']); // ensure integer

    // Fetch book master details
    $sql = "SELECT b.BookName, 
                   COALESCE(c.CategoryName, 'N/A') AS CategoryName, 
                   COALESCE(a.AuthorName, 'N/A') AS AuthorName, 
                   b.ISBNNumber, b.BookPrice, b.PurchaseDate, 
                   b.Edition, b.Volume, b.Publisher, b.PublicationYear, 
                   b.QRCode
            FROM tblbooks b
            LEFT JOIN tblcategory c ON c.id = b.CatId 
            LEFT JOIN tblauthors a ON a.id = b.AuthorId
            WHERE b.id = :bookid";
    $query = $dbh->prepare($sql);
    $query->bindParam(':bookid', $bookid, PDO::PARAM_INT);
    $query->execute();
    $result = $query->fetch(PDO::FETCH_ASSOC);

    // Fetch copies of the book
    $sqlCopies = "SELECT id, book_no, status, QRCode 
                  FROM tblbookcopies 
                  WHERE book_id = :bookid";
    $stmt = $dbh->prepare($sqlCopies);
    $stmt->bindParam(':bookid', $bookid, PDO::PARAM_INT);
    $stmt->execute();
    $copies = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Book Details</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

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

    <style>
        body { background: #f2f6fc; font-family: 'Segoe UI', sans-serif; }
        .card { border-radius: 16px; box-shadow: 0 8px 20px rgba(0,0,0,0.1); }
        .card-header { background: linear-gradient(135deg, #4598cc, #00c6ff); color: white; border-radius: 16px 16px 0 0; }
        .book-title { font-size: 1.8rem; font-weight: 600; color: #343a40; margin-bottom: 1rem; text-align: center; }
        .copy-available { color: green; font-weight: bold; }
        .copy-issued { color: red; font-weight: bold; }
        .qrcode-img { width: 60px; height: 60px; }
    </style>
</head>
<body>
<div class="container py-5">
    <div class="row justify-content-center">
        <div class="col-md-10">
            <div class="card text-center">
                <div class="card-header">
                    <h2>📘 Book Details</h2>
                </div>
                <div class="card-body">
                    <?php if($result): ?>
                        <div class="book-title"><?php echo htmlentities($result['BookName']); ?></div>
                        
                        <!-- <?php if(!empty($result['QRCode'])): ?>
                            <img src="<?php echo htmlentities($result['QRCode']); ?>" class="qrcode-img mb-3">
                        <?php endif; ?> -->

                        <table class="table table-borderless w-75 mx-auto text-start">
                            <tr><th>Category :</th><td><?php echo htmlentities($result['CategoryName']); ?></td></tr>
                            <tr><th>Author :</th><td><?php echo htmlentities($result['AuthorName']); ?></td></tr>
                            <tr><th>ISBN :</th><td><?php echo htmlentities($result['ISBNNumber']); ?></td></tr>
                            <tr><th>Price :</th><td>₹<?php echo htmlentities($result['BookPrice']); ?></td></tr>
                            <tr><th>Purchase Date :</th><td><?php echo htmlentities($result['PurchaseDate']); ?></td></tr>
                            <tr><th>Edition :</th><td><?php echo htmlentities($result['Edition']); ?></td></tr>
                            <tr><th>Volume :</th><td><?php echo htmlentities($result['Volume']); ?></td></tr>
                            <tr><th>Publisher :</th><td><?php echo htmlentities($result['Publisher']); ?></td></tr>
                            <tr><th>Publication Year :</th><td><?php echo htmlentities($result['PublicationYear']); ?></td></tr>
                        </table>

                        <hr>
                        <h4>📖 Copies</h4>
                        <table class="table table-striped table-bordered mt-3">
                            <thead class="table-light">
                                <tr>
                                    <th>#</th>
                                    <th>Book No</th>
                                    <th>Status</th>
                                    <th>QR Code</th>
                                </tr>
                            </thead>
                            <tbody>
                            <?php if($copies): ?>
                                <?php foreach($copies as $i => $copy): ?>
                                    <tr>
                                        <td><?php echo $i+1; ?></td>
                                        <td><?php echo htmlentities($copy['book_no']); ?></td>
                                        <td>
                                            <?php if($copy['status'] == 1): ?>
                                                <span class="copy-available">Available</span>
                                            <?php else: ?>
                                                <span class="copy-issued">Issued</span>
                                            <?php endif; ?>
                                        </td>
                                     <td>
  <?php if(!empty($copy['QRCode'])): ?>
    <img src="../<?php echo $copy['QRCode']; ?>" class="qrcode-img" width="100">
  <?php else: ?>
    N/A
  <?php endif; ?>
</td>

                                    </tr>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <tr><td colspan="4">No copies found</td></tr>
                            <?php endif; ?>
                            </tbody>
                        </table>
                    <?php else: ?>
                        <div class="alert alert-warning">❌ Book not found or invalid ID!</div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
