<?php 
session_start();

if ( !isset($_SESSION["login"]) ) {
    header("location: ../../login.php");
    exit;
}

include'../../koneksi.php';

$nis = $_GET['nis'];    
$kode_kelas = $_GET['kode_kelas'];
$kode_mapel = $_GET['kode_mapel'];
$jenis_ujian = $_GET['jenis_ujian'];

$data = mysqli_query($koneksi,"SELECT * FROM mengajar
    INNER JOIN guru ON mengajar.nip = guru.nip WHERE kode_kelas='$kode_kelas' AND kode_mapel='$kode_mapel'");
$row_guru = mysqli_fetch_array($data);

$kelas = mysqli_query($koneksi,"SELECT * FROM kelas WHERE kode_kelas='$kode_kelas'");
$row_kelas = mysqli_fetch_array($kelas);

$mapel = mysqli_query($koneksi,"SELECT * FROM mapel WHERE kode_mapel='$kode_mapel'");
$row_mapel = mysqli_fetch_array($mapel);

$siswa = mysqli_query($koneksi,"SELECT * FROM siswa WHERE nis='$nis'");
$row = mysqli_fetch_array($siswa);

// Ambil nilai akhir dari tabel nilai_essay
$nilai_akhir_query = mysqli_query($koneksi,"SELECT nilai_akhir FROM nilai_essay 
    WHERE nis='$nis' 
    AND kode_kelas='$kode_kelas' 
    AND kode_mapel='$kode_mapel' 
    AND jenis_ujian='$jenis_ujian'");
$row_nilai = mysqli_fetch_array($nilai_akhir_query);
$nilai_akhir = $row_nilai['nilai_akhir'] ?? 0;
?>
<!DOCTYPE html>
<html>
<head>
    <title>Jawaban Essay</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            padding: 20px;
        }
        .info-table td {
            padding: 5px 10px;
        }
        .soal-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 10px;
        }
        .soal-table th, .soal-table td {
            border: 1px solid #ccc;
            padding: 8px 12px;
            text-align: left;
            vertical-align: top;
        }
        .soal-table th {
            background-color: #f0f0f0;
            text-align: center;
        }
        .soal-table td.no {
            text-align: center;
            width: 40px;
        }
        .hasil {
            margin-top: 15px;
        }
        .badge {
            padding: 5px 12px;
            border-radius: 5px;
            font-weight: bold;
            color: white;
            font-size: 14px;
        }
        .badge-success { background-color: #28a745; }
        .badge-danger  { background-color: #dc3545; }
        .badge-secondary { background-color: #6c757d; }

        /* Tombol cetak pojok kanan atas */
        .btn-cetak {
            position: fixed;
            top: 20px;
            right: 20px;
            padding: 10px 20px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 14px;
            cursor: pointer;
            box-shadow: 0 2px 6px rgba(0,0,0,0.3);
            z-index: 999;
        }
        .btn-cetak:hover {
            background-color: #0056b3;
        }
    </style>
    <style media="print">
        .badge { border: 1px solid #000; color: #000 !important; }
        body { padding: 0; }
        .btn-cetak { display: none; }
    </style>
</head>
<body>

    <!-- Tombol cetak pojok kanan atas -->
    <button class="btn-cetak" onclick="window.print()">🖨️ Cetak</button>

    <h3>Detail Jawaban Essay</h3>

    <table class="info-table">
        <tr>
            <td><b>Kelas</b></td>
            <td>: <?= $row_kelas['nama_kelas']; ?></td>
        </tr>
        <tr>
            <td><b>Guru Pengajar</b></td>
            <td>: <?= $row_guru['nama_guru']; ?></td>
        </tr>
        <tr>
            <td><b>Nama Siswa</b></td>
            <td>: <?= $row['nama_siswa']; ?></td>
        </tr>
        <tr>
            <td><b>Mata Pelajaran</b></td>
            <td>: <?= $row_mapel['nama_mapel']; ?></td>
        </tr>
        <tr>
            <td><b>Jenis Ujian</b></td>
            <td>: <?= $jenis_ujian; ?></td>
        </tr>
    </table>

    <hr>

    <table class="soal-table">
        <thead>
            <tr>
                <th>NO</th>
                <th>SOAL</th>
                <th>JAWABAN</th>
                <th>SKOR</th>
            </tr>
        </thead>
        <tbody>
        <?php
        $no = 1;
        $jumlah_soal = 0;

        $hasil_ujian_essay = mysqli_query($koneksi,"SELECT * FROM jawaban_essay
            INNER JOIN soal_essay ON jawaban_essay.id_soal = soal_essay.id_soal 
            WHERE jawaban_essay.nis='$nis' 
            AND jawaban_essay.kode_kelas='$kode_kelas' 
            AND jawaban_essay.kode_mapel='$kode_mapel' 
            AND jawaban_essay.jenis_ujian='$jenis_ujian'");

        while ($tampil = mysqli_fetch_array($hasil_ujian_essay)) {
        ?>
            <tr>
                <td class="no"><?= $no++; ?></td>
                <td><?= $tampil['soal']; ?></td>
                <td><?= $tampil['jawaban']; ?></td>
                <td style="text-align:center;"><?= $tampil['skor']; ?></td>
            </tr>
        <?php 
            $jumlah_soal++;
        } 
        ?>
        </tbody>
    </table>

    <br>
    <hr>

    <div class="hasil">
        <h3>Nilai Essay: <?= $nilai_akhir; ?></h3>
        <?php
        if ($jumlah_soal > 0) {
            if ($nilai_akhir >= 75) {
                echo '<span class="badge badge-success">Lulus</span>';
            } else {
                echo '<span class="badge badge-danger">Tidak Lulus</span>';
            }
        } else {
            echo '<span class="badge badge-secondary">-</span>';
        }
        ?>
    </div>

</body>
</html>