<?php
require_once __DIR__ . '/includes/functions.php';
requireLogin();
$pageTitle = 'Laporan SPP';

$bulan = $_GET['bulan'] ?? date('m');
$tahun = $_GET['tahun'] ?? date('Y');

$stmt = $pdo->prepare("
    SELECT t.nama as tingkat_nama, k.nama as kelas_nama,
        COUNT(ts.id) as jumlah_siswa,
        SUM(ts.nominal) as total_tagihan,
        SUM(ts.jumlah_dibayar) as total_dibayar,
        SUM(CASE WHEN ts.status='lunas' THEN 1 ELSE 0 END) as jml_lunas,
        SUM(CASE WHEN ts.status='belum_lunas' THEN 1 ELSE 0 END) as jml_belum
    FROM tagihan_spp ts
    JOIN siswa s ON s.id = ts.siswa_id
    LEFT JOIN kelas k ON k.id = s.kelas_id
    LEFT JOIN tingkat t ON t.id = k.tingkat_id
    WHERE ts.bulan = :b AND ts.tahun = :t
    GROUP BY k.id
    ORDER BY t.nama, k.nama
");
$stmt->execute(['b'=>$bulan, 't'=>$tahun]);
$rekapKelas = $stmt->fetchAll();

$totalTagihan = array_sum(array_column($rekapKelas, 'total_tagihan'));
$totalDibayar = array_sum(array_column($rekapKelas, 'total_dibayar'));
$totalSiswa = array_sum(array_column($rekapKelas, 'jumlah_siswa'));
$totalLunas = array_sum(array_column($rekapKelas, 'jml_lunas'));
$totalBelum = array_sum(array_column($rekapKelas, 'jml_belum'));

include __DIR__ . '/includes/header.php';
include __DIR__ . '/includes/sidebar.php';
?>

<div class="d-flex justify-content-between align-items-center mb-3 no-print">
    <h5 class="mb-0">Laporan SPP per Kelas</h5>
    <button class="btn btn-outline-secondary" onclick="window.print()"><i class="bi bi-printer"></i> Cetak</button>
</div>

<div class="card mb-3 no-print">
    <div class="card-body">
        <form method="GET" class="row g-2">
            <div class="col-md-3">
                <select name="bulan" class="form-select">
                    <?php for ($i=1;$i<=12;$i++): ?><option value="<?= $i ?>" <?= $i==$bulan?'selected':'' ?>><?= namaBulan($i) ?></option><?php endfor; ?>
                </select>
            </div>
            <div class="col-md-3"><input type="number" name="tahun" value="<?= htmlspecialchars($tahun) ?>" class="form-control"></div>
            <div class="col-md-2"><button class="btn btn-primary w-100">Tampilkan</button></div>
        </form>
    </div>
</div>

<?= renderKopSurat(getPengaturan($pdo)) ?>
<div class="judul-laporan-resmi d-none d-print-block">
    <h5>LAPORAN SPP PER KELAS</h5>
    <p>Bulan <?= namaBulan($bulan) ?> <?= $tahun ?></p>
</div>

<div class="row g-3 mb-3">
    <div class="col-md-3"><div class="card stat-card p-3"><div class="text-muted small">Total Siswa Ditagih</div><div class="fw-bold fs-5"><?= $totalSiswa ?></div></div></div>
    <div class="col-md-3"><div class="card stat-card p-3"><div class="text-muted small">Sudah Lunas</div><div class="fw-bold fs-5 text-success"><?= $totalLunas ?></div></div></div>
    <div class="col-md-3"><div class="card stat-card p-3"><div class="text-muted small">Belum Lunas</div><div class="fw-bold fs-5 text-danger"><?= $totalBelum ?></div></div></div>
    <div class="col-md-3"><div class="card stat-card p-3"><div class="text-muted small">Total Terkumpul</div><div class="fw-bold fs-5"><?= rupiah($totalDibayar) ?></div></div></div>
</div>

<div class="card">
    <div class="card-header no-print">Rekap per Kelas - <?= namaBulan($bulan) ?> <?= $tahun ?></div>
    <div class="card-body">
        <table class="table table-bordered">
            <thead><tr><th>Kelas</th><th class="text-end">Jml Siswa</th><th class="text-end">Lunas</th><th class="text-end">Belum Lunas</th><th class="text-end">Total Tagihan</th><th class="text-end">Total Terbayar</th></tr></thead>
            <tbody>
            <?php foreach ($rekapKelas as $row): ?>
                <tr>
                    <td><?= htmlspecialchars(formatKelasLabel($row['tingkat_nama'] ?? '', $row['kelas_nama'] ?? '')) ?></td>
                    <td class="text-end"><?= $row['jumlah_siswa'] ?></td>
                    <td class="text-end text-success"><?= $row['jml_lunas'] ?></td>
                    <td class="text-end text-danger"><?= $row['jml_belum'] ?></td>
                    <td class="text-end"><?= rupiah($row['total_tagihan']) ?></td>
                    <td class="text-end"><?= rupiah($row['total_dibayar']) ?></td>
                </tr>
            <?php endforeach; ?>
            <?php if (empty($rekapKelas)): ?><tr><td colspan="6" class="text-center text-muted py-3">Belum ada tagihan periode ini</td></tr><?php endif; ?>
            </tbody>
            <tfoot class="table-light fw-bold">
                <tr><td>TOTAL</td><td class="text-end"><?= $totalSiswa ?></td><td class="text-end"><?= $totalLunas ?></td><td class="text-end"><?= $totalBelum ?></td><td class="text-end"><?= rupiah($totalTagihan) ?></td><td class="text-end"><?= rupiah($totalDibayar) ?></td></tr>
            </tfoot>
        </table>
    </div>
</div>

<?= renderTandaTanganLaporan(getPengaturan($pdo)) ?>

<?php include __DIR__ . '/includes/footer.php'; ?>