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

$q_sem = mysqli_query($koneksi, "SELECT id_semester FROM semester WHERE status = 'Aktif' LIMIT 1");
$d_sem = mysqli_fetch_assoc($q_sem);
$id_semester = $d_sem['id_semester'] ?? '1';
$id_rombel = $_POST['id_rombel'] ?? '';

if (isset($_POST['sakit'])) {
    foreach ($_POST['sakit'] as $id_siswa => $jml_sakit) {
        $izin = $_POST['izin'][$id_siswa];
        $alfa = $_POST['alfa'][$id_siswa];
        
        $sql = "INSERT INTO absensi (id_siswa, id_semester, tanggal, sakit, izin, alfa) 
                VALUES ('$id_siswa', '$id_semester', '9999-12-31', '$jml_sakit', '$izin', '$alfa')
                ON DUPLICATE KEY UPDATE sakit='$jml_sakit', izin='$izin', alfa='$alfa'";
        mysqli_query($koneksi, $sql);
    }
    echo "<script>
        alert('Berhasil disimpan!'); 
        window.location='absensi_siswa.php?menu=rekap_manual&id_rombel=$id_rombel';
      </script>";
} else {
    echo "Data tidak diterima.";
}
?>