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

$bookid = isset($_GET['bookid']) ? (int) $_GET['bookid'] : 0;
$startRow = isset($_GET['startRow']) ? (int) $_GET['startRow'] : 1;
$startCol = isset($_GET['startCol']) ? (int) $_GET['startCol'] : 1;

if ($bookid <= 0) {
    die("Invalid book id");
}

// Fetch all copies with QR
$sql = "SELECT c.book_no, c.QRCode, b.BookName, b.ISBNNumber, c.accession_no
        FROM tblbookcopies c
        JOIN tblbooks b ON b.id = c.book_id
        WHERE c.book_id = :bookid
        ORDER BY c.id ASC";
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':bookid', $bookid, PDO::PARAM_INT);
$stmt->execute();
$copies = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Grid config
$colsPerRow = 3;
$rowsPerPage = 8; // 8 rows per A4 page
$slotsPerPage = $colsPerRow * $rowsPerPage;

// Calculate starting offset
$startOffset = ($startRow - 1) * $colsPerRow + ($startCol - 1);

// Total number of copies
$totalCopies = count($copies);

// Initialize counter
$printed = 0;
?>
<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>Print All QR - <?= htmlentities($copies[0]['BookName'] ?? '') ?></title>
    <style>
        @page {
            size: A4;
            margin: 8mm;
        }

        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
        }

        .sheet {
            display: grid;
            grid-template-columns: repeat(<?= $colsPerRow ?>, 1fr);
            grid-auto-rows: 1fr;
            column-gap: 12px;
            row-gap: 10px;
            padding: 10px;
            box-sizing: border-box;
            page-break-after: always;
        }

        .label {
            border: 1px dashed #ccc;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 8px;
            font-size: 15px;
        }

        .label-text {
            text-align: left;
            max-width: 55%;
        }

        .label img {
            width: 90px;
            height: 90px;
        }

        .label strong {
            display: block;
        }
    </style>
</head>

<body onload="window.print()">

    <?php
    while ($printed < $totalCopies) {
        echo '<div class="sheet">';

        // Add empty labels if first page
        if ($printed === 0) {
            for ($i = 0; $i < $startOffset; $i++) {
                echo '<div class="label"></div>';
            }
            $slotsFilled = $startOffset;
        } else {
            $slotsFilled = 0;
        }

        // Fill slots for this page
        while ($slotsFilled < $slotsPerPage && $printed < $totalCopies) {
            $copy = $copies[$printed];
            ?>
            <div class="label">
                <div class="label-text">
                    <strong>ACC No:</strong> <?= htmlentities($copy['accession_no'] ?? '') ?><br>
                    <strong>Book No:</strong> <?= htmlentities($copy['book_no']) ?>
                </div>
                <?php if (!empty($copy['QRCode'])): ?>
                    <img src="../<?= htmlentities($copy['QRCode']) ?>" alt="QR Code">
                <?php else: ?>
                    <span>No QR</span>
                <?php endif; ?>
            </div>
            <?php
            $printed++;
            $slotsFilled++;
        }

        echo '</div>'; // end sheet
    }
    ?>

</body>

</html>