<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Laporan Alokasi Herregistrasi</title>
    <style>
        @page {
            size: landscape;
            margin: 5mm;
        }

        body {
            font-family: Arial, sans-serif;
            font-size: 10px;
            margin: 10px;
            color: #000;
        }

        .header {
            text-align: center;
            margin-bottom: 10px;
            border-bottom: 2px solid #000;
            padding-bottom: 6px;
        }

        .header h1 {
            margin: 0;
            font-size: 16px;
            font-weight: bold;
        }

        .header p {
            margin: 2px 0;
            font-size: 9px;
        }

        .info {
            margin-bottom: 10px;
        }

        .info table {
            width: 100%;
            border: none;
        }

        .info td {
            padding: 2px 4px;
            border: none;
        }

        h3 {
            margin: 12px 0 6px;
            font-size: 12px;
            border-bottom: 1px solid #000;
            padding-bottom: 3px;
        }

        h4 {
            margin: 8px 0 4px;
            font-size: 11px;
            font-weight: bold;
            background-color: #e5e7eb;
            padding: 5px 8px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 12px;
            page-break-inside: avoid;
        }

        thead {
            display: table-header-group;
        }

        th,
        td {
            border: 1px solid #000;
            padding: 3px 4px;
            font-size: 8px;
        }

        th {
            background: #e5e7eb;
            text-align: center;
            font-weight: bold;
            font-size: 9px;
        }

        td {
            font-size: 9px;
        }

        .text-center {
            text-align: center;
        }

        .text-right {
            text-align: right;
        }

        .text-left {
            text-align: left;
        }

        .font-bold {
            font-weight: bold;
        }

        .total-row {
            background: #f3f4f6;
            font-weight: bold;
        }

        .grand-total {
            background: #fef3c7;
            font-weight: bold;
        }

        .pph-row {
            background: #fef2f2;
        }

        .total-keseluruhan {
            color: #7c3aed;
            font-weight: bold;
        }

        .bagi-kampus {
            color: #2563eb;
            font-weight: bold;
        }

        .bagi-k2 {
            color: #dc2626;
            font-weight: bold;
        }

        .label {
            background-color: #e5e7eb;
            font-weight: bold;
        }

        .ttd-section {
            width: 100%;
            margin-top: 50px;
            margin-bottom: 20px;
            page-break-before: always;
        }

        .ttd-box {
            width: 40%;
            float: right;
            text-align: center;
            font-size: 10px;
        }

        .ttd-space {
            height: 50px;
        }

        .clear {
            clear: both;
        }

        .table-wrapper {
            overflow-x: auto;
            margin-bottom: 15px;
            max-width: 100%;
        }

        .kesimpulan-detail table {
            width: 80%;
            margin: 0 auto;
        }

        .kesimpulan-detail td {
            padding: 6px 10px;
        }

        .sub-header {
            background-color: #d1d5db;
            font-weight: bold;
        }

        .akhir-laporan {
            page-break-inside: avoid;
            margin-top: 20px;
        }

        .ringkasan-bagi-hasil,
        .kesimpulan-detail,
        .ttd-section {
            page-break-inside: avoid;
        }

        .table-wrapper table {
            page-break-inside: auto;
        }
    </style>
</head>

<body>

    <!-- HEADER -->
    <div class="header">
        <h1>LAPORAN ALOKASI HERREGISTRASI</h1>
        <p>SIMA - Sistem Informasi Mahasiswa</p>
        <p>Tanggal Cetak: {{ $tanggal_cetak->format('d/m/Y H:i') }}</p>
    </div>

    <!-- INFO -->
    <div class="info">
        <table>
            <tr>
                <td width="20%"><strong>Dicetak Oleh</strong></td>
                <td width="30%">: {{ auth()->user()->name ?? 'System' }}</td>
                <td width="20%"><strong>Total Mahasiswa</strong></td>
                <td width="30%">: {{ number_format($totalPendaftar) }}</td>
            </tr>
            @if ($filter['tahun_ajaran'] ?? null)
            <tr>
                <td><strong>Tahun Ajaran</strong></td>
                <td>: {{ $filter['tahun_ajaran'] }}</td>
                <td><strong>Periode</strong></td>
                <td>: {{ $startDate ?? '' }} s/d {{ $endDate ?? '' }}</td>
            </tr>
            @endif
            @if ($filter['kampus_id'] ?? null)
            <tr>
                <td><strong>Kampus</strong></td>
                <td>: {{ \App\Models\Kampus::find($filter['kampus_id'])?->nama ?? '-' }}</td>
                <td><strong>Prodi</strong></td>
                <td>: {{ \App\Models\Prodi::find($filter['prodi_id'])?->nama ?? '-' }}</td>
            </tr>
            @endif
        </table>
    </div>

    <!-- RINGKASAN PER KAMPUS -->
<h3>Ringkasan per Kampus</h3>
<div class="table-wrapper">
    <table>
        <thead>
            <tr>
                <th>No</th>
                <th>Kampus</th>
                <th>Jml Mhs</th>
                <th>Total Herregistrasi</th>
                @php
                    // Perbaikan: $ringkasanData adalah array, bukan Collection
                    $firstKampus = !empty($ringkasanData) ? reset($ringkasanData) : null;
                    if ($firstKampus && isset($firstKampus['biaya_tetap']) && is_array($firstKampus['biaya_tetap'])) {
                        foreach ($firstKampus['biaya_tetap'] as $biaya) {
                            echo '<th>' . $biaya['nama'] . '</th>';
                        }
                    }
                @endphp
                <th>Total Biaya</th>
                <th>Bagi Kampus</th>
                <th>Bagi K2</th>
            </tr>
        </thead>
        <tbody>
            @php $no = 1; @endphp
            @foreach ($ringkasanData as $item)
            <tr>
                <td class="text-center">{{ $no++ }}</td>
                <td class="text-left">{{ $item['kampus']->nama }}</td>
                <td class="text-center">{{ number_format($item['jumlah_pendaftar']) }}</td>
                <td class="text-right">Rp {{ number_format($item['biaya_herregistrasi']['total'], 0, ',', '.') }}</td>
                @foreach ($item['biaya_tetap'] as $biaya)
                    <td class="text-right">Rp {{ number_format($biaya['total'], 0, ',', '.') }}</td>
                @endforeach
                <td class="text-right total-keseluruhan">Rp {{ number_format($item['total_biaya'], 0, ',', '.') }}</td>
                <td class="text-right bagi-kampus">Rp {{ number_format($item['total_bagi_hasil_kampus'], 0, ',', '.') }}</td>
                <td class="text-right bagi-k2">Rp {{ number_format($item['total_bagi_hasil_k2'], 0, ',', '.') }}</td>
            </tr>
            @endforeach
            <tr class="total-row">
                <td colspan="3" class="text-right"><strong>TOTAL</strong></td>
                <td class="text-right"><strong>Rp {{ number_format($totalBiayaHerregistrasi, 0, ',', '.') }}</strong></td>
                @foreach ($totalBiayaTetapPerJenis as $total)
                    <td class="text-right"><strong>Rp {{ number_format($total, 0, ',', '.') }}</strong></td>
                @endforeach
                <td class="text-right"><strong>Rp {{ number_format($totalBiayaHerregistrasi + $totalSemuaBiayaTetap, 0, ',', '.') }}</strong></td>
                <td class="text-right"><strong>Rp {{ number_format($totalBagiHasilKampus + $totalSemuaBagiHasilTetapKampus, 0, ',', '.') }}</strong></td>
                <td class="text-right"><strong>Rp {{ number_format($totalBagiHasilK2 + $totalSemuaBagiHasilTetapK2, 0, ',', '.') }}</strong></td>
            </tr>
        </tbody>
    </table>
</div>

    <!-- DETAIL MAHASISWA PER TAHUN AJARAN -->
    <h3>Detail Mahasiswa per Tahun Ajaran</h3>

    <div style="font-size: 8px; margin-bottom: 8px; font-style: italic;">
        <strong>Keterangan:</strong> SPB (Sumbangan Pengembangan Bangunan) | SPP (Sumbangan Pembinaan Pendidikan) | UKT (Uang Kuliah Tunggal) | BT (Biaya Tetap)
    </div>

    @php
        $groupedByTahun = $mahasiswas->groupBy('tahun_ajaran');
        $sortedTahun = $groupedByTahun->keys()->sortDesc();
        $hasSpb = $mahasiswas->contains(fn($m) => ($m->spb1->dibayar ?? 0) > 0);
        $hasSpp = $mahasiswas->contains(fn($m) => ($m->spp1->dibayar ?? 0) > 0);
        $hasUkt = $mahasiswas->contains(fn($m) => ($m->ukt1->dibayar ?? 0) > 0);

        $allBiayaTetap = [];
        foreach ($mahasiswas as $mhs) {
            $biayaTetapItems = $mhs->biaya_tetap_list ?? [];
            foreach ($biayaTetapItems as $nama => $nominal) {
                if (!in_array($nama, $allBiayaTetap)) {
                    $allBiayaTetap[] = $nama;
                }
            }
        }
        sort($allBiayaTetap);
    @endphp

    @foreach ($sortedTahun as $tahun)
        @php $mahasiswaGroup = $groupedByTahun[$tahun]; @endphp
        <h4>Tahun Ajaran: {{ $tahun ?? 'Tidak Diketahui' }} ({{ $mahasiswaGroup->count() }} Mahasiswa)</h4>

        <div class="table-wrapper">
            <table style="min-width: 950px;">
                <thead>
                    <tr>
                        <th>No</th>
                        <th>NIM</th>
                        <th>Nama</th>
                        <th>Kampus</th>
                        <th>Prodi</th>
                        @if ($hasSpb)
                            <th>SPB</th>
                        @endif
                        @if ($hasSpp)
                            <th>SPP</th>
                        @endif
                        @if ($hasUkt)
                            <th>UKT</th>
                        @endif
                        @foreach ($allBiayaTetap as $biayaTetap)
                            <th>{{ $biayaTetap }}</th>
                        @endforeach
                        <th>Total Dibayar</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach ($mahasiswaGroup as $index => $mhs)
                        @php
                            $spbNominal = $mhs->spb1->dibayar ?? 0;
                            $sppNominal = $mhs->spp1->dibayar ?? 0;
                            $uktNominal = $mhs->ukt1->dibayar ?? 0;

                            $biayaTetapItems = $mhs->biaya_tetap_list ?? [];
                            $totalBiayaTetap = 0;
                            foreach ($allBiayaTetap as $bt) {
                                $totalBiayaTetap += $biayaTetapItems[$bt] ?? 0;
                            }
                            $totalDibayar = $spbNominal + $sppNominal + $uktNominal + $totalBiayaTetap;
                        @endphp
                        <tr>
                            <td class="text-center">{{ $index + 1 }}</td>
                            <td class="text-center">{{ $mhs->nim ?? '-' }}</td>
                            <td class="text-left">{{ $mhs->nama ?? '-' }}</td>
                            <td class="text-left">{{ $mhs->kampus->nama ?? '-' }}</td>
                            <td class="text-left">{{ $mhs->prodi->nama ?? '-' }}</td>

                            @if ($hasSpb)
                                <td class="text-right">{{ $spbNominal > 0 ? 'Rp ' . number_format($spbNominal, 0, ',', '.') : '-' }}</td>
                            @endif

                            @if ($hasSpp)
                                <td class="text-right">{{ $sppNominal > 0 ? 'Rp ' . number_format($sppNominal, 0, ',', '.') : '-' }}</td>
                            @endif

                            @if ($hasUkt)
                                <td class="text-right">{{ $uktNominal > 0 ? 'Rp ' . number_format($uktNominal, 0, ',', '.') : '-' }}</td>
                            @endif

                            @foreach ($allBiayaTetap as $biayaTetap)
                                <td class="text-right">
                                    @if (isset($biayaTetapItems[$biayaTetap]) && $biayaTetapItems[$biayaTetap] > 0)
                                        Rp {{ number_format($biayaTetapItems[$biayaTetap], 0, ',', '.') }}
                                    @else
                                        -
                                    @endif
                                </td>
                            @endforeach

                            <td class="text-right"><strong>Rp {{ number_format($totalDibayar, 0, ',', '.') }}</strong></td>
                            </tr>
                    @endforeach
                </tbody>
                @php
                    $totalColspan = 5;
                    if ($hasSpb) $totalColspan++;
                    if ($hasSpp) $totalColspan++;
                    if ($hasUkt) $totalColspan++;
                    $totalColspan += count($allBiayaTetap);
                    $totalColspan += 2;
                @endphp
                <tfoot style="background: #f3f4f6; font-weight: bold;">
                    <tr class="total-row">
                        <td colspan="{{ $totalColspan - 2 }}" class="text-right"><strong>TOTAL</strong></td>
                        <td class="text-right"><strong>Rp {{ number_format($mahasiswaGroup->sum(function($mhs) {
                            $spb = $mhs->spb1->dibayar ?? 0;
                            $spp = $mhs->spp1->dibayar ?? 0;
                            $ukt = $mhs->ukt1->dibayar ?? 0;
                            $bt = 0;
                            foreach (($mhs->biaya_tetap_list ?? []) as $nominal) {
                                $bt += $nominal;
                            }
                            return $spb + $spp + $ukt + $bt;
                        }), 0, ',', '.') }}</strong></td>
                    </tr>
                </tfoot>
            </table>
        </div>
    @endforeach

    <!-- ========== BAGIAN AKHIR LAPORAN ========== -->
    <div class="akhir-laporan">
        <!-- RINGKASAN BAGI HASIL -->
        <h3>Ringkasan Bagi Hasil</h3>
        <div class="table-wrapper ringkasan-bagi-hasil">
            <table>
                <thead>
                    <tr>
                        <th width="35%">Komponen</th>
                        <th width="20%">Total</th>
                        <th width="15%">Persen Kampus / K2</th>
                        <th width="15%">Bagi Hasil Kampus</th>
                        <th width="15%">Bagi Hasil K2</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td class="text-left"><strong>Herregistrasi</strong></td>
                        <td class="text-right">Rp {{ number_format($totalBiayaHerregistrasi, 0, ',', '.') }}</td>
                        <td class="text-center">{{ $persenKampus ?? 70 }}% / {{ $persenK2 ?? 30 }}%</td>
                        <td class="text-right bagi-kampus">Rp {{ number_format($totalBagiHasilKampus, 0, ',', '.') }}</td>
                        <td class="text-right bagi-k2">Rp {{ number_format($totalBagiHasilK2, 0, ',', '.') }}</td>
                    </tr>
                    @foreach ($totalBiayaTetapPerJenis as $jenis => $total)
                    <tr>
                        <td class="text-left"><strong>{{ $jenis }}</strong></td>
                        <td class="text-right">Rp {{ number_format($total, 0, ',', '.') }}</td>
                        <td class="text-center">100% / 0%</td>
                        <td class="text-right bagi-kampus">Rp {{ number_format($total, 0, ',', '.') }}</td>
                        <td class="text-right bagi-k2">Rp 0</td>
                    </tr>
                    @endforeach
                    <tr class="total-row">
                        <td class="text-right"><strong>TOTAL KESELURUHAN</strong></td>
                        <td class="text-right"><strong>Rp {{ number_format($totalBiayaHerregistrasi + $totalSemuaBiayaTetap, 0, ',', '.') }}</strong></td>
                        <td class="text-center"><strong>-</strong></td>
                        <td class="text-right"><strong>Rp {{ number_format($totalBagiHasilKampus + $totalSemuaBagiHasilTetapKampus, 0, ',', '.') }}</strong></td>
                        <td class="text-right"><strong>Rp {{ number_format($totalBagiHasilK2, 0, ',', '.') }}</strong></td>
                    </tr>
                </tbody>
            </table>
        </div>

        <!-- KESIMPULAN TOTAL -->
        <h3>Kesimpulan Total</h3>
        <div class="kesimpulan-detail">
            <table>
                <tbody>
                    @php
                        $persenKampus = $ringkasanData ? reset($ringkasanData)['biaya_herregistrasi']['persen_kampus'] ?? 70 : 70;
                        $persenK2 = $ringkasanData ? reset($ringkasanData)['biaya_herregistrasi']['persen_k2'] ?? 30 : 30;
                    @endphp
                    <tr>
                        <td width="45%" class="label sub-header"><strong>RINCIAN PENDAPATAN</strong></td>
                        <td width="55%" class="text-right sub-header"></td>
                    </tr>
                    <tr>
                        <td class="label">Total SPB</td>
                        <td class="text-right">Rp {{ number_format($totalSpb ?? 0, 0, ',', '.') }}</td>
                    </tr>
                    <tr>
                        <td class="label">Total SPP</td>
                        <td class="text-right">Rp {{ number_format($totalSpp ?? 0, 0, ',', '.') }}</td>
                    </tr>
                    <tr>
                        <td class="label">Total UKT</td>
                        <td class="text-right">Rp {{ number_format($totalUkt ?? 0, 0, ',', '.') }}</td>
                    </tr>
                    <tr>
                        <td class="label"><strong>Total Herregistrasi</strong></td>
                        <td class="text-right total-keseluruhan"><strong>Rp {{ number_format($totalBiayaHerregistrasi, 0, ',', '.') }}</strong></td>
                    </tr>

                    @foreach ($totalBiayaTetapPerJenis as $jenis => $total)
                    <tr>
                        <td class="label">Total {{ $jenis }}</td>
                        <td class="text-right">Rp {{ number_format($total, 0, ',', '.') }}</td>
                    </tr>
                    @endforeach

                    <tr>
                        <td class="label"><strong>Total Biaya Tetap</strong></td>
                        <td class="text-right"><strong>Rp {{ number_format($totalSemuaBiayaTetap, 0, ',', '.') }}</strong></td>
                    </tr>

                    <tr style="border-top: 2px solid #000;">
                        <td class="label"><strong>TOTAL PENDAPATAN KESELURUHAN</strong></td>
                        <td class="text-right grand-total"><strong>Rp {{ number_format($totalBiayaHerregistrasi + $totalSemuaBiayaTetap, 0, ',', '.') }}</strong></td>
                    </tr>

                    <tr>
                        <td class="label sub-header"><strong>ALOKASI BAGI HASIL</strong></td>
                        <td class="text-right sub-header"></td>
                    </tr>
                    <tr>
                        <td class="label">Hak Kampus ({{ $persenKampus }}% dari Herregistrasi + 100% Biaya Tetap)</td>
                        <td class="text-right bagi-kampus">Rp {{ number_format($totalBagiHasilKampus + $totalSemuaBagiHasilTetapKampus, 0, ',', '.') }}</td>
                    </tr>
                    <tr>
                        <td class="label">Hak K2 ({{ $persenK2 }}% dari Herregistrasi)</td>
                        <td class="text-right bagi-k2">Rp {{ number_format($totalBagiHasilK2, 0, ',', '.') }}</td>
                    </tr>
                    <tr>
                        <td class="label">PPh 23 (10% × Total Herregistrasi × 2%)</td>
                        <td class="text-right pph-row">Rp {{ number_format($pph23, 0, ',', '.') }}</td>
                    </tr>

                    <tr style="background: #fef3c7;">
                        <td class="label"><strong>GRAND TOTAL (Kampus + K2)</strong></td>
                        <td class="text-right grand-total"><strong>Rp {{ number_format(($totalBagiHasilKampus + $totalSemuaBagiHasilTetapKampus) + $totalBagiHasilK2, 0, ',', '.') }}</strong></td>
                    </tr>
                </tbody>
            </table>
        </div>

        <!-- TANDA TANGAN -->
        <div class="ttd-section">
            <div class="ttd-box">
                <p>Mengetahui,</p>
                <p><strong>Bagian Keuangan</strong></p>
                <div class="ttd-space"></div>
                <p><strong>(______________________)</strong></p>
            </div>
            <div class="clear"></div>
        </div>
    </div>

</body>

</html>
