<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Class Prep Sheet</title>
    <link rel="icon" href="<?= base_url('assets/images/favicon.ico') ?>">
    <style>
        body {
            font-family: Arial, sans-serif;
            padding: 5cm;
        }

        .prep-label {
            border: 2px solid #000;
            padding: 2rem;
            width: 400px;
            margin: auto;
            text-align: center;
        }

        .prep-label h2 {
            font-size: 5rem;
            margin-bottom: 1rem;
        }

        .prep-label ul {
            list-style: none;
            padding-left: 0;
            font-size: 3rem;
        }

        @media print {
            body {
                margin: 0;
                padding: 0;
            }

            .prep-label {
                border: none;
                page-break-after: always;
            }
        }
    </style>
</head>

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

    <?php
    // prefer friendly name if provided by controller
    $sectionTitle = isset($classSection) && $classSection ? $classSection : ($classSectionId ?? 'Class');

    // make sure we have an array
    $prepItems = is_array($prepItems ?? null) ? $prepItems : [];

    // optional: if you want a specific order, define it here.
    // otherwise we’ll just print in the array’s order.
    $preferredOrder = [
        'Grade Box',
        'Large Table',
        'Regular Chair',
        'Small Chair',
        'Small Table',
        'Teacher Chair',
        'Trash Bin',
        'White Board',
    ];

    // reorder $prepItems by $preferredOrder when present
    $ordered = [];
    foreach ($preferredOrder as $k) {
        if (array_key_exists($k, $prepItems)) {
            $ordered[$k] = $prepItems[$k];
        }
    }
    // include any remaining keys (if any)
    foreach ($prepItems as $k => $v) {
        if (!array_key_exists($k, $ordered)) {
            $ordered[$k] = $v;
        }
    }
    ?>

    <div class="prep-label">
        <h2>Grade <?= esc($sectionTitle) ?></h2>
        <ul>
            <?php
            $printed = 0;
            foreach ($ordered as $name => $qty):
                $qty = (int)$qty;
                if ($qty > 0):
                    $printed++;
            ?>
                    <li><strong><?= $qty ?></strong> <?= esc($name) ?></li>
                <?php
                endif;
            endforeach;

            if ($printed === 0): ?>
                <li>No items needed</li>
            <?php endif; ?>
        </ul>
    </div>

</body>

</html>