<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>{{ $title ?? 'Jurnal Umum' }}</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            color: #111827;
            font-size: 12px;
            margin: 24px;
        }
        .header {
            margin-bottom: 18px;
            text-align: center;
        }
        .title {
            font-size: 20px;
            font-weight: 700;
            margin-bottom: 4px;
        }
        .meta {
            font-size: 12px;
            color: #6b7280;
        }
        .summary {
            margin: 14px 0 16px;
            font-size: 12px;
        }
        .summary span {
            display: inline-block;
            margin-right: 18px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
        }
        th, td {
            border: 1px solid #d1d5db;
            padding: 8px 10px;
        }
        th {
            background: #f3f4f6;
            text-align: left;
            font-size: 11px;
        }
        .text-right {
            text-align: right;
        }
        .indent {
            padding-left: 28px;
        }
        tfoot td {
            font-weight: 700;
            background: #f9fafb;
        }
    </style>
</head>
<body>
    @php
        $formatRupiah = fn ($value) => 'Rp ' . number_format((float) $value, 0, ',', '.');
        $entries = $entries ?? collect();
        $summary = $summary ?? [];
        $lastGroup = null;
    @endphp

    <div class="header">
        <div class="title">JURNAL UMUM</div>
        <div class="meta">Periode {{ \Carbon\Carbon::parse($startDate)->format('d/m/Y') }} s.d. {{ \Carbon\Carbon::parse($endDate)->format('d/m/Y') }}</div>
    </div>

    <div class="summary">
        <span>Total Jurnal: <strong>{{ number_format((int) ($summary['total_journals'] ?? 0), 0, ',', '.') }}</strong></span>
        <span>Total Debit: <strong>{{ $formatRupiah($summary['total_debit'] ?? 0) }}</strong></span>
        <span>Total Kredit: <strong>{{ $formatRupiah($summary['total_kredit'] ?? 0) }}</strong></span>
    </div>

    <table>
        <thead>
            <tr>
                <th style="width: 100px;">Tanggal</th>
                <th style="width: 110px;">Ref</th>
                <th>Keterangan</th>
                <th style="width: 240px;">Akun</th>
                <th style="width: 140px;" class="text-right">Debit</th>
                <th style="width: 140px;" class="text-right">Kredit</th>
            </tr>
        </thead>
        <tbody>
            @forelse($entries as $entry)
                <tr>
                    <td>{{ \Carbon\Carbon::parse($entry['tanggal'])->format('d/m/Y') }}</td>
                    <td>{{ $entry['reference'] }}</td>
                    <td>{{ $entry['description'] }}</td>
                    <td class="{{ ($entry['debit'] ?? 0) == 0 ? 'indent' : '' }}">{{ $entry['account'] }}</td>
                    <td class="text-right">{{ ($entry['debit'] ?? 0) > 0 ? $formatRupiah($entry['debit']) : '-' }}</td>
                    <td class="text-right">{{ ($entry['kredit'] ?? 0) > 0 ? $formatRupiah($entry['kredit']) : '-' }}</td>
                </tr>
            @empty
                <tr>
                    <td colspan="6" style="text-align:center;">Tidak ada data</td>
                </tr>
            @endforelse
        </tbody>
        @if($entries->isNotEmpty())
            <tfoot>
                <tr>
                    <td colspan="4" class="text-right">Total</td>
                    <td class="text-right">{{ $formatRupiah($summary['total_debit'] ?? 0) }}</td>
                    <td class="text-right">{{ $formatRupiah($summary['total_kredit'] ?? 0) }}</td>
                </tr>
            </tfoot>
        @endif
    </table>

    <script>
        window.addEventListener('load', function () {
            window.print();
        });
    </script>
</body>
</html>