<?php
session_start();
require_once '../koneksi.php';
require_once '../auth.php';
cekGuru();

$id_guru = $_SESSION['id_guru'];
$id_ekskul_terpilih = $_GET['id_ekskul'] ?? null;

// Proses Simpan Nilai
if (isset($_POST['simpan'])) {
    $id_siswa = $_POST['id_siswa'];
    $id_ekskul = $_POST['id_ekskul'];
    $predikat = $_POST['predikat'];
    $deskripsi = $_POST['deskripsi'];

    // Kueri INSERT
    $sql = "INSERT INTO nilai_ekskul (id_siswa, id_ekskul, predikat, deskripsi) 
            VALUES ('$id_siswa', '$id_ekskul', '$predikat', '$deskripsi')";
            
    if (mysqli_query($koneksi, $sql)) {
        echo "Data berhasil disimpan!";
    } else {
        echo "Error: " . mysqli_error($koneksi); // Penting untuk melihat pesan error database
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Input Nilai Ekskul - SIAKAD</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .sidebar { width: 250px; background-color: #002147; min-height: 100vh; color: white; position: fixed; }
        .nav-link { color: #ffffff !important; padding: 15px 20px; }
        .main-content { margin-left: 250px; padding: 20px; }
    </style>
</head>
<body>

<div class="sidebar">
    <h4>SIAKAD GURU</h4>
    <hr>
    <ul class="nav flex-column">
        <li class="nav-item"><a class="nav-link" href="dashboard.php">Dashboard</a></li>
        <li class="nav-item"><a class="nav-link active" href="input_nilai_ekskul.php">Input Ekskul</a></li>
    </ul>
</div>

<div class="main-content">
    <h3>Input Anggota & Nilai Ekskul</h3>
    
    <form method="GET" class="mb-4">
        <select name="id_ekskul" class="form-control w-50" onchange="this.form.submit()">
            <option value="">-- Pilih Ekskul Anda --</option>
            <?php
            $ekskul = mysqli_query($koneksi, "SELECT * FROM ekstrakurikuler WHERE id_guru = '$id_guru'");
            while($e = mysqli_fetch_array($ekskul)) {
                $sel = ($id_ekskul_terpilih == $e['id_ekskul']) ? 'selected' : '';
                echo "<option value='".$e['id_ekskul']."' $sel>".$e['nama_ekskul']."</option>";
            }
            ?>
        </select>
    </form>

    <?php if ($id_ekskul_terpilih): ?>
    <form method="POST" action="input_detail_ekskul.php">
    <input type="hidden" name="id_siswa" value="23">
    <input type="hidden" name="id_ekskul" value="1">
    
    <select name="predikat">
        <option value="A">A</option>
        <option value="B">B</option>
    </select>
    
    <textarea name="deskripsi"></textarea>
    
    <button type="submit" name="simpan">Simpan</button>
</form>
    <?php endif; ?>
</div>
</body>
</html>