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

$id_guru_aktif = $_SESSION['id_guru'] ?? 0;
$role = $_SESSION['role'] ?? 'guru';

// 1. Ambil Periode Aktif (Semester & TP)
$q_aktif = mysqli_query($koneksi, "SELECT s.id_semester, s.nama_semester, t.tahun_pelajaran 
                                   FROM semester s, tahun_pelajaran t 
                                   WHERE s.status = 'Aktif' AND t.status = 'Aktif' LIMIT 1");
$aktif = mysqli_fetch_array($q_aktif);
$id_sem_aktif = $aktif['id_semester'] ?? 0;
$nama_sem     = $aktif['nama_semester'] ?? '-';
$nama_tp      = $aktif['tahun_pelajaran'] ?? '-';

include 'layout.php';

$id_rombel_pilih = $_GET['id_rombel'] ?? '';
$id_ekskul_pilih = $_GET['id_ekskul'] ?? '';
?>

<div class="container-fluid">
    <h3>Input Nilai Ekstrakurikuler</h3>
    
    <div class="alert alert-secondary shadow-sm">
        <i class="fa fa-calendar"></i> Periode: <strong><?= htmlspecialchars($nama_tp) ?></strong> | 
        <i class="fa fa-book"></i> Semester: <strong><?= htmlspecialchars($nama_sem) ?></strong>
    </div>

    <form method="GET" class="card p-3 mb-4 shadow-sm">
        <div class="row">
            <div class="col-md-3">
                <label class="fw-bold">Pilih Kelas:</label>
                <select name="id_rombel" class="form-select" onchange="this.form.submit()">
                    <option value="">-- Pilih Kelas --</option>
                    <?php
                    $qr = mysqli_query($koneksi, "SELECT id_rombel, kelas FROM rombel ORDER BY kelas ASC");
                    while($r = mysqli_fetch_array($qr)) {
                        $sel = ($id_rombel_pilih == $r['id_rombel']) ? 'selected' : '';
                        echo "<option value='{$r['id_rombel']}' $sel>{$r['kelas']}</option>";
                    }
                    ?>
                </select>
            </div>

            <div class="col-md-4">
                <label class="fw-bold">Pilih Ekstrakurikuler:</label>
                <select name="id_ekskul" class="form-select" onchange="this.form.submit()">
                    <option value="">-- Pilih Ekskul --</option>
                    <?php
                    $qe = mysqli_query($koneksi, "SELECT id_ekskul, nama_ekskul FROM ekstrakurikuler");
                    while($e = mysqli_fetch_array($qe)) {
                        $sel = ($id_ekskul_pilih == $e['id_ekskul']) ? 'selected' : '';
                        echo "<option value='{$e['id_ekskul']}' $sel>{$e['nama_ekskul']}</option>";
                    }
                    ?>
                </select>
            </div>
        </div>
    </form>

    <?php
    if(!empty($id_rombel_pilih) && !empty($id_ekskul_pilih)) {
        // Mengambil daftar siswa
        $q_siswa = mysqli_query($koneksi, "SELECT s.id_siswa, s.nama FROM master_siswa s 
                                          JOIN anggota_rombel ar ON s.id_siswa = ar.id_siswa 
                                          WHERE ar.id_rombel = '$id_rombel_pilih' AND ar.id_semester = '$id_sem_aktif'");

        $q_nilai = mysqli_query($koneksi, "SELECT * FROM nilai_ekskul 
                                           WHERE id_ekskul = '$id_ekskul_pilih' 
                                           AND id_semester = '$id_sem_aktif'");
        $data_nilai = [];
        while($row = mysqli_fetch_assoc($q_nilai)) { $data_nilai[$row['id_siswa']] = $row; }

        echo "<div class='card p-4 shadow-sm table-responsive'>
              <form method='POST' action='proses_simpan_ekskul.php'>
              <input type='hidden' name='id_ekskul' value='$id_ekskul_pilih'>
              <table class='table table-bordered table-hover'>
                <thead class='table-dark'>
                    <tr><th>No</th><th>Nama Siswa</th><th>Nilai</th><th>Predikat</th><th>Deskripsi</th></tr>
                </thead><tbody>";
        
        $no = 1;
        while($s = mysqli_fetch_array($q_siswa)) {
            $id_s = $s['id_siswa'];
            $n = $data_nilai[$id_s] ?? ['nilai'=>'', 'predikat'=>'', 'deskripsi'=>''];
            echo "<tr>
                    <td>{$no}</td>
                    <td>".htmlspecialchars($s['nama'])."</td>
                    <td><input type='number' name='nilai[{$id_s}]' value='{$n['nilai']}' class='form-control form-control-sm' style='width:70px'></td>
                    <td>
                        <select name='predikat[{$id_s}]' class='form-select form-select-sm'>
                            <option value='' ".($n['predikat']==''?'selected':'').">-</option>
                            <option value='A' ".($n['predikat']=='A'?'selected':'').">A</option>
                            <option value='B' ".($n['predikat']=='B'?'selected':'').">B</option>
                            <option value='C' ".($n['predikat']=='C'?'selected':'').">C</option>
                        </select>
                    </td>
                    <td><input type='text' name='deskripsi[{$id_s}]' value='{$n['deskripsi']}' class='form-control form-control-sm'></td>
                  </tr>";
            $no++;
        }
        echo "</tbody></table><button type='submit' class='btn btn-success mt-3'>Simpan Nilai Ekskul</button></form></div>";
    } elseif (!empty($id_rombel_pilih) && empty($id_ekskul_pilih)) {
        echo "<div class='alert alert-warning'>Silakan pilih Ekstrakurikuler terlebih dahulu.</div>";
    }
    ?>
</div>

<?php include 'layout_footer.php'; ?>