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

if (!isset($_SESSION['last_book_id'])) {
    header('location:add-book.php');
    exit;
}

$bookId = $_SESSION['last_book_id'];
unset($_SESSION['last_qr_path'], $_SESSION['last_book_copies'], $_SESSION['last_book_id']);

// Fetch book info
$sqlBook = "SELECT BookName FROM tblbooks WHERE id = :id";
$queryBook = $dbh->prepare($sqlBook);
$queryBook->bindParam(':id', $bookId, PDO::PARAM_INT);
$queryBook->execute();
$book = $queryBook->fetch(PDO::FETCH_ASSOC);

// Fetch all copies
$sqlCopies = "SELECT book_no, QRCode FROM tblbookcopies WHERE book_id = :book_id ORDER BY id ASC";
$queryCopies = $dbh->prepare($sqlCopies);
$queryCopies->bindParam(':book_id', $bookId, PDO::PARAM_INT);
$queryCopies->execute();
$copies = $queryCopies->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html>
<head>
    <title>Book Added Successfully</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f6f9;
        }
        .success-box {
            text-align: center;
            margin-top: 40px;
        }
        .copies-container {
            display: flex;
            flex-wrap: wrap;
            justify-content: center;
            margin-top: 20px;
        }
        .copy-card {
            background: #fff;
            border: 1px solid #ddd;
            border-radius: 8px;
            margin: 10px;
            padding: 15px;
            width: 200px;
            text-align: center;
            box-shadow: 0px 2px 6px rgba(0,0,0,0.1);
        }
        .qr-img {
            width: 150px;
            height: auto;
            margin-bottom: 10px;
        }
        .btn {
            padding: 10px 20px;
            background-color: #2cabe3;
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            text-decoration: none;
            margin: 5px;
        }
        .btn:hover {
            background-color: #1b8cc2;
        }
    </style>
</head>
<body>

<div class="success-box">
    <h2>✅ Book Added Successfully</h2>
    <p><strong>Book ID:</strong> <?php echo $bookId; ?></p>
    <p><strong>Book Title:</strong> <?php echo htmlspecialchars($book['BookName']); ?></p>

    <div class="copies-container">
        <?php foreach ($copies as $copy): ?>
            <div class="copy-card">
                <p><strong><?php echo $copy['book_no']; ?></strong></p>
                <img src="../<?php echo $copy['QRCode']; ?>" alt="QR Code" class="qr-img">
            </div>
        <?php endforeach; ?>
    </div>

    <br>
    <button class="btn" onclick="printAllQRCodes()">🖨️ Print All QR Codes</button>
    <br><br>
    <a href="add-book.php" class="btn">➕ Add Another Book</a>
    <a href="manage-books.php" class="btn">📚 Manage Books</a>
</div>

<script>
function printAllQRCodes() {
    var content = document.querySelector('.copies-container').innerHTML;
    var printWindow = window.open('', '_blank');
    printWindow.document.write(`
        <html><head><title>Print QR Codes</title></head>
        <body style="text-align:center; font-family:Arial;">
            <h2>Book Copies QR Codes</h2>
            ${content}
            <script>
                window.onload = function() {
                    window.print();
                    window.onafterprint = function() { window.close(); };
                };
            <\/script>
        </body></html>
    `);
    printWindow.document.close();
}
</script>

</body>
</html>
