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

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

$kelas = isset($_GET['kelas']) ? trim($_GET['kelas']) : '';
$where = '';
if ($kelas !== '') {
    $kelasAman = $conn->real_escape_string($kelas);
    $where = "WHERE kelas = '$kelasAman'";
}

$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;
    }
}

$perPage = 4;
$pages = array_chunk($all, $perPage);

if (!function_exists('e')) {
    function e($value)
    {
        return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8');
    }
}
?>
<!doctype html>
<html lang="id">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Cetak Massal ID-Card Potret</title>
    <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;
            display: flex;
            justify-content: space-between;
            gap: 10px;
            flex-wrap: wrap;
            align-items: center;
        }

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

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

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

        .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;
            padding: 6mm;
            background: #fff;
            display: grid;
            grid-template-columns: repeat(2, 86mm);
            grid-auto-rows: 135mm;
            gap: 8mm 12mm;
            align-content: start;
            justify-content: center;
            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: 86mm;
            height: 135mm;
            break-inside: avoid;
            page-break-inside: avoid;
            overflow: hidden;
        }

        .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: 8mm 12mm;
                page-break-after: always;
            }
            .a4-sheet:last-child { page-break-after: auto; }
            .exam-card-potret {
                width: 86mm !important;
                height: 135mm !important;
                min-height: 135mm !important;
                border-radius: 0 !important;
                box-shadow: none !important;
                margin: 0 !important;
            }
        }
    </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 Potret</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 $chunk): ?>
            <div class="a4-sheet">
                <?php foreach ($chunk as $s): ?>
                    <div class="kartu-box">
                        <?php include 'template_kartu_potret.php'; ?>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endforeach; ?>
    <?php endif; ?>
</body>

</html>
