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

$id_user_login = $_SESSION['id_user'] ?? '';
$q_sinkron = mysqli_query($koneksi, "SELECT id_guru FROM users WHERE id_user = '$id_user_login'");
$data_guru = mysqli_fetch_array($q_sinkron);
$id_guru = $data_guru['id_guru'] ?? '';

include 'layout.php';

$id_rombel_pilih = $_GET['id_rombel'] ?? '';
$tanggal_pilih = $_GET['tanggal'] ?? date('Y-m-d');
$menu = $_GET['menu'] ?? 'input';
?>

<div class="main-content">
    <h3 class="mb-4">Absensi Siswa</h3>
    
    <div class="card p-3 mb-4 shadow-sm">
        <div class="d-flex gap-2">
            <a href="?menu=input" class="btn btn-primary">Input Harian</a>
            <a href="?menu=rekap_bulanan" class="btn btn-info text-white">Rekap Bulanan</a>
            <a href="?menu=rekap_semester" class="btn btn-warning">Rekap Semester</a>
        </div>
    </div>

    <div class="card p-4 shadow-sm">
        <?php 
        // --- MENU 1: INPUT HARIAN ---
        if ($menu == 'input') { 
            // ... (Kode Input Harian Anda tetap sama) ...
            include 'sub_menu_input.php'; // Atau paste kode input Anda di sini
        
        // --- MENU 2: REKAP BULANAN ---
        } elseif ($menu == 'rekap_bulanan') {
            echo "<div class='d-flex justify-content-between mb-3'><h5>Rekap Bulan ".date('F Y')."</h5><a href='cetak_rekap_bulanan.php' target='_blank' class='btn btn-secondary'>Cetak Rekap</a></div>";
            $res = mysqli_query($koneksi, "SELECT s.nama, SUM(IF(a.keterangan='S',1,0)) as s, SUM(IF(a.keterangan='I',1,0)) as i, SUM(IF(a.keterangan='A',1,0)) as a FROM master_siswa s JOIN anggota_rombel ar ON s.id_siswa = ar.id_siswa JOIN rombel r ON ar.id_rombel = r.id_rombel LEFT JOIN absensi a ON s.id_siswa = a.id_siswa WHERE r.wali_id = '$id_guru' GROUP BY s.id_siswa");
            echo "<table class='table table-bordered'><tr><th>Nama</th><th>S</th><th>I</th><th>A</th></tr>";
            while($r = mysqli_fetch_array($res)) { echo "<tr><td>{$r['nama']}</td><td>{$r['s']}</td><td>{$r['i']}</td><td>{$r['a']}</td></tr>"; }
            echo "</table>";

        // --- MENU 3: REKAP SEMESTER (INPUT MANUAL) ---
        } elseif ($menu == 'rekap_semester') {
            echo "<h5>Input Data Rapor (Manual)</h5>";
            echo "<form method='POST' action='simpan_rekap_semester.php'>";
            echo "<table class='table table-bordered'>
                    <tr><th>Nama Siswa</th><th>Sakit</th><th>Izin</th><th>Alfa</th></tr>";
            
            $q_siswa = mysqli_query($koneksi, "SELECT s.id_siswa, s.nama, r.sakit, r.izin, r.alfa 
                                              FROM master_siswa s 
                                              JOIN anggota_rombel ar ON s.id_siswa = ar.id_siswa 
                                              LEFT JOIN rekap_absensi_semester r ON s.id_siswa = r.id_siswa
                                              WHERE ar.id_rombel IN (SELECT id_rombel FROM rombel WHERE wali_id = '$id_guru')");
            
            while($s = mysqli_fetch_array($q_siswa)) {
                echo "<tr><td>{$s['nama']}</td>
                      <td><input type='number' name='s[{$s['id_siswa']}]' value='".($s['sakit'] ?? 0)."' class='form-control' style='width:80px'></td>
                      <td><input type='number' name='i[{$s['id_siswa']}]' value='".($s['izin'] ?? 0)."' class='form-control' style='width:80px'></td>
                      <td><input type='number' name='a[{$s['id_siswa']}]' value='".($s['alfa'] ?? 0)."' class='form-control' style='width:80px'></td></tr>";
            }
            echo "</table>
                  <button type='submit' class='btn btn-primary'>Simpan untuk Rapor</button>
                  <a href='cetak_rapor.php' target='_blank' class='btn btn-success'>Cetak Rapor</a>
                  </form>";
        }
        ?>
    </div>
</div>