<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Laporan Alokasi Pendaftaran</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;
        }

        .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 PENDAFTARAN</h1>
        <p>SIMA - Sistem Informasi Mahasiswa</p>
        <p>Tanggal Cetak: <?php echo e($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%">: <?php echo e(auth()->user()->name ?? 'System'); ?></td>
                <td width="20%"><strong>Total Mahasiswa</strong></td>
                <td width="30%">: <?php echo e(number_format($totalPendaftar)); ?></td>
            </tr>
            <?php if($filter['tahun_ajaran'] ?? null): ?>
            <tr>
                <td><strong>Tahun Ajaran</strong></td>
                <td>: <?php echo e($filter['tahun_ajaran']); ?></td>
                <td><strong>Periode</strong></td>
                <td>: <?php echo e($startDate ?? ''); ?> s/d <?php echo e($endDate ?? ''); ?></td>
            </tr>
            <?php endif; ?>
            <?php if($filter['kampus_id'] ?? null): ?>
            <tr>
                <td><strong>Kampus</strong></td>
                <td>: <?php echo e(\App\Models\Kampus::find($filter['kampus_id'])?->nama ?? '-'); ?></td>
                <td><strong>Prodi</strong></td>
                <td>: <?php echo e(\App\Models\Prodi::find($filter['prodi_id'])?->nama ?? '-'); ?></td>
            </tr>
            <?php 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 Biaya Formulir</th>
                    <th>Bagi Kampus</th>
                    <th>Bagi K2</th>
                </tr>
            </thead>
            <tbody>
                <?php $no = 1; ?>
                <?php $__currentLoopData = $ringkasanBagiHasil; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <tr>
                    <td class="text-center"><?php echo e($no++); ?></td>
                    <td class="text-left"><?php echo e($item['kampus']->nama); ?></td>
                    <td class="text-center"><?php echo e(number_format($item['jumlah_pendaftar'])); ?></td>
                    <td class="text-right">Rp <?php echo e(number_format($item['total_biaya'], 0, ',', '.')); ?></td>
                    <td class="text-right bagi-kampus">Rp <?php echo e(number_format($item['bagi_hasil_kampus'], 0, ',', '.')); ?></td>
                    <td class="text-right bagi-k2">Rp <?php echo e(number_format($item['bagi_hasil_k2'], 0, ',', '.')); ?></td>
                </tr>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                <tr class="total-row">
                    <td colspan="3" class="text-right"><strong>TOTAL</strong></td>
                    <td class="text-right"><strong>Rp <?php echo e(number_format($totalBiayaFormulir, 0, ',', '.')); ?></strong></td>
                    <td class="text-right"><strong>Rp <?php echo e(number_format($totalBagiHasilKampus, 0, ',', '.')); ?></strong></td>
                    <td class="text-right"><strong>Rp <?php echo e(number_format($totalBagiHasilK2, 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> Biaya Formulir = Biaya Pendaftaran
    </div>

    <?php
        $groupedByTahun = $mahasiswas->groupBy('tahun_ajaran');
        $sortedTahun = $groupedByTahun->keys()->sortDesc();
    ?>

    <?php $__currentLoopData = $sortedTahun; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $tahun): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
        <?php $mahasiswaGroup = $groupedByTahun[$tahun]; ?>
        <h4>Tahun Ajaran: <?php echo e($tahun ?? 'Tidak Diketahui'); ?> (<?php echo e($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>
                        <th>Jenjang</th>
                        <th>Biaya Formulir</th>
                        <th>Tgl Daftar</th>
                    </tr>
                </thead>
                <tbody>
                    <?php $__currentLoopData = $mahasiswaGroup; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index => $mhs): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <?php
                            $biayaFormulir = 0;
                            $prodi = $mhs->prodi;
                            if ($prodi && $prodi->jenjang) {
                                $biayaFormulirKampus = json_decode($mhs->kampus->biaya_formulir ?? '{}', true);
                                if (isset($biayaFormulirKampus[$prodi->jenjang])) {
                                    $data = $biayaFormulirKampus[$prodi->jenjang];
                                    $biayaFormulir = is_array($data) ? ($data['total'] ?? 0) : ($data ?? 0);
                                }
                            }
                        ?>
                        <tr>
                            <td class="text-center"><?php echo e($index + 1); ?></td>
                            <td class="text-center"><?php echo e($mhs->nim ?? '-'); ?></td>
                            <td class="text-left"><?php echo e($mhs->nama ?? '-'); ?></td>
                            <td class="text-left"><?php echo e($mhs->kampus->nama ?? '-'); ?></td>
                            <td class="text-left"><?php echo e($mhs->prodi->nama ?? '-'); ?></td>
                            <td class="text-center"><?php echo e($mhs->prodi->jenjang ?? '-'); ?></td>
                            <td class="text-right"><?php echo e($biayaFormulir > 0 ? 'Rp ' . number_format($biayaFormulir, 0, ',', '.') : '-'); ?></td>
                            <td class="text-center"><?php echo e(\Carbon\Carbon::parse($mhs->created_at)->format('d/m/Y') ?? '-'); ?></td>
                        </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </tbody>
                <?php
                    $totalColspan = 6;
                ?>
                <tfoot style="background: #f3f4f6; font-weight: bold;">
                    <tr class="total-row">
                        <td colspan="<?php echo e($totalColspan); ?>" class="text-right"><strong>TOTAL</strong></td>
                        <td class="text-right"><strong>Rp <?php echo e(number_format($mahasiswaGroup->sum(function($mhs) {
                            $biayaFormulir = 0;
                            $prodi = $mhs->prodi;
                            if ($prodi && $prodi->jenjang) {
                                $biayaFormulirKampus = json_decode($mhs->kampus->biaya_formulir ?? '{}', true);
                                if (isset($biayaFormulirKampus[$prodi->jenjang])) {
                                    $data = $biayaFormulirKampus[$prodi->jenjang];
                                    $biayaFormulir = is_array($data) ? ($data['total'] ?? 0) : ($data ?? 0);
                                }
                            }
                            return $biayaFormulir;
                        }), 0, ',', '.')); ?></strong></td>
                        <td class="text-center">-</td>
                    </tr>
                </tfoot>
            </table>
        </div>
    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

    <!-- ========== BAGIAN AKHIR LAPORAN ========== -->
    <div class="akhir-laporan">
        <!-- RINGKASAN BAGI HASIL PER JENJANG -->
        <h3>Ringkasan Bagi Hasil</h3>
        <div class="table-wrapper ringkasan-bagi-hasil">
            <table>
                <thead>
                    <tr>
                        <th width="30%">Komponen</th>
                        <th width="15%">Total</th>
                        <th width="15%">Persen Kampus</th>
                        <th width="15%">Persen K2</th>
                        <th width="15%">Bagi Hasil Kampus</th>
                        <th width="15%">Bagi Hasil K2</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                        // Kumpulkan semua jenjang dari semua kampus
                        $allJenjang = [];
                        foreach ($ringkasanBagiHasil as $item) {
                            foreach ($item['detail_jenjang'] as $jenjang => $detail) {
                                if (!isset($allJenjang[$jenjang])) {
                                    $allJenjang[$jenjang] = [
                                        'total_biaya' => 0,
                                        'total_bagi_kampus' => 0,
                                        'total_bagi_k2' => 0,
                                        'persen_kampus' => $detail['persen_kampus'],
                                        'persen_k2' => $detail['persen_k2']
                                    ];
                                }
                                $allJenjang[$jenjang]['total_biaya'] += $detail['total_biaya'];
                                $allJenjang[$jenjang]['total_bagi_kampus'] += ($detail['total_biaya'] * $detail['persen_kampus']) / 100;
                                $allJenjang[$jenjang]['total_bagi_k2'] += ($detail['total_biaya'] * $detail['persen_k2']) / 100;
                            }
                        }
                    ?>

                    <?php $__currentLoopData = $allJenjang; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $jenjang => $data): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <?php if($data['total_biaya'] > 0): ?>
                        <tr>
                            <td class="text-left"><strong>Biaya Formulir - <?php echo e($jenjang); ?></strong></td>
                            <td class="text-right">Rp <?php echo e(number_format($data['total_biaya'], 0, ',', '.')); ?></td>
                            <td class="text-center"><?php echo e($data['persen_kampus']); ?>%</td>
                            <td class="text-center"><?php echo e($data['persen_k2']); ?>%</td>
                            <td class="text-right bagi-kampus">Rp <?php echo e(number_format($data['total_bagi_kampus'], 0, ',', '.')); ?></td>
                            <td class="text-right bagi-k2">Rp <?php echo e(number_format($data['total_bagi_k2'], 0, ',', '.')); ?></td>
                        </tr>
                        <?php endif; ?>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                    <tr class="total-row">
                        <td class="text-right"><strong>TOTAL KESELURUHAN</strong></td>
                        <td class="text-right"><strong>Rp <?php echo e(number_format($totalBiayaFormulir, 0, ',', '.')); ?></strong></td>
                        <td class="text-center"><strong>-</strong></td>
                        <td class="text-center"><strong>-</strong></td>
                        <td class="text-right"><strong>Rp <?php echo e(number_format($totalBagiHasilKampus, 0, ',', '.')); ?></strong></td>
                        <td class="text-right"><strong>Rp <?php echo e(number_format($totalBagiHasilK2, 0, ',', '.')); ?></strong></td>
                    </tr>
                </tbody>
            </table>
        </div>

        <!-- KESIMPULAN TOTAL -->
        <h3>Kesimpulan Total</h3>
        <div class="kesimpulan-detail">
            <table>
                <tbody>
                    <tr class="label sub-header">
                        <td width="45%"><strong>RINCIAN PENDAPATAN</strong></td>
                        <td width="55%" class="text-right"><strong>JUMLAH</strong></td>
                    </tr>
                    <tr>
                        <td class="label">Total Biaya Formulir</td>
                        <td class="text-right total-keseluruhan"><strong>Rp <?php echo e(number_format($totalBiayaFormulir, 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 <?php echo e(number_format($totalBiayaFormulir, 0, ',', '.')); ?></strong></td>
                    </tr>

                    <tr>
                        <td class="label sub-header"><strong>ALOKASI BAGI HASIL</strong></td>
                        <td class="text-right sub-header"></td>
                    </tr>

                    <?php $__currentLoopData = $allJenjang; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $jenjang => $data): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <?php if($data['total_biaya'] > 0): ?>
                        <tr>
                            <td class="label">Hak Kampus - <?php echo e($jenjang); ?> (<?php echo e($data['persen_kampus']); ?>% dari Biaya Formulir)</td>
                            <td class="text-right bagi-kampus">Rp <?php echo e(number_format($data['total_bagi_kampus'], 0, ',', '.')); ?></td>
                        </tr>
                        <tr>
                            <td class="label">Hak K2 - <?php echo e($jenjang); ?> (<?php echo e($data['persen_k2']); ?>% dari Biaya Formulir)</td>
                            <td class="text-right bagi-k2">Rp <?php echo e(number_format($data['total_bagi_k2'], 0, ',', '.')); ?></td>
                        </tr>
                        <?php endif; ?>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                    <tr class="total-row">
                        <td class="label"><strong>TOTAL BAGI HASIL KAMPUS</strong></td>
                        <td class="text-right bagi-kampus"><strong>Rp <?php echo e(number_format($totalBagiHasilKampus, 0, ',', '.')); ?></strong></td>
                    </tr>
                    <tr class="total-row">
                        <td class="label"><strong>TOTAL BAGI HASIL K2</strong></td>
                        <td class="text-right bagi-k2"><strong>Rp <?php echo e(number_format($totalBagiHasilK2, 0, ',', '.')); ?></strong></td>
                    </tr>

                    <tr style="background: #fef3c7;">
                        <td class="label"><strong>GRAND TOTAL (Kampus + K2)</strong></td>
                        <td class="text-right grand-total"><strong>Rp <?php echo e(number_format($totalBagiHasilKampus + $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>
<?php /**PATH C:\Users\User\Documents\K2-PROJECTS\k2-sipema\resources\views/laporan/pdf/alokasi-pendaftaran.blade.php ENDPATH**/ ?>