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

$id = isset($_GET['id']) ? (int) $_GET['id'] : 0;

if ($id <= 0) {
    die('ID siswa tidak valid');
}

$qSiswa = $conn->query("SELECT * FROM siswa WHERE id = $id LIMIT 1");

if (!$qSiswa || $qSiswa->num_rows == 0) {
    die('Data siswa tidak ditemukan');
}

$s = $qSiswa->fetch_assoc();

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

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 ID-Card Potret</title>
    <style>
        @page {
            size: 86mm 135mm;
            margin: 0;
        }

        * { box-sizing: border-box; }

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

        .no-print {
            margin: 0 auto 18px;
            max-width: 420px;
            display: flex;
            gap: 8px;
            flex-wrap: wrap;
        }

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

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

        .single-card-page {
            display: flex;
            justify-content: center;
            align-items: flex-start;
        }

        @media print {
            .no-print { display: none !important; }
            html, body {
                margin: 0 !important;
                padding: 0 !important;
                background: #fff !important;
                -webkit-print-color-adjust: exact;
                print-color-adjust: exact;
            }
            .single-card-page { margin: 0; }
            .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="no-print">
        <a href="../admin/siswa.php" class="btn btn-gray">← Kembali</a>
        <button onclick="window.print()" class="btn btn-green">🖨 Cetak Potret</button>
    </div>

    <div class="single-card-page">
        <?php include 'template_kartu_potret.php'; ?>
    </div>
</body>

</html>
