<?php
// Start session only if not already started
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Include header and database connection
include 'header_siswa.php';
include 'koneksi.php';

// Check if user is logged in and is a 'siswa'
if (!isset($_SESSION['email']) || $_SESSION['role'] != 'siswa') {
    header("Location: login.php");
    exit();
}

// Retrieve selected semester if set
$selected_semester = isset($_GET['semester']) ? $_GET['semester'] : '';

$data = null;
$jumlah_data = 0;

// Only run the query if a semester is selected
if ($selected_semester) {
    $query = "SELECT * FROM prestasi WHERE semester = '$selected_semester'";
    $data = mysqli_query($koneksi, $query);
    
    // Debugging - check if query has any issues
    if (!$data) {
        echo "Query Error: " . mysqli_error($koneksi);
        exit;
    }

    $jumlah_data = mysqli_num_rows($data);

    // Debugging - Uncomment to check if data is retrieved
    echo "<p>Jumlah data ditemukan: $jumlah_data</p>";
}
?>

<div class="container mt-5">
    <h2 class="text-center mb-5" style="font-size: 2.5em;">Prestasi Siswa</h2>

    <!-- Semester selection dropdown -->
    <form method="GET" action="" class="mb-4">
        <div class="row justify-content-center">
            <div class="col-md-4">
                <select name="semester" class="form-control" onchange="this.form.submit()">
                    <option value="">Pilih Semester</option>
                    <option value="ganjil" <?php if ($selected_semester == 'ganjil') echo 'selected'; ?>>Ganjil</option>
                    <option value="genap" <?php if ($selected_semester == 'genap') echo 'selected'; ?>>Genap</option>
                </select>
            </div>
        </div>
    </form>

    <div class="row justify-content-center">
        <?php if ($selected_semester && $jumlah_data > 0): ?>
            <?php while ($d = mysqli_fetch_array($data)) { ?>
                <div class="col-md-6 col-lg-4 mb-5">
                    <div class="card shadow-sm" style="padding: 20px; border-radius: 15px;">
                        <div class="card-body" style="font-size: 1.2em;">
                            <h5 class="card-title" style="font-size: 1.5em;"><?php echo $d['jenis_prestasi']; ?></h5>
                            <p class="card-text">
                                <strong>Nama Siswa:</strong> <?php echo $d['tingkat']; ?><br>
                                <strong>Alamat:</strong> <?php echo $d['tanggal']; ?><br>
                                <strong>Handphone:</strong> <?php echo $d['keterangan']; ?>
                            </p>
                        </div>
                    </div>
                </div>
            <?php } ?>
        <?php elseif ($selected_semester): ?>
            <div class="col-12">
                <div class="alert alert-info text-center" style="font-size: 1.5em; padding: 20px;" role="alert">
                    Belum Mendapatkan Prestasi Belajar Yang Giat ya?
                </div>
            </div>
        <?php endif; ?>
    </div>
</div>
