<?php
require_once '../config/koneksi.php';

// Ambil profil sekolah
$qProfil = $conn->query("SELECT * FROM profil_sekolah LIMIT 1");
$p = $qProfil ? $qProfil->fetch_assoc() : [];

// Ambil filter kelas dari URL, contoh: kartu_massal.php?kelas=6%20(ENAM)
$kelas = isset($_GET['kelas']) ? trim($_GET['kelas']) : '';

$where = '';
if ($kelas !== '') {
    $kelasAman = $conn->real_escape_string($kelas);
    $where = "WHERE kelas = '$kelasAman'";
}

// Ambil semua data siswa sesuai filter kelas
$sql = "SELECT * FROM siswa $where ORDER BY kelas ASC, nama ASC";
$res = $conn->query($sql);

$all = [];
if ($res) {
    while ($row = $res->fetch_assoc()) {
        $all[] = $row;
    }
}

// Pecah data: 8 kartu per halaman A4
$perPage = 6;
$pages = array_chunk($all, $perPage);
?>
<!doctype html>
<html lang="id">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Cetak Massal Kartu Ujian</title>
    <link rel="stylesheet" href="../assets/css/style.css">

    <style>
        @page {
            size: A4 portrait;
            margin: 8mm;
        }

        * {
            box-sizing: border-box;
        }

        body {
            margin: 0;
            padding: 10px;
            background: #e5e7eb;
            font-family: Arial, sans-serif;
        }

        .print-toolbar {
            max-width: 210mm;
            margin: 0 auto 16px auto;
            display: flex;
            gap: 10px;
            align-items: center;
            justify-content: space-between;
            flex-wrap: wrap;
        }

        .toolbar-left,
        .toolbar-right {
            display: flex;
            gap: 10px;
            align-items: center;
            flex-wrap: wrap;
        }

        .btn-gray,
        .btn-green,
        .btn-blue {
            display: inline-block;
            padding: 10px 14px;
            border: 0;
            border-radius: 8px;
            text-decoration: none;
            color: #fff;
            cursor: pointer;
            font-size: 14px;
            line-height: 1;
        }

        .btn-gray {
            background: #334155;
        }

        .btn-green {
            background: #16a34a;
        }

        .btn-blue {
            background: #2563eb;
        }

        .info-cetak {
            color: #334155;
            font-size: 14px;
            background: #fff;
            border-radius: 8px;
            padding: 10px 12px;
        }

        .a4-sheet {
            width: 210mm;
            min-height: 297mm;
            margin: 0 auto 20px auto;
            padding: 8mm;
            background: #fff;
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            grid-auto-rows: auto;
            gap: 8mm 6mm;
            align-content: start;
            box-shadow: 0 10px 30px rgba(15, 23, 42, .18);
            page-break-after: always;
        }

        .a4-sheet:last-child {
            page-break-after: auto;
        }

        .kartu-box {
            width: 100%;
            break-inside: avoid;
            page-break-inside: avoid;
        }

        .empty-message {
            max-width: 210mm;
            margin: 30px auto;
            background: #fff;
            padding: 20px;
            border-radius: 10px;
            color: #991b1b;
            font-weight: bold;
        }

        @media print {

            html,
            body {
                margin: 0 !important;
                padding: 0 !important;
                background: #fff !important;
                -webkit-print-color-adjust: exact;
                print-color-adjust: exact;
            }

            .no-print {
                display: none !important;
            }

            .a4-sheet {
                width: auto;
                min-height: auto;
                margin: 0;
                padding: 0;
                box-shadow: none;
                gap: 6mm 5mm;
                page-break-after: always;
            }

            .a4-sheet:last-child {
                page-break-after: auto;
            }

            .kartu-box,
            .mini-card {
                break-inside: avoid;
                page-break-inside: avoid;
            }
        }
    </style>
</head>

<body>

    <div class="print-toolbar no-print">
        <div class="toolbar-left">
            <a class="btn-gray" href="../admin/cetak_massal.php">← Kembali</a>
            <button class="btn-green" type="button" onclick="window.print()">🖨 Cetak / Simpan PDF</button>
        </div>

        <div class="toolbar-right">
            <div class="info-cetak">
                Total siswa: <?= count($all) ?>
                <?php if ($kelas !== ''): ?>
                    | Kelas: <?= e($kelas) ?>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <?php if (empty($all)): ?>
        <div class="empty-message no-print">
            Data siswa tidak ditemukan. Silakan cek data siswa atau filter kelas.
        </div>
    <?php else: ?>

        <?php foreach ($pages as $pageIndex => $chunk): ?>
            <div class="a4-sheet">
                <?php foreach ($chunk as $i => $s): ?>
                    <?php
                    // Nomor urut global, dibutuhkan oleh template_kartu_mini.php
                    $num = ($pageIndex * 8) + $i + 1;
                    ?>

                    <div class="kartu-box">
                        <?php include 'template_kartu_mini.php'; ?>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endforeach; ?>

    <?php endif; ?>

</body>

</html>