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

if(isset($_GET['bookid'])) {
    $bookid = intval($_GET['bookid']);
    $sql = "SELECT book_no, QRCode FROM tblbookcopies WHERE book_id = :bookid";
    $query = $dbh->prepare($sql);
    $query->bindParam(':bookid', $bookid, PDO::PARAM_INT);
    $query->execute();
    $copies = $query->fetchAll(PDO::FETCH_OBJ);

    if ($query->rowCount() > 0) {
        foreach ($copies as $copy) {
            if (!empty($copy->QRCode) && file_exists("../" . $copy->QRCode)) {
                echo "<div style='margin:15px; display:inline-block; text-align:center;'>
                        <img src='../".htmlentities($copy->QRCode)."' width='120'><br>
                        <small>".htmlentities($copy->book_no)."</small>
                      </div>";
            } else {
                echo "<div style='margin:15px; display:inline-block; text-align:center;'>
                        <span>No QR</span><br>
                        <small>".htmlentities($copy->book_no)."</small>
                      </div>";
            }
        }
    } else {
        echo "<p>No copies found.</p>";
    }
}
?>
