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

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $sakit = $_POST['s'] ?? [];
    $izin = $_POST['i'] ?? [];
    $alfa = $_POST['a'] ?? [];

    foreach ($sakit as $id_siswa => $jml_sakit) {
        $jml_izin = $izin[$id_siswa] ?? 0;
        $jml_alfa = $alfa[$id_siswa] ?? 0;

        // Update data jika sudah ada, atau buat baru
        $sql = "INSERT INTO rekap_absensi_semester (id_siswa, sakit, izin, alfa) 
                VALUES ('$id_siswa', '$jml_sakit', '$jml_izin', '$jml_alfa')
                ON DUPLICATE KEY UPDATE 
                sakit = '$jml_sakit', izin = '$jml_izin', alfa = '$jml_alfa'";
        
        mysqli_query($koneksi, $sql);
    }

    echo "<script>alert('Data Rapor Manual Berhasil Disimpan!'); window.location.href='absensi_siswa.php?menu=rekap_semester';</script>";
} else {
    header("Location: absensi_siswa.php?menu=rekap_semester");
}
?>