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

// Ambil parameter filter, default bulan ini
$id_rombel = $_GET['id_rombel'] ?? '';
$bulan = $_GET['bulan'] ?? date('m');
$tahun = $_GET['tahun'] ?? date('Y');
$jumlah_hari = cal_days_in_month(CAL_GREGORIAN, $bulan, $tahun);

include 'layout.php';
?>

<style>
    @media print {
        .no-print { display: none !important; }
        .table { border: 1px solid #000; }
        .table th, .table td { padding: 4px; font-size: 10px; }
    }
</style>

<div class="container-fluid p-4">
    <h3>Rekap Absensi Bulanan</h3>
    
    <form method="GET" class="row g-3 mb-4 no-print card p-3 shadow-sm">
        <input type="hidden" name="menu" value="rekap_bulanan">
        <div class="col-md-3">
            <label>Kelas:</label>
            <select name="id_rombel" class="form-select" required>
                <option value="">-- Pilih Kelas --</option>
                <?php
                $q_r = mysqli_query($koneksi, "SELECT * FROM rombel ORDER BY kelas ASC");
                while($r = mysqli_fetch_array($q_r)) {
                    $sel = ($r['id_rombel'] == $id_rombel) ? 'selected' : '';
                    echo "<option value='{$r['id_rombel']}' $sel>{$r['kelas']}</option>";
                }
                ?>
            </select>
        </div>
        <div class="col-md-2">
            <label>Bulan:</label>
            <select name="bulan" class="form-select">
                <?php for($m=1; $m<=12; $m++): $m_val = str_pad($m, 2, '0', STR_PAD_LEFT); ?>
                    <option value="<?= $m_val ?>" <?= ($m_val == $bulan) ? 'selected' : '' ?>><?= date('F', mktime(0,0,0,$m,1)) ?></option>
                <?php endfor; ?>
            </select>
        </div>
        <div class="col-md-2">
            <label>Tahun:</label>
            <input type="number" name="tahun" class="form-control" value="<?= $tahun ?>">
        </div>
        <div class="col-md-2 d-flex align-items-end">
            <button type="submit" class="btn btn-primary w-100">Tampilkan</button>
        </div>
    </form>

    <?php if (!empty($id_rombel)): ?>
        <div class="card p-4 shadow-sm">
            <div class="table-responsive">
                <table class="table table-bordered table-sm text-center">
                    <thead class="table-dark">
                        <tr>
                            <th class="text-start" style="min-width: 150px;">Nama Siswa</th>
                            <?php for($d=1; $d<=$jumlah_hari; $d++) echo "<th>$d</th>"; ?>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        // Ambil semua data absensi bulan terpilih untuk rombel ini
                        $sql_data = "SELECT a.id_siswa, DAY(a.tanggal) as tgl, a.status 
                                     FROM absensi a
                                     JOIN anggota_rombel ar ON a.id_siswa = ar.id_siswa
                                     WHERE ar.id_rombel = '$id_rombel' 
                                     AND MONTH(a.tanggal) = '$bulan' AND YEAR(a.tanggal) = '$tahun'";
                        $res_data = mysqli_query($koneksi, $sql_data);
                        $data_absensi = [];
                        while($row = mysqli_fetch_assoc($res_data)) {
                            $data_absensi[$row['id_siswa']][$row['tgl']] = $row['status'];
                        }

                        // 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' ORDER BY s.nama ASC");
                        
                        while($s = mysqli_fetch_array($q_siswa)) {
                            echo "<tr><td class='text-start'>{$s['nama']}</td>";
                            for($d=1; $d<=$jumlah_hari; $d++) {
                                $status = $data_absensi[$s['id_siswa']][$d] ?? '-';
                                echo "<td>$status</td>";
                            }
                            echo "</tr>";
                        }
                        ?>
                    </tbody>
                </table>
            </div>
            <button onclick="window.print()" class="btn btn-success no-print mt-3">Cetak Rekap</button>
        </div>
    <?php endif; ?>
</div>

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