<?php
session_start();
if (empty($_SESSION['login']) || ($_SESSION['role'] ?? '') !== 'siswa' || empty($_SESSION['siswa_id'])) {
    session_unset();
    session_destroy();
    header('Location: ../index.php');
    exit;
}
require_once '../config/koneksi.php';

if (!function_exists('e')) {
    function e($value) { return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8'); }
}

$id = (int) $_SESSION['siswa_id'];
$stmt = $conn->prepare('SELECT * FROM siswa WHERE id=? LIMIT 1');
$stmt->bind_param('i', $id);
$stmt->execute();
$siswa = $stmt->get_result()->fetch_assoc();

$profil = $conn->query('SELECT * FROM profil_sekolah LIMIT 1')->fetch_assoc();
if (!$siswa) {
    session_unset();
    session_destroy();
    header('Location: ../index.php?error=siswa');
    exit;
}

$status = strtoupper(trim($siswa['status_lulus'] ?? '')) ?: 'BELUM DIUMUMKAN';
$is_lulus = $status === 'LULUS';
$tanggal_skl = !empty($siswa['tanggal_skl']) ? date('d-m-Y', strtotime($siswa['tanggal_skl'])) : '-';
?>
<!doctype html>
<html lang="id">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Status Kelulusan</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css">
    <style>
        body { background:#f1f5f9; min-height:100vh; display:flex; align-items:center; justify-content:center; padding:20px; }
        .card-status { max-width:720px; width:100%; border:0; border-radius:22px; overflow:hidden; }
        .kop { background:#0f172a; color:white; padding:22px; text-align:center; }
        .logo { width:72px; height:72px; object-fit:contain; background:white; border-radius:50%; padding:6px; margin-bottom:10px; }
        .status-box { border-radius:18px; padding:22px; }
        .status-lulus { background:#dcfce7; color:#166534; }
        .status-tidak { background:#fee2e2; color:#991b1b; }
        .status-belum { background:#e2e8f0; color:#334155; }
    </style>
</head>
<body>
    <div class="card card-status shadow-lg">
        <div class="kop">
            <img class="logo" src="../assets/uploads/<?= e($profil['logo'] ?? 'logo.png') ?>" alt="Logo Sekolah">
            <h4 class="fw-bold mb-1"><?= e($profil['nama_sekolah'] ?? 'Sekolah') ?></h4>
            <div class="small"><?= e($profil['alamat'] ?? '') ?></div>
        </div>
        <div class="card-body p-4 p-md-5">
            <div class="text-center mb-4">
                <h3 class="fw-bold">Keterangan Kelulusan</h3>
                <p class="text-muted mb-0">Tahun Pelajaran <?= e($siswa['tahun_pelajaran'] ?: ($profil['tahun_pelajaran'] ?? '')) ?></p>
            </div>

            <table class="table table-borderless mb-4">
                <tr><td width="170">No Peserta</td><td>: <b><?= e($siswa['no_ujian']) ?></b></td></tr>
                <tr><td>Nama</td><td>: <b><?= e($siswa['nama']) ?></b></td></tr>
                <tr><td>NISN</td><td>: <?= e($siswa['nisn']) ?></td></tr>
                <tr><td>Kelas</td><td>: <?= e($siswa['kelas']) ?></td></tr>
                <tr><td>Nomor SKL</td><td>: <?= e($siswa['nomor_skl'] ?: '-') ?></td></tr>
                <tr><td>Tanggal SKL</td><td>: <?= e($tanggal_skl) ?></td></tr>
            </table>

            <?php $kelas_status = $status === 'BELUM DIUMUMKAN' ? 'status-belum' : ($is_lulus ? 'status-lulus' : 'status-tidak'); ?>
            <div class="status-box text-center <?= $kelas_status ?>">
                <div class="small text-uppercase fw-semibold">Status</div>
                <div class="display-6 fw-bold"><?= e($status) ?></div>
                <?php if (!empty($siswa['keterangan_lulus'])): ?>
                    <p class="mb-0 mt-2"><?= e($siswa['keterangan_lulus']) ?></p>
                <?php endif; ?>
            </div>

            <div class="d-flex gap-2 justify-content-center mt-4">
                <?php if ($status !== 'BELUM DIUMUMKAN'): ?>
                    <a class="btn btn-primary" target="_blank" href="../cetak/skl.php?id=<?= (int)$siswa['id'] ?>"><i class="fa-solid fa-print"></i> Cetak SKL</a>
                <?php endif; ?>
                <a class="btn btn-outline-danger" href="../auth/logout.php"><i class="fa-solid fa-right-from-bracket"></i> Logout</a>
            </div>
        </div>
    </div>
</body>
</html>
