<?php $__env->startSection('title', $isWaliKelas ? 'Laporan SPP & DPP' : ($tab === 'dpp' ? 'Daftar Pembayaran DPP' : ($tab === 'tagihan' ? 'Daftar Pembayaran Tagihan' : 'Daftar Pembayaran SPP'))); ?>

<?php $__env->startPush('styles'); ?>
    <!-- Select2 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@ttskch/select2-bootstrap4-theme@x.x.x/dist/select2-bootstrap4.min.css">
    <style>
        @media print {
            /* Sembunyikan elemen yang tidak perlu dicetak */
            form, .btn, .main-header, .main-sidebar, .main-footer, .pagination, .alert {
                display: none !important;
            }
            .card { border: none !important; box-shadow: none !important; }
            .card-body { padding: 0 !important; }
            .table-responsive { overflow: visible !important; }
            body { background: #fff !important; }
            /* Mencegah tfoot (Total) berulang di setiap halaman saat dicetak */
            tfoot { display: table-row-group; }
            
            .content-wrapper::before {
                display: block;
                text-align: center;
                font-size: 24px;
                font-weight: bold;
                margin-bottom: 20px;
                <?php
                    $printTitle = 'DAFTAR PEMBAYARAN SPP';
                    if ($tab === 'dpp') $printTitle = 'DAFTAR PEMBAYARAN DPP';
                    elseif ($tab === 'semua' || empty($tab)) $printTitle = 'DAFTAR PEMBAYARAN SPP & DPP';
                    elseif ($tab === 'harian') $printTitle = 'DAFTAR PENERIMAAN PEMBAYARAN HARIAN';
                    elseif ($tab === 'tagihan') $printTitle = 'LAPORAN TAGIHAN';
                    elseif ($tab === 'riwayat') $printTitle = 'RIWAYAT PEMBAYARAN';
                ?>
                content: "<?php echo e($printTitle); ?>";
            }
        }
    </style>
<?php $__env->stopPush(); ?>

<?php $__env->startSection('content'); ?>
<div class="row">
    <div class="col-12">
        <div class="card card-primary card-outline">
            <div class="card-body">
                    
                    <?php if($tab === 'spp'): ?>
                        <!-- TAB REKAP PEMBAYARAN SPP PER SISWA -->
                        <form action="<?php echo e(route('laporan.index')); ?>" method="GET" class="mb-4">
                            <input type="hidden" name="tab" value="spp">
                            <input type="hidden" name="filter" value="1">
                            <div class="row align-items-end">
                                <div class="col-md-3 col-sm-6">
                                    <div class="form-group">
                                        <label>Tahun Ajaran</label>
                                        <select name="tahun_ajaran_id" id="filter-ta-spp" class="form-control">
                                            <?php $__currentLoopData = $tahunAjarans; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $ta): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                <option value="<?php echo e($ta->id); ?>" <?php echo e((request('tahun_ajaran_id') == $ta->id || (!request('tahun_ajaran_id') && $ta->status === 'aktif')) ? 'selected' : ''); ?>>
                                                    <?php echo e($ta->tahun); ?>

                                                </option>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                        </select>
                                    </div>
                                </div>
                                <?php if(!$isWaliKelas && !$isKepalaSekolah): ?>
                                <div class="col-md-2 col-sm-6">
                                    <div class="form-group">
                                        <label>Jenjang</label>
                                        <select name="jenjang" id="filter-jenjang-spp" class="form-control">
                                            <option value="">-- Semua Jenjang --</option>
                                            <?php $__currentLoopData = ['TK', 'SD', 'SMP', 'SMA']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $j): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                <option value="<?php echo e($j); ?>" <?php echo e(request('jenjang') == $j ? 'selected' : ''); ?>><?php echo e($j); ?></option>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                        </select>
                                    </div>
                                </div>
                                <?php endif; ?>
                                <?php if(!$isWaliKelas): ?>
                                <div class="col-md-2 col-sm-6">
                                    <div class="form-group">
                                        <label>Tingkat</label>
                                        <select name="tingkat" id="filter-tingkat-spp" class="form-control">
                                            <option value="">-- Semua Tingkat --</option>
                                            <?php $__currentLoopData = $kelases->map->only(['jenjang', 'tingkat'])->unique('tingkat')->sortBy('tingkat'); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $k): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                <?php if($k['tingkat']): ?>
                                                <option value="<?php echo e($k['tingkat']); ?>" 
                                                        data-jenjang="<?php echo e($k['jenjang']); ?>"
                                                        <?php echo e(request('tingkat') == $k['tingkat'] ? 'selected' : ''); ?>>
                                                    <?php echo e($k['tingkat']); ?>

                                                </option>
                                                <?php endif; ?>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-md-2 col-sm-6">
                                    <div class="form-group">
                                        <label>Paralel</label>
                                        <select name="paralel" id="filter-paralel-spp" class="form-control">
                                            <option value="">-- Semua Paralel --</option>
                                            <?php $__currentLoopData = $kelases->pluck('paralel')->filter()->unique()->sort(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $p): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                <option value="<?php echo e($p); ?>" <?php echo e(request('paralel') == $p ? 'selected' : ''); ?>><?php echo e($p); ?></option>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-md-3 col-sm-12">
                                    <div class="form-group">
                                        <label>Siswa</label>
                                        <select name="nis" class="form-control select2-siswa">
                                            <option value="">-- Semua Siswa --</option>
                                        </select>
                                    </div>
                                </div>
                                <?php endif; ?>
                                <div class="col-md-<?php echo e($isWaliKelas ? '9' : '12'); ?> d-flex align-items-end pb-3">
                                    <button type="submit" class="btn btn-primary mr-2"><i class="fas fa-search"></i> Tampilkan</button>
                                    <a href="<?php echo e(route('laporan.index', ['tab' => 'spp', 'filter' => '1'])); ?>" class="btn btn-default mr-2">Reset</a>
                                    
                                    <?php if(request()->has('filter')): ?>
                                        <button type="button" class="btn btn-info mr-2" onclick="window.print()">
                                            <i class="fas fa-print"></i> Cetak
                                        </button>
                                        <a href="<?php echo e(route('laporan.pembayaran.export', array_merge(request()->all(), ['type' => 'excel', 'jenis_pembayaran' => 'SPP']))); ?>" class="btn btn-success">
                                            <i class="fas fa-file-excel"></i> Export Excel
                                        </a>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </form>

                        <?php if(request()->has('filter') && $selectedTahunAjaran): ?>
                            <div class="card card-light mb-3">
                                <div class="card-header py-2">
                                    <h3 class="card-title text-sm"><i class="fas fa-info-circle mr-1"></i> Informasi Simbol Status</h3>
                                </div>
                                <div class="card-body py-2 text-sm d-flex flex-wrap">
                                    <div class="mr-4"><span class="text-success font-weight-bold" style="font-size: 1.1rem;">✓</span> = Lunas</div>
                                    <div class="mr-4"><span class="text-danger font-weight-bold" style="font-size: 1.4rem; line-height: 1;">&bull;</span> = Tunggakan / Belum Lunas (Bulan Sudah Berjalan)</div>
                                    <div><span class="text-muted font-weight-bold">-</span> = Belum Berjalan (Bulan Masa Mendatang)</div>
                                </div>
                            </div>

                            <div class="table-responsive">
                                <table class="table table-bordered table-sm table-striped table-hover text-center align-middle" style="font-size: 0.9rem;">
                                    <thead class="bg-light align-middle">
                                        <tr>
                                            <th width="40" rowspan="2" class="align-middle">No</th>
                                            <th width="75" rowspan="2" class="align-middle">NIS</th>
                                            <th rowspan="2" class="text-left align-middle font-weight-bold" style="min-width: 180px;">Nama Siswa</th>
                                            <th width="65" rowspan="2" class="align-middle">Kelas</th>
                                            <th colspan="12" class="py-1">Bulan Periode SPP (<?php echo e($selectedTahunAjaran->tahun); ?>)</th>
                                            <th width="80" rowspan="2" class="align-middle text-center text-nowrap">Belum Bayar</th>
                                            <th width="110" rowspan="2" class="align-middle text-right text-nowrap pr-3">Total Terbayar</th>
                                        </tr>
                                        <tr>
                                             <?php $__currentLoopData = $bulanList; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $bulan): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                 <?php
                                                     $tahunTagihan = ($bulan < $selectedTahunAjaran->bulan_mulai_spp) ? $tahunAwal + 1 : $tahunAwal;
                                                     
                                                     $isBelumBerjalan = false;
                                                     if ($selectedTahunAjaran->status === 'aktif') {
                                                         $akhirBulanTagihan = \Carbon\Carbon::create($tahunTagihan, $bulan, 1)->endOfMonth()->startOfDay();
                                                         $isBelumBerjalan = $akhirBulanTagihan->isFuture();}
                                                     $now = \Carbon\Carbon::now();
                                                     $isSudahBerjalan = ($tahunTagihan < $now->year) || ($tahunTagihan == $now->year && $bulan <= $now->month);
                                                 ?>
                                                 <th width="50" class="py-1 font-weight-normal" style="font-size: 0.8rem; <?php echo $isSudahBerjalan ? 'background-color: #fff3cd !important; color: #856404 !important; font-weight: bold !important;' : ''; ?>">
                                                     <?php echo e(substr(\App\Models\TahunAjaran::namaBulan($bulan), 0, 3)); ?>

                                                 </th>
                                             <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                         </tr>
                                    </thead>
                                    <tbody>
                                         <?php
                                                $grandTotalSPPTerbayar = 0;
                                                $grandTotalBulanBelumBayar = 0;
                                            ?>
                                        <?php $__empty_1 = true; $__currentLoopData = $siswaKelasList; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index => $sk): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                            <?php
                                                $siswa = $sk->siswa;
                                                $kelas = $sk->kelas;
                                                $studentTagihans = $rekapTagihans->get($sk->siswa_id, collect());
                                            ?>
                                            <tr>
                                                <td class="align-middle"><?php echo e($index + 1); ?></td>
                                                <td class="align-middle"><?php echo e($siswa->nis ?? '-'); ?></td>
                                                <td class="text-left align-middle font-weight-bold"><?php echo e($siswa->nama_lengkap ?? '-'); ?></td>
                                                <td class="align-middle"><?php echo e($kelas->nama_kelas ?? '-'); ?></td>

                                                <?php 
                                                    $totalSPPTerbayar = 0; 
                                                    $totalBulanBelumBayar = 0;
                                                ?>
                                                <?php $__currentLoopData = $bulanList; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $bulan): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                    <?php
                                                        $tahunTagihan = ($bulan < $selectedTahunAjaran->bulan_mulai_spp) ? $tahunAwal + 1 : $tahunAwal;
                                                                                   $isBelumBerjalan = false;
                            if ($selectedTahunAjaran->status === 'aktif') {
                                $akhirBulanTagihan = \Carbon\Carbon::create($tahunTagihan, $bulan, 1)->endOfMonth()->startOfDay();
                                $isBelumBerjalan = $akhirBulanTagihan->isFuture();
                            } elseif ($selectedTahunAjaran->id > $activeTahunAjaranId) {
                                $isBelumBerjalan = true;
                            }
                            $now = \Carbon\Carbon::now();
                            $isSudahBerjalan = ($tahunTagihan < $now->year) || ($tahunTagihan == $now->year && $bulan <= $now->month);

                            $tagihan = $studentTagihans->where('tarifPembayaran.jenis', 'SPP')->firstWhere('bulan', $bulan);

                            if($tagihan && $tagihan->status !== 'lunas') {
                                $totalBulanBelumBayar++;
                            } elseif(!$tagihan && !$isBelumBerjalan) {
                                $totalBulanBelumBayar++;
                            }
                                                    ?>
                                                    <td class="align-middle" <?php echo $isSudahBerjalan ? 'style="background-color: #fff3cd !important; font-weight: bold !important;"' : ''; ?>>
                                                        <?php if($tagihan): ?>
                                                            <?php if($tagihan->status === 'lunas'): ?>
                                                                <?php $totalSPPTerbayar += $tagihan->jumlah_terbayar; ?>
                                                                <span class="text-success font-weight-bold" style="font-size: 1.1rem;" title="Lunas: Rp <?php echo e(number_format($tagihan->jumlah_tagihan, 0, ',', '.')); ?>">✓</span>
                                                            <?php elseif($tagihan->is_tunggakan || ($isSudahBerjalan && $tagihan->status !== 'lunas')): ?>
                                                                <?php $totalSPPTerbayar += $tagihan->jumlah_terbayar; ?>
                                                                <span class="text-danger font-weight-bold" style="font-size: 1.4rem; line-height: 1;" title="Tunggakan SPP: Sisa Rp <?php echo e(number_format($tagihan->sisa_tagihan, 0, ',', '.')); ?>">&#8226;</span>
                                                            <?php else: ?>
                                                                <span class="text-muted font-weight-bold" title="Belum Berjalan">-</span>
                                                            <?php endif; ?>
                                                        <?php else: ?>
                                                            <?php if($isBelumBerjalan): ?>
                                                                <span class="text-muted font-weight-bold">-</span>
                                                            <?php else: ?>
                                                                <span class="text-danger font-weight-bold" style="font-size: 1.4rem; line-height: 1;" title="Tunggakan SPP (Belum ada tagihan)">&#8226;</span>
                                                            <?php endif; ?>
                                                        <?php endif; ?>
                                                    </td>
                                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                                <td class="align-middle text-center font-weight-bold text-danger">
                                                    <?php echo e($totalBulanBelumBayar > 0 ? $totalBulanBelumBayar : '-'); ?>

                                                </td>
                                                <td class="align-middle text-right font-weight-bold text-nowrap pr-3">
                                                    <span class="float-left">Rp</span> <?php echo e(number_format($totalSPPTerbayar, 0, ',', '.')); ?>

                                                </td>
                                            </tr>
                                            <?php 
                                                $grandTotalSPPTerbayar += $totalSPPTerbayar; 
                                                $grandTotalBulanBelumBayar += $totalBulanBelumBayar;
                                            ?>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                            <tr>
                                                <td colspan="30" class="text-center text-muted py-3">Data siswa tidak ditemukan untuk filter ini.</td>
                                            </tr>
                                        <?php endif; ?>
                                    </tbody>
                                    <tfoot>
                                        <tr class="bg-light font-weight-bold">
                                            <td colspan="<?php echo e(4 + count($bulanList)); ?>" class="text-right align-middle pr-3">Total Terbayar</td>
                                            <td class="align-middle text-center font-weight-bold text-danger"><?php echo e($grandTotalBulanBelumBayar > 0 ? $grandTotalBulanBelumBayar : '-'); ?></td>
                                            <td class="align-middle text-right text-nowrap pr-3 text-success">
                                                <span class="float-left">Rp</span> <?php echo e(number_format($grandTotalSPPTerbayar, 0, ',', '.')); ?>

                                            </td>
                                        </tr>
                                    </tfoot>
                                </table>
                            </div>
                            <div class="mt-3 d-flex justify-content-between align-items-center flex-wrap">
                                <div class="text-muted text-sm my-1">
                                    Menampilkan <?php echo e($siswaKelasList->count()); ?> siswa.
                                </div>
                            </div>
                        <?php else: ?>
                            <div class="alert alert-light text-center border">Silahkan isi filter dan klik Tampilkan untuk melihat rekap pembayaran SPP siswa.</div>
                        <?php endif; ?>
                    <?php elseif($tab === 'dpp'): ?>

                    
                        <!-- TAB REKAP PEMBAYARAN DPP PER SISWA -->
                        <form action="<?php echo e(route('laporan.index')); ?>" method="GET" class="mb-4">
                            <input type="hidden" name="tab" value="dpp">
                            <input type="hidden" name="filter" value="1">
                            <div class="row align-items-end">
                                <div class="col-md-3 col-sm-6">
                                    <div class="form-group">
                                        <label>Tahun Ajaran</label>
                                        <select name="tahun_ajaran_id" id="filter-ta-dpp" class="form-control">
                                            <?php $__currentLoopData = $tahunAjarans; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $ta): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                <option value="<?php echo e($ta->id); ?>" <?php echo e((request('tahun_ajaran_id') == $ta->id || (!request('tahun_ajaran_id') && $ta->status === 'aktif')) ? 'selected' : ''); ?>>
                                                    <?php echo e($ta->tahun); ?>

                                                </option>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                        </select>
                                    </div>
                                </div>
                                <?php if(!$isWaliKelas && !$isKepalaSekolah): ?>
                                <div class="col-md-2 col-sm-6">
                                    <div class="form-group">
                                        <label>Jenjang</label>
                                        <select name="jenjang" id="filter-jenjang-dpp" class="form-control">
                                            <option value="">-- Semua Jenjang --</option>
                                            <?php $__currentLoopData = ['TK', 'SD', 'SMP', 'SMA']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $j): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                <option value="<?php echo e($j); ?>" <?php echo e(request('jenjang') == $j ? 'selected' : ''); ?>><?php echo e($j); ?></option>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                        </select>
                                    </div>
                                </div>
                                <?php endif; ?>
                                <?php if(!$isWaliKelas): ?>
                                <div class="col-md-2 col-sm-6">
                                    <div class="form-group">
                                        <label>Tingkat</label>
                                        <select name="tingkat" id="filter-tingkat-dpp" class="form-control">
                                            <option value="">-- Semua Tingkat --</option>
                                            <?php $__currentLoopData = $kelases->map->only(['jenjang', 'tingkat'])->unique('tingkat')->sortBy('tingkat'); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $k): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                <?php if($k['tingkat']): ?>
                                                <option value="<?php echo e($k['tingkat']); ?>" 
                                                        data-jenjang="<?php echo e($k['jenjang']); ?>"
                                                        <?php echo e(request('tingkat') == $k['tingkat'] ? 'selected' : ''); ?>>
                                                    <?php echo e($k['tingkat']); ?>

                                                </option>
                                                <?php endif; ?>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-md-2 col-sm-6">
                                    <div class="form-group">
                                        <label>Paralel</label>
                                        <select name="paralel" id="filter-paralel-dpp" class="form-control">
                                            <option value="">-- Semua Paralel --</option>
                                            <?php $__currentLoopData = $kelases->pluck('paralel')->filter()->unique()->sort(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $p): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                <option value="<?php echo e($p); ?>" <?php echo e(request('paralel') == $p ? 'selected' : ''); ?>><?php echo e($p); ?></option>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-md-3 col-sm-12">
                                    <div class="form-group">
                                        <label>Siswa</label>
                                        <select name="nis" class="form-control select2-siswa">
                                            <option value="">-- Semua Siswa --</option>
                                        </select>
                                    </div>
                                </div>
                                <?php endif; ?>
                                <div class="col-md-<?php echo e($isWaliKelas ? '9' : '12'); ?> d-flex align-items-end pb-3">
                                    <button type="submit" class="btn btn-primary mr-2"><i class="fas fa-search"></i> Tampilkan</button>
                                    <a href="<?php echo e(route('laporan.index', ['tab' => 'dpp', 'filter' => '1'])); ?>" class="btn btn-default mr-2">Reset</a>
                                    
                                    <?php if(request()->has('filter')): ?>
                                        <button type="button" class="btn btn-info mr-2" onclick="window.print()">
                                            <i class="fas fa-print"></i> Cetak
                                        </button>
                                        <a href="<?php echo e(route('laporan.pembayaran.export', array_merge(request()->all(), ['type' => 'excel', 'jenis_pembayaran' => 'DPP']))); ?>" class="btn btn-success">
                                            <i class="fas fa-file-excel"></i> Export Excel
                                        </a>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </form>

                        <?php if(request()->has('filter') && $selectedTahunAjaran): ?>
                            <div class="table-responsive">
                                <table class="table table-bordered table-sm table-striped table-hover text-center align-middle" style="font-size: 0.9rem;">
                                    <thead class="bg-light align-middle">
                                        <tr>
                                            <th width="40" class="align-middle">No</th>
                                            <th width="80" class="align-middle">NIS</th>
                                            <th class="text-left align-middle font-weight-bold" style="min-width: 180px;">Nama Siswa</th>
                                            <th width="70" class="align-middle">Kelas</th>
                                            <th width="110" class="align-middle text-right pr-3">Tarif DPP</th>
                                            <th width="110" class="align-middle text-right pr-3">Tarif Lama</th>
                                            <th width="110" class="align-middle text-right pr-3">Terbayar</th>
                                            <th width="110" class="align-middle text-right pr-3">Sisa Tagihan</th>
                                            <th width="90" class="align-middle">Status</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                            $grandTotalDPPTerbayar = 0;
                                            $grandTotalDPPSisaTagihan = 0;
                                        ?>
                                        <?php $__empty_1 = true; $__currentLoopData = $siswaKelasList; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index => $sk): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                            <?php
                                                $siswa = $sk->siswa;
                                                $kelas = $sk->kelas;
                                                $studentTagihans = $rekapTagihans->get($sk->siswa_id, collect());
                                                $dppTagihan = $studentTagihans->where('tarifPembayaran.jenis', 'DPP')->first();
                                            ?>
                                            <tr>
                                                <td class="align-middle"><?php echo e($index + 1); ?></td>
                                                <td class="align-middle"><?php echo e($siswa->nis ?? '-'); ?></td>
                                                <td class="text-left align-middle font-weight-bold"><?php echo e($siswa->nama_lengkap ?? '-'); ?></td>
                                                <td class="align-middle"><?php echo e($kelas->nama_kelas ?? '-'); ?></td>
                                                <td class="text-right align-middle pr-3">
                                                    <span class="float-left">Rp</span> <?php echo e(number_format($dppTagihan->jumlah_tagihan ?? 0, 0, ',', '.')); ?>

                                                </td>
                                                <td class="text-right align-middle text-info pr-3">
                                                    <span class="float-left">Rp</span> <?php echo e(number_format($dppTagihan->tarif_lama ?? 0, 0, ',', '.')); ?>

                                                </td>
                                                <td class="text-right align-middle text-success font-weight-bold pr-3">
                                                    <span class="float-left">Rp</span> <?php echo e(number_format($dppTagihan->jumlah_terbayar ?? 0, 0, ',', '.')); ?>

                                                </td>
                                                <td class="text-right align-middle text-danger pr-3">
                                                    <span class="float-left">Rp</span> <?php echo e(number_format($dppTagihan->sisa_tagihan ?? 0, 0, ',', '.')); ?>

                                                </td>
                                                <td class="align-middle">
                                                    <?php if($dppTagihan): ?>
                                                        <?php if($dppTagihan->status === 'lunas'): ?>
                                                            <span class="badge badge-success">✓ Lunas</span>
                                                        <?php elseif($dppTagihan->status === 'kurang'): ?>
                                                            <span class="badge badge-warning">Kurang</span>
                                                        <?php else: ?>
                                                            <span class="badge badge-secondary">Belum Bayar</span>
                                                        <?php endif; ?>
                                                    <?php else: ?>
                                                        <span class="text-muted">-</span>
                                                    <?php endif; ?>
                                                </td>
                                            </tr>
                                            <?php
                                                $grandTotalDPPTerbayar += $dppTagihan->jumlah_terbayar ?? 0;
                                                $grandTotalDPPSisaTagihan += $dppTagihan->sisa_tagihan ?? 0;
                                            ?>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                            <tr>
                                                <td colspan="8" class="text-center text-muted py-3">Data siswa tidak ditemukan untuk filter ini.</td>
                                            </tr>
                                        <?php endif; ?>
                                    </tbody>
                                    <tfoot>
                                        <tr class="bg-light font-weight-bold">
                                            <td colspan="6" class="text-right align-middle pr-3">Total Terbayar & Sisa Tagihan</td>
                                            <td class="align-middle text-right text-nowrap pr-3 text-success">
                                                <span class="float-left">Rp</span> <?php echo e(number_format($grandTotalDPPTerbayar, 0, ',', '.')); ?>

                                            </td>
                                            <td class="align-middle text-right text-nowrap pr-3 text-danger">
                                                <span class="float-left">Rp</span> <?php echo e(number_format($grandTotalDPPSisaTagihan, 0, ',', '.')); ?>

                                            </td>
                                            <td class="align-middle text-center">-</td>
                                        </tr>
                                    </tfoot>
                                </table>
                            </div>
                            <div class="mt-3 d-flex justify-content-between align-items-center flex-wrap">
                                <div class="text-muted text-sm my-1">
                                    Menampilkan <?php echo e($siswaKelasList->count()); ?> siswa.
                                </div>
                            </div>
                        <?php else: ?>
                            <div class="alert alert-light text-center border">Silahkan isi filter dan klik Tampilkan untuk melihat rekap pembayaran DPP siswa.</div>
                        <?php endif; ?>
                    <?php elseif($tab === 'tagihan'): ?>
                        <!-- TAB LAPORAN TAGIHAN BULAN AKTIF -->
                        <form action="<?php echo e(route('laporan.index')); ?>" method="GET" class="mb-4">
                            <input type="hidden" name="tab" value="tagihan">
                            <input type="hidden" name="filter" value="1">
                            <div class="row align-items-end">
                                <div class="col-md-3 col-sm-6">
                                    <div class="form-group">
                                        <label>Tahun Ajaran</label>
                                        <select name="tahun_ajaran_id" class="form-control">
                                            <?php $__currentLoopData = $tahunAjarans; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $ta): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                <option value="<?php echo e($ta->id); ?>" <?php echo e((request('tahun_ajaran_id') == $ta->id || (!request('tahun_ajaran_id') && $ta->status === 'aktif')) ? 'selected' : ''); ?>>
                                                    <?php echo e($ta->tahun); ?>

                                                </option>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                        </select>
                                    </div>
                                </div>
                                <?php if(!$isWaliKelas && !$isKepalaSekolah): ?>
                                <div class="col-md-2 col-sm-6">
                                    <div class="form-group">
                                        <label>Jenjang</label>
                                        <select name="jenjang" class="form-control">
                                            <option value="">-- Semua Jenjang --</option>
                                            <?php $__currentLoopData = ['TK', 'SD', 'SMP', 'SMA']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $j): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                <option value="<?php echo e($j); ?>" <?php echo e(request('jenjang') == $j ? 'selected' : ''); ?>><?php echo e($j); ?></option>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                        </select>
                                    </div>
                                </div>
                                <?php endif; ?>
                                <?php if(!$isWaliKelas): ?>
                                <div class="col-md-2 col-sm-6">
                                    <div class="form-group">
                                        <label>Tingkat</label>
                                        <select name="tingkat" class="form-control">
                                            <option value="">-- Semua Tingkat --</option>
                                            <?php $__currentLoopData = $kelases->map->only(['jenjang', 'tingkat'])->unique('tingkat')->sortBy('tingkat'); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $k): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                <?php if($k['tingkat']): ?>
                                                <option value="<?php echo e($k['tingkat']); ?>" 
                                                        data-jenjang="<?php echo e($k['jenjang']); ?>"
                                                        <?php echo e(request('tingkat') == $k['tingkat'] ? 'selected' : ''); ?>>
                                                    <?php echo e($k['tingkat']); ?>

                                                </option>
                                                <?php endif; ?>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-md-2 col-sm-6">
                                    <div class="form-group">
                                        <label>Paralel</label>
                                        <select name="paralel" class="form-control">
                                            <option value="">-- Semua Paralel --</option>
                                            <?php $__currentLoopData = $kelases->pluck('paralel')->filter()->unique()->sort(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $p): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                <option value="<?php echo e($p); ?>" <?php echo e(request('paralel') == $p ? 'selected' : ''); ?>><?php echo e($p); ?></option>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-md-3 col-sm-12">
                                    <div class="form-group">
                                        <label>Siswa</label>
                                        <select name="nis" class="form-control select2-siswa">
                                            <option value="">-- Semua Siswa --</option>
                                        </select>
                                    </div>
                                </div>
                                <?php endif; ?>
                                <div class="col-md-<?php echo e($isWaliKelas ? '9' : '12'); ?> d-flex align-items-end pb-3">
                                    <button type="submit" class="btn btn-primary mr-2"><i class="fas fa-search"></i> Tampilkan</button>
                                    <a href="<?php echo e(route('laporan.index', ['tab' => 'tagihan', 'filter' => '1'])); ?>" class="btn btn-default mr-2">Reset</a>
                                    
                                    <?php if(request()->has('filter')): ?>
                                        <button type="button" class="btn btn-info mr-2" onclick="window.print()">
                                            <i class="fas fa-print"></i> Cetak
                                        </button>
                                        <a href="<?php echo e(route('laporan.tagihan.export', array_merge(request()->all(), ['type' => 'excel']))); ?>" class="btn btn-success">
                                            <i class="fas fa-file-excel"></i> Export Excel
                                        </a>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </form>

                        <?php if(request()->has('filter') && $selectedTahunAjaran): ?>
                            <div class="table-responsive">
                                <table class="table table-bordered table-sm table-striped table-hover align-middle text-center" style="font-size: 0.9rem;">
                                    <thead class="bg-light">
                                        <tr>
                                            <th width="40" class="align-middle">No</th>
                                            <th width="90" class="align-middle">NIS</th>
                                            <th class="text-left align-middle">Nama Siswa</th>
                                            <th width="80" class="align-middle">Kelas</th>
                                            <th width="120" class="align-middle text-right pr-3">Sisa SPP</th>
                                            <th width="120" class="align-middle text-right pr-3">Sisa DPP</th>
                                            <th width="130" class="align-middle text-right pr-3">Total Sisa</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                            $totalSPP = 0;
                                            $totalDPP = 0;
                                        ?>
                                        <?php $__empty_1 = true; $__currentLoopData = $siswaKelasList; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index => $sk): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                            <?php
                                                $siswa = $sk->siswa;
                                                $kelas = $sk->kelas;
                                                $studentTagihans = $rekapTagihans->get($sk->siswa_id, collect());
                                                
                                                $rowSPP = $studentTagihans->where('tarifPembayaran.jenis', 'SPP')->where('status', '!=', 'lunas')->sum('sisa_tagihan');
                                                $rowDPP = $studentTagihans->where('tarifPembayaran.jenis', 'DPP')->where('status', '!=', 'lunas')->sum('sisa_tagihan');
                                                $rowJumlah = $rowSPP + $rowDPP;

                                                $totalSPP += $rowSPP;
                                                $totalDPP += $rowDPP;
                                            ?>
                                            <tr>
                                                <td class="align-middle"><?php echo e($index + 1); ?></td>
                                                <td class="align-middle"><?php echo e($siswa->nis ?? '-'); ?></td>
                                                <td class="text-left align-middle font-weight-bold"><?php echo e($siswa->nama_lengkap ?? '-'); ?></td>
                                                <td class="align-middle"><?php echo e($kelas->nama_kelas ?? '-'); ?></td>
                                                <td class="text-right align-middle pr-3">
                                                    <?php if($rowSPP > 0): ?><span class="float-left">Rp</span> <?php echo e(number_format($rowSPP, 0, ',', '.')); ?><?php else: ?><span class="text-muted">-</span><?php endif; ?>
                                                </td>
                                                <td class="text-right align-middle pr-3">
                                                    <?php if($rowDPP > 0): ?><span class="float-left">Rp</span> <?php echo e(number_format($rowDPP, 0, ',', '.')); ?><?php else: ?><span class="text-muted">-</span><?php endif; ?>
                                                </td>
                                                <td class="text-right align-middle pr-3 font-weight-bold">
                                                    <?php if($rowJumlah > 0): ?><span class="float-left">Rp</span> <?php echo e(number_format($rowJumlah, 0, ',', '.')); ?><?php else: ?><span class="text-muted">-</span><?php endif; ?>
                                                </td>
                                            </tr>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                            <tr><td colspan="7" class="text-center text-muted py-3">Data siswa tidak ditemukan untuk filter ini.</td></tr>
                                        <?php endif; ?>
                                    </tbody>
                                    <?php if($siswaKelasList->isNotEmpty()): ?>
                                    <tfoot>
                                        <tr class="font-weight-bold bg-light">
                                            <th colspan="4" class="text-right align-middle">TOTAL SISA TAGIHAN</th>
                                            <th class="text-right align-middle pr-3 text-primary">
                                                <span class="float-left">Rp</span> <?php echo e(number_format($totalSPP, 0, ',', '.')); ?>

                                            </th>
                                            <th class="text-right align-middle pr-3 text-info">
                                                <span class="float-left">Rp</span> <?php echo e(number_format($totalDPP, 0, ',', '.')); ?>

                                            </th>
                                            <th class="text-right align-middle pr-3 text-success">
                                                <span class="float-left">Rp</span> <?php echo e(number_format($totalSPP + $totalDPP, 0, ',', '.')); ?>

                                            </th>
                                        </tr>
                                    </tfoot>
                                    <?php endif; ?>
                                </table>
                            </div>
                            <div class="mt-3 d-flex justify-content-between align-items-center flex-wrap">
                                <div class="text-muted text-sm my-1">
                                    Menampilkan <?php echo e($siswaKelasList->count()); ?> siswa.
                                </div>
                            </div>
                        <?php else: ?>
                            <div class="alert alert-light text-center border">Silahkan isi filter dan klik Tampilkan untuk melihat laporan tagihan bulan aktif.</div>
                        <?php endif; ?>

                    <?php elseif($tab === 'harian'): ?>
                        <!-- TAB PENERIMAAN HARIAN -->
                        <form action="<?php echo e(route('laporan.index')); ?>" method="GET" class="mb-4">
                            <input type="hidden" name="tab" value="harian">
                            <input type="hidden" name="filter" value="1">
                            <div class="row align-items-end">
    <div class="col-md-3">
        <div class="form-group">
            <label>Tanggal Transaksi</label>
            <input type="date" name="tanggal" class="form-control" value="<?php echo e(request('tanggal', $tanggalHarian)); ?>">
        </div>
    </div>
    <div class="col-md-3">
        <div class="form-group">
            <label>Metode Pembayaran</label>
            <select name="metode_pembayaran" class="form-control">
                <option value="">-- Semua Metode --</option>
                <?php $__currentLoopData = ['tunai','transfer','qris']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $mp): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <option value="<?php echo e($mp); ?>" <?php echo e(request('metode_pembayaran') == $mp ? 'selected' : ''); ?>><?php echo e(ucfirst($mp)); ?></option>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            </select>
        </div>
    </div>
    <div class="col-md-6 d-flex align-items-end pb-3">
        <button type="submit" class="btn btn-primary mr-2"><i class="fas fa-search"></i> Tampilkan</button>
        <a href="<?php echo e(route('laporan.index', ['tab' => 'harian', 'filter' => '1'])); ?>" class="btn btn-default mr-2">Reset</a>
        <?php if(request()->has('filter')): ?>
            <button type="button" class="btn btn-info mr-2" onclick="window.print()">
                <i class="fas fa-print"></i> Cetak
            </button>
            <a href="<?php echo e(route('laporan.harian.export', array_merge(request()->all(), ['type' => 'excel']))); ?>" class="btn btn-success">
                <i class="fas fa-file-excel"></i> Export Excel
            </a>
        <?php endif; ?>
    </div>
</div>
                            </div>
                        </form>

                        <?php if(request()->has('filter')): ?>
                            <div class="table-responsive">
                                <table class="table table-bordered table-sm table-striped table-hover align-middle text-center">
                                    <thead class="bg-light">
                                        <tr>
                                            <th width="40" class="align-middle">No</th>
                                            <th width="90" class="align-middle">NIS</th>
                                            <th class="text-left align-middle">Nama Siswa</th>
                                            <th width="80" class="align-middle">Kelas</th>
                                            <th width="120" class="align-middle">Bulan Bayar</th>
                                            <th width="120" class="align-middle text-right pr-3">SPP</th>
                                            <th width="120" class="align-middle text-right pr-3">DPP</th>
                                            <th width="130" class="align-middle text-right pr-3">Jumlah</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                            $totalSPP = 0;
                                            $totalDPP = 0;
                                        ?>
                                        <?php $__empty_1 = true; $__currentLoopData = $transaksiHarian; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index => $transaksi): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                            <?php
                                                $rowSPP = 0;
                                                $rowDPP = 0;
                                                $bulanBayar = [];
                                                foreach ($transaksi->detailPembayaran as $detail) {
                                                    $jenis = $detail->tagihan->tarifPembayaran->jenis ?? '';
                                                    if ($jenis === 'SPP') {
                                                        $rowSPP += $detail->jumlah_bayar;
                                                        if ($detail->tagihan->nama_bulan) {
                                                            $bulanBayar[] = substr($detail->tagihan->nama_bulan, 0, 3);
                                                        }
                                                    } elseif ($jenis === 'DPP') {
                                                        $rowDPP += $detail->jumlah_bayar;
                                                    }
                                                }
                                                $totalSPP += $rowSPP;
                                                $totalDPP += $rowDPP;
                                                $bulanStr = implode(', ', array_unique($bulanBayar));
                                            ?>
                                            <tr>
                                                <td class="align-middle"><?php echo e($index + 1); ?></td>
                                                <td class="align-middle"><?php echo e($transaksi->siswa->nis ?? '-'); ?></td>
                                                <td class="text-left align-middle font-weight-bold"><?php echo e($transaksi->siswa->nama_lengkap ?? '-'); ?></td>
                                                <td class="align-middle"><?php echo e($transaksi->siswa->kelas->first()->nama_kelas ?? '-'); ?></td>
                                                <td class="align-middle"><?php echo e($bulanStr ?: '-'); ?></td>
                                                <td class="text-right align-middle pr-3">
                                                    <?php if($rowSPP > 0): ?><span class="float-left">Rp</span> <?php echo e(number_format($rowSPP, 0, ',', '.')); ?><?php else: ?><span class="text-muted">-</span><?php endif; ?>
                                                </td>
                                                <td class="text-right align-middle pr-3">
                                                    <?php if($rowDPP > 0): ?><span class="float-left">Rp</span> <?php echo e(number_format($rowDPP, 0, ',', '.')); ?><?php else: ?><span class="text-muted">-</span><?php endif; ?>
                                                </td>
                                                <td class="text-right align-middle pr-3 font-weight-bold">
                                                    <span class="float-left">Rp</span> <?php echo e(number_format($transaksi->total_bayar, 0, ',', '.')); ?>

                                                </td>
                                            </tr>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                            <tr><td colspan="8" class="text-center text-muted">Tidak ada transaksi pada tanggal ini.</td></tr>
                                        <?php endif; ?>
                                    </tbody>
                                    <?php if($transaksiHarian->isNotEmpty()): ?>
                                    <tfoot>
                                        <tr class="font-weight-bold bg-light">
                                            <th colspan="5" class="text-right align-middle">TOTAL PENERIMAAN</th>
                                            <th class="text-right align-middle pr-3 text-primary">
                                                <span class="float-left">Rp</span> <?php echo e(number_format($totalSPP, 0, ',', '.')); ?>

                                            </th>
                                            <th class="text-right align-middle pr-3 text-info">
                                                <span class="float-left">Rp</span> <?php echo e(number_format($totalDPP, 0, ',', '.')); ?>

                                            </th>
                                            <th class="text-right align-middle pr-3 text-success">
                                                <span class="float-left">Rp</span> <?php echo e(number_format($transaksiHarian->sum('total_bayar'), 0, ',', '.')); ?>

                                            </th>
                                        </tr>
                                    </tfoot>
                                    <?php endif; ?>
                                </table>
                            </div>
                        <?php else: ?>
                            <div class="alert alert-light text-center">Silahkan isi tanggal dan klik Tampilkan untuk melihat data penerimaan harian.</div>
                        <?php endif; ?>

                    <?php elseif($tab === 'riwayat'): ?>
                        <div class="row">
                            <form method="GET" action="<?php echo e(route('laporan.index')); ?>" class="col-12 mb-3">
                                <input type="hidden" name="tab" value="riwayat">
                                <input type="hidden" name="filter" value="1">
                                <div class="row">
                                    <div class="col-md-2 mb-2">
                                        <select name="tahun_ajaran_id" class="form-control" required>
                                            <option value="">-- Pilih Tahun Ajaran --</option>
                                            <?php $__currentLoopData = $tahunAjarans; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $ta): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                <option value="<?php echo e($ta->id); ?>" <?php echo e((request('tahun_ajaran_id', $activeTahunAjaranId) == $ta->id) ? 'selected' : ''); ?>>
                                                    <?php echo e($ta->tahun); ?> <?php echo e($ta->status == 'aktif' ? '(Aktif)' : ''); ?>

                                                </option>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                        </select>
                                    </div>
                                    <?php if(!$isWaliKelas): ?>
                                        <?php if(!$isKepalaSekolah): ?>
                                        <div class="col-md-2 mb-2">
                                            <select name="jenjang" class="form-control" id="filter-jenjang-riwayat">
                                                <option value="">Semua Jenjang</option>
                                                <option value="TK" <?php echo e(request('jenjang') == 'TK' ? 'selected' : ''); ?>>TK</option>
                                                <option value="SD" <?php echo e(request('jenjang') == 'SD' ? 'selected' : ''); ?>>SD</option>
                                                <option value="SMP" <?php echo e(request('jenjang') == 'SMP' ? 'selected' : ''); ?>>SMP</option>
                                                <option value="SMA" <?php echo e(request('jenjang') == 'SMA' ? 'selected' : ''); ?>>SMA</option>
                                            </select>
                                        </div>
                                        <?php endif; ?>
                                        <div class="col-md-2 mb-2">
                                            <select name="tingkat" class="form-control" id="filter-tingkat-riwayat">
                                                <option value="">Semua Tingkat</option>
                                                <option value="<?php echo e(request('tingkat')); ?>" selected><?php echo e(request('tingkat')); ?></option>
                                            </select>
                                        </div>
                                        <div class="col-md-2 mb-2">
                                            <select name="paralel" class="form-control">
                                                <option value="">Semua Paralel</option>
                                                <?php $__currentLoopData = range('A', 'F'); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $char): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                    <option value="<?php echo e($char); ?>" <?php echo e(request('paralel') == $char ? 'selected' : ''); ?>><?php echo e($char); ?></option>
                                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                            </select>
                                        </div>
                                    <?php endif; ?>
                                    <div class="col-md-3 mb-2">
                                        <select name="nis" class="form-control select2-siswa">
                                            <!-- Select2 AJAX -->
                                        </select>
                                    </div>
                                </div>
                                <div class="row mt-2">
                                    <div class="col-md-12 d-flex align-items-end pb-3">
                                        <button type="submit" class="btn btn-primary mr-2"><i class="fas fa-search"></i> Tampilkan</button>
                                        <a href="<?php echo e(route('laporan.index', ['tab' => 'riwayat', 'filter' => '1'])); ?>" class="btn btn-default mr-2">Reset</a>
                                        <?php if(request()->has('filter')): ?>
                                            <button type="button" class="btn btn-info mr-2" onclick="window.print()">
                                                <i class="fas fa-print"></i> Cetak
                                            </button>
                                            <a href="<?php echo e(route('laporan.riwayat.export', array_merge(request()->all(), ['type' => 'excel']))); ?>" class="btn btn-success">
                                                <i class="fas fa-file-excel"></i> Export Excel
                                            </a>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </form>
                        </div>
                        <div class="table-responsive mt-3">
                            <table class="table table-bordered table-sm table-striped table-hover align-middle text-center" style="font-size: 0.9rem;">
                                <thead class="bg-light align-middle">
                                    <tr>
                                        <th width="40" class="align-middle">No</th>
                                        <th width="100" class="align-middle">NIS</th>
                                        <th class="text-left align-middle font-weight-bold">Nama Siswa</th>
                                        <th width="100" class="align-middle">Kelas</th>
                                        <th width="150" class="align-middle text-right pr-3">Total Bayar</th>
                                        <th width="80" class="align-middle">Aksi</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php $__empty_1 = true; $__currentLoopData = $siswaKelasList; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index => $sk): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                        <?php
                                            $siswaTransaksis = $riwayatTransaksi->get($sk->siswa_id, collect());
                                            $totalBayar = $siswaTransaksis->sum('total_bayar');
                                        ?>
                                        <tr>
                                            <td class="align-middle"><?php echo e($index + 1); ?></td>
                                            <td class="align-middle"><?php echo e($sk->siswa->nis); ?></td>
                                            <td class="text-left align-middle font-weight-bold"><?php echo e($sk->siswa->nama_lengkap); ?></td>
                                            <td class="align-middle"><?php echo e($sk->kelas->nama_kelas); ?></td>
                                            <td class="text-right align-middle pr-3">
                                                <span class="float-left">Rp</span> <?php echo e(number_format($totalBayar, 0, ',', '.')); ?>

                                            </td>
                                            <td class="align-middle">
                                                <button type="button" class="btn btn-sm btn-info" data-toggle="modal" data-target="#modalRiwayat<?php echo e($sk->siswa_id); ?>">
                                                    Detail
                                                </button>

                                                <!-- Modal Riwayat Detail -->
                                                <div class="modal fade" id="modalRiwayat<?php echo e($sk->siswa_id); ?>" tabindex="-1" role="dialog" aria-labelledby="modalRiwayatLabel<?php echo e($sk->siswa_id); ?>" aria-hidden="true">
                                                  <div class="modal-dialog modal-lg" role="document">
                                                    <div class="modal-content">
                                                      <div class="modal-header bg-primary text-white">
                                                        <div>
                                                            <h5 class="modal-title" id="modalRiwayatLabel<?php echo e($sk->siswa_id); ?>">Riwayat Pembayaran: <?php echo e($sk->siswa->nama_lengkap); ?></h5>
                                                            <small>NIS: <?php echo e($sk->siswa->nis); ?> | Kelas: <?php echo e($sk->kelas->nama_kelas); ?></small>
                                                        </div>
                                                        <button type="button" class="close text-white" data-dismiss="modal" aria-label="Close">
                                                          <span aria-hidden="true">&times;</span>
                                                        </button>
                                                      </div>
                                                      <div class="modal-body text-left p-0">
                                                          <table class="table table-striped table-sm m-0">
                                                              <thead class="bg-light">
                                                                  <tr>
                                                                      <th class="text-center">Tanggal</th>
                                                                      <th class="text-center">Jenis</th>
                                                                      <th>Keterangan</th>
                                                                      <th class="text-right">Nominal</th>
                                                                      <th class="text-center">Metode</th>
                                                                      <th class="text-center">Cetak</th>
                                                                      <th class="text-center">Bukti</th>
                                                                  </tr>
                                                              </thead>
                                                              <tbody>
                                                                  <?php
                                                                      $dppCount = 0;
                                                                      $totalSPPDetail = 0;
                                                                      $totalDPPDetail = 0;
                                                                      $totalDPPTransactions = 0;
                                                                      $detailWA = "";
                                                                      foreach($siswaTransaksis as $t) {
                                                                          foreach($t->detailPembayaran as $d) {
                                                                              if (($d->tagihan->tarifPembayaran->jenis ?? '') === 'DPP') {
                                                                                  $totalDPPTransactions++;
                                                                              }
                                                                          }
                                                                      }
                                                                  ?>
                                                                  <?php $__empty_2 = true; $__currentLoopData = $siswaTransaksis; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $transaksi): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_2 = false; ?>
                                                                      <?php $rowspan = $transaksi->detailPembayaran->count(); ?>
                                                                      <?php $__currentLoopData = $transaksi->detailPembayaran; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $detail): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                                      <tr>
                                                                          <?php if($loop->first): ?>
                                                                          <td rowspan="<?php echo e($rowspan); ?>" class="text-center align-middle"><?php echo e(\Carbon\Carbon::parse($transaksi->tanggal_bayar)->format('d/m/Y')); ?></td>
                                                                          <?php endif; ?>
                                                                          <td class="text-center align-middle"><?php echo e($detail->tagihan->tarifPembayaran->jenis ?? '-'); ?></td>
                                                                          <td class="align-middle">
                                                                            <?php if(($detail->tagihan->tarifPembayaran->jenis ?? '') == 'SPP'): ?>
                                                                                <?php 
                                                                                    $totalSPPDetail += $detail->jumlah_bayar; 
                                                                                    $ketWA = "SPP " . $detail->tagihan->nama_bulan . " " . ($transaksi->tahunAjaran->tahun ?? '');
                                                                                ?>
                                                                                <?php echo e($ketWA); ?>

                                                                            <?php elseif(($detail->tagihan->tarifPembayaran->jenis ?? '') == 'DPP'): ?>
                                                                                <?php 
                                                                                    $totalDPPDetail += $detail->jumlah_bayar; 
                                                                                    $dppCount++;
                                                                                    $isLunas = $detail->tagihan->status === 'lunas';
                                                                                    $isCicilan = $totalDPPTransactions > 1 || !$isLunas;
                                                                                    $ketWA = $isCicilan ? 'Pembayaran DPP ' . $dppCount : 'Pembayaran DPP';
                                                                                ?>
                                                                                <?php echo e($ketWA); ?>

                                                                            <?php else: ?>
                                                                                <?php $ketWA = $detail->tagihan->tarifPembayaran->nama_tarif ?? 'Tagihan'; ?>
                                                                                <?php echo e($ketWA); ?>

                                                                            <?php endif; ?>
                                                                            <?php
                                                                                $tglWA = \Carbon\Carbon::parse($transaksi->tanggal_bayar)->format('d/m/Y');
                                                                                $jenisWA = $detail->tagihan->tarifPembayaran->jenis ?? '-';
                                                                                $nomWA = "Rp " . number_format($detail->jumlah_bayar, 0, ',', '.');
                                                                                $metodeWA = strtoupper($transaksi->metode_pembayaran);
                                                                                $detailWA .= "- {$tglWA} | {$jenisWA} | {$ketWA} | {$nomWA} | {$metodeWA}\n";
                                                                            ?>
                                                                          </td>
                                                                          <td class="text-right align-middle pr-3">
                                                                              <span class="float-left">Rp</span> <?php echo e(number_format($detail->jumlah_bayar, 0, ',', '.')); ?>

                                                                          </td>
                                                                          <?php if($loop->first): ?>
                                                                          <td rowspan="<?php echo e($rowspan); ?>" class="text-center align-middle">
                                                                              <span class="badge badge-success"><?php echo e(strtoupper($transaksi->metode_pembayaran)); ?></span>
                                                                          </td>
                                                                          <td rowspan="<?php echo e($rowspan); ?>" class="text-center align-middle">
                                                                              <a href="<?php echo e(route('transaksi.bukti', $transaksi->id)); ?>" target="_blank" class="btn btn-sm btn-outline-primary" title="Cetak Bukti">
                                                                                  <i class="fas fa-print"></i> Cetak
                                                                              </a>
                                                                          </td>
                                                                          <td rowspan="<?php echo e($rowspan); ?>" class="text-center align-middle">
                                                                              <?php if($transaksi->bukti_bayar): ?>
                                                                                  <a href="<?php echo e(asset('storage/' . $transaksi->bukti_bayar)); ?>" target="_blank" class="btn btn-sm btn-outline-info" title="Lihat Bukti Bayar">
                                                                                      <i class="fas fa-image"></i> Lihat
                                                                                  </a>
                                                                              <?php else: ?>
                                                                                  <span class="text-muted">-</span>
                                                                              <?php endif; ?>
                                                                          </td>
                                                                          <?php endif; ?>
                                                                      </tr>
                                                                      <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                                                  <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_2): ?>
                                                                      <tr>
                                                                          <td colspan="7" class="text-center text-muted">Belum ada transaksi di tahun ajaran ini.</td>
                                                                      </tr>
                                                                  <?php endif; ?>
                                                              </tbody>
                                                          </table>
                                                      </div>
                                                      <div class="p-3 bg-light border-top">
                                                          <div class="d-flex justify-content-between mb-1">
                                                              <span class="text-muted">Total Pembayaran SPP:</span>
                                                              <span class="font-weight-bold">Rp <?php echo e(number_format($totalSPPDetail, 0, ',', '.')); ?></span>
                                                          </div>
                                                          <div class="d-flex justify-content-between mb-1">
                                                              <span class="text-muted">Total Pembayaran DPP:</span>
                                                              <span class="font-weight-bold">Rp <?php echo e(number_format($totalDPPDetail, 0, ',', '.')); ?></span>
                                                          </div>
                                                          <div class="d-flex justify-content-between border-top pt-2 mt-2">
                                                              <span class="font-weight-bold">Total Keseluruhan:</span>
                                                              <span class="font-weight-bold text-success">Rp <?php echo e(number_format($totalBayar, 0, ',', '.')); ?></span>
                                                          </div>
                                                      </div>
                                                      <div class="modal-footer justify-content-between">
                                                        <div>
                                                            <?php
                                                                $pesanWA = "Rekap Pembayaran Siswa\n" . 
                                                                           "Nama: {$sk->siswa->nama_lengkap}\n" . 
                                                                           "NIS: {$sk->siswa->nis}\n" . 
                                                                           "Kelas: {$sk->kelas->nama_kelas}\n\n" . 
                                                                           "Rincian Pembayaran:\n" . 
                                                                           ($detailWA ?: "- Belum ada pembayaran\n") . "\n" .
                                                                           "Total Bayar SPP: Rp " . number_format($totalSPPDetail, 0, ',', '.') . "\n" . 
                                                                           "Total Bayar DPP: Rp " . number_format($totalDPPDetail, 0, ',', '.') . "\n" . 
                                                                           "Total Keseluruhan: Rp " . number_format($totalBayar, 0, ',', '.');
                                                                $noWa = $sk->siswa->no_wa ?? '';
                                                            ?>
                                                            <?php if($noWa): ?>
                                                            <button type="button" class="btn btn-success btn-kirim-wa-rekap" data-siswa-id="<?php echo e($sk->siswa_id); ?>" data-pesan="<?php echo e(htmlspecialchars($pesanWA)); ?>"><i class="fab fa-whatsapp"></i> Kirim Pesan</button>
                                                            <?php else: ?>
                                                            <button type="button" class="btn btn-secondary" disabled title="Nomor WA tidak tersedia"><i class="fab fa-whatsapp"></i> Kirim Pesan</button>
                                                            <?php endif; ?>
                                                            <a href="<?php echo e(route('laporan.cetakRiwayat', ['siswa_id' => $sk->siswa_id, 'tahun_ajaran_id' => $selectedTahunAjaran->id ?? ''])); ?>" target="_blank" class="btn btn-primary"><i class="fas fa-print"></i> Cetak Rekap</a>
                                                        </div>
                                                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Tutup</button>
                                                      </div>
                                                    </div>
                                                  </div>
                                                </div>
                                            </td>
                                        </tr>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                        <tr><td colspan="6" class="text-center text-muted">Data tidak ditemukan.</td></tr>
                                    <?php endif; ?>
                                </tbody>
                            </table>
                        </div>
                        <div class="d-flex justify-content-center mt-3">
                            <span class="text-muted">Menampilkan <?php echo e($siswaKelasList->count()); ?> siswa.</span>
                        </div>
                    <?php endif; ?>
            </div>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<script>
$(document).ready(function() {
    $('.select2-siswa').select2({
        theme: 'bootstrap4',
        placeholder: 'Ketik NIS atau Nama...',
        allowClear: true,
        ajax: {
            url: "<?php echo e(route('ajax.search-nis')); ?>",
            dataType: 'json',
            delay: 250,
            data: function (params) {
                return { q: params.term };
            },
            processResults: function (data) {
                let items = data.results || data;
                return {
                    results: $.map(items, function (item) {
                        return { text: item.text || (item.nis + ' - ' + item.nama_lengkap), id: item.id }
                    })
                };
            },
            cache: true
        }
    });

    // Pre-select nis if exists in URL
    let oldSiswaId = '<?php echo e(request("nis")); ?>';
    if(oldSiswaId) {
        $.ajax({
            type: 'GET',
            url: "<?php echo e(route('ajax.search-nis')); ?>",
            data: { q: oldSiswaId }
        }).then(function (data) {
            let items = data.results || data;
            let found = items.find(item => item.id == oldSiswaId);
            if(found) {
                var option = new Option(found.text, found.id, true, true);
                $('.select2-siswa').append(option).trigger('change');
            }
        });
    }

    // Filter Tingkat Dropdown berdasarkan Jenjang
    function setupFilterTingkat(suffix) {
        let jenjangSelect = $('#filter-jenjang-' + suffix);
        let tingkatSelect = $('#filter-tingkat-' + suffix);

        if (!tingkatSelect.length) return;

        function runFilter() {
            let jenjang = jenjangSelect.val();
            
            tingkatSelect.find('option').each(function() {
                let option = $(this);
                let optionJenjang = option.attr('data-jenjang');
                
                if (!option.val()) {
                    option.show();
                    return;
                }
                
                let matchJenjang = !jenjang || optionJenjang == jenjang;
                
                if (matchJenjang) {
                    option.show();
                } else {
                    option.hide();
                    if (option.is(':selected')) {
                        tingkatSelect.val('');
                    }
                }
            });
        }

        if (jenjangSelect.length) {
            jenjangSelect.on('change', runFilter);
            runFilter();
        }
    }

    setupFilterTingkat('spp');
    setupFilterTingkat('dpp');

    $('.btn-kirim-wa-rekap').click(function(e) {
        e.preventDefault();
        var btn = $(this);
        var siswaId = btn.data('siswa-id');
        var pesan = btn.data('pesan');
        var originalHtml = btn.html();

        btn.prop('disabled', true).html('<i class="fas fa-spinner fa-spin"></i> Mengirim...');

        $.ajax({
            url: '<?php echo e(route("ajax.laporan.kirim-wa-rekap")); ?>',
            type: 'POST',
            data: {
                _token: '<?php echo e(csrf_token()); ?>',
                siswa_id: siswaId,
                pesan: pesan
            },
            success: function(res) {
                if (res.success) {
                    Swal.fire({ icon: 'success', title: 'Berhasil', text: 'Pesan rekap berhasil dikirim via WhatsApp!', timer: 3000, showConfirmButton: false });
                    btn.html('<i class="fas fa-check"></i> Terkirim');
                    setTimeout(function() { btn.prop('disabled', false).html(originalHtml); }, 3000);
                } else {
                    Swal.fire({ icon: 'error', title: 'Gagal', text: res.message || 'Gagal mengirim pesan' });
                    btn.prop('disabled', false).html(originalHtml);
                }
            },
            error: function(err) {
                Swal.fire({ icon: 'error', title: 'Error', text: 'Terjadi kesalahan sistem atau koneksi.' });
                btn.prop('disabled', false).html(originalHtml);
            }
        });
    });
});
</script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\Herd\spp_app\resources\views/laporan/index.blade.php ENDPATH**/ ?>