<?php
session_start();
require_once('../koneksi.php');
$id_guru = $_SESSION['id_guru'];

$q_wali = mysqli_query($koneksi, "SELECT id_rombel FROM rombel WHERE wali_id = '$id_guru'");
$wali = mysqli_fetch_array($q_wali);
$id_rombel = $wali['id_rombel'];

// Proses Simpan Rekap
if(isset($_POST['simpan'])) {
    foreach($_POST['rekap'] as $id_siswa => $data) {
        $h = $data['h']; $s = $data['s']; $i = $data['i']; $a = $data['a'];
        mysqli_query($koneksi, "INSERT INTO rekap_absensi_semester (id_siswa, hadir, sakit, izin, alfa) 
                                VALUES ('$id_siswa', '$h', '$s', '$i', '$a') 
                                ON DUPLICATE KEY UPDATE hadir='$h', sakit='$s', izin='$i', alfa='$a'");
    }
    echo "<script>alert('Rekap absensi semester berhasil disimpan!');</script>";
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <style>
        .sidebar { background-color: #002147; height: 100vh; width: 260px; position: fixed; color: white; }
        .main-content { margin-left: 260px; padding: 30px; }
    </style>
</head>
<body>
    <div class="sidebar">
        <div class="p-3 text-center"><h5>SIAKAD GURU</h5></div><hr>
        <a href="input_rekap_semester.php" class="text-white p-2 d-block text-decoration-none bg-primary">Input Rekap Semester</a>
        <a href="dashboard.php" class="text-white p-2 d-block text-decoration-none">Kembali ke Dashboard</a>
    </div>

    <div class="main-content">
        <h3>Input Rekapitulasi Absensi Semester</h3>
        <form method="POST" class="card p-4 shadow-sm">
            <table class="table table-bordered">
                <thead>
                    <tr><th>Nama Siswa</th><th>Hadir</th><th>Sakit</th><th>Izin</th><th>Alfa</th></tr>
                </thead>
                <tbody>
                    <?php
                    $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'");
                    while($s = mysqli_fetch_array($siswa)) {
                        echo "<tr>
                                <td>{$s['nama']}</td>
                                <td><input type='number' name='rekap[{$s['id_siswa']}][h]' class='form-control' value='0'></td>
                                <td><input type='number' name='rekap[{$s['id_siswa']}][s]' class='form-control' value='0'></td>
                                <td><input type='number' name='rekap[{$s['id_siswa']}][i]' class='form-control' value='0'></td>
                                <td><input type='number' name='rekap[{$s['id_siswa']}][a]' class='form-control' value='0'></td>
                              </tr>";
                    }
                    ?>
                </tbody>
            </table>
            <button type="submit" name="simpan" class="btn btn-success"><i class="bi bi-floppy"></i> Simpan Rekap Semester</button>
        </form>
    </div>
</body>
</html>