<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="utf-8">
    <title>{{ $title ?? 'Laporan Buku Besar' }}</title>
    <style>
        body {
            font-family: Arial, Helvetica, sans-serif;
            color: #111827;
            font-size: 12px;
            margin: 24px;
        }
        .header {
            margin-bottom: 18px;
        }
        .title {
            font-size: 20px;
            font-weight: 700;
            margin-bottom: 4px;
        }
        .meta {
            font-size: 12px;
            color: #4b5563;
            margin-bottom: 2px;
        }
        .summary {
            margin: 16px 0;
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 10px;
        }
        .card {
            border: 1px solid #d1d5db;
            padding: 10px;
            border-radius: 8px;
        }
        .card-title {
            font-size: 11px;
            color: #6b7280;
            margin-bottom: 6px;
        }
        .card-value {
            font-size: 16px;
            font-weight: 700;
        }
        table {
            width: 100%;
            border-collapse: collapse;
        }
        th, td {
            border: 1px solid #d1d5db;
            padding: 8px;
            vertical-align: top;
        }
        th {
            background: #f3f4f6;
            text-align: left;
            font-size: 11px;
        }
        .text-right {
            text-align: right;
        }
        .text-center {
            text-align: center;
        }
        .muted {
            color: #6b7280;
        }
        .footer {
            margin-top: 18px;
            font-size: 11px;
            color: #6b7280;
            text-align: center;
        }
        @media print {
            body {
                margin: 12mm;
            }
        }
    </style>
</head>
<body>
@php
    $transactionsCol = collect($transactions ?? []);
    $accountsCol = collect($accounts ?? []);

    $totalDebit = $transactionsCol->sum('debit');
    $totalKredit = $transactionsCol->sum('kredit');
    $saldoAkhir = data_get($transactionsCol->last(), 'saldo', $saldoAwal ?? 0);

    $isSingleDay = ($startDate === $endDate);

    $periodLabel = $isSingleDay
        ? \Carbon\Carbon::parse($startDate)->format('d M Y')
        : \Carbon\Carbon::parse($startDate)->format('d M Y') . ' s/d ' . \Carbon\Carbon::parse($endDate)->format('d M Y');

    $selectedAccount = request('payment_account_id');
    $selectedAccountLabel = 'Semua Rekening';

    if ($selectedAccount !== null && $selectedAccount !== '') {
        $selectedAccountLabel = $accountsCol->first(function ($acc) use ($selectedAccount) {
            return (string) data_get($acc, 'id') === (string) $selectedAccount;
        })->name ?? 'Rekening Terpilih';
    }
@endphp

<div class="header">
    <div class="title">Laporan Buku Besar</div>
    <div class="meta">Periode: {{ $periodLabel }}</div>
    <div class="meta">Rekening: {{ $selectedAccountLabel }}</div>
    <div class="meta">Dicetak: {{ now()->format('d/m/Y H:i') }}</div>
</div>

<div class="summary">
    <div class="card">
        <div class="card-title">Saldo Awal</div>
        <div class="card-value">Rp {{ number_format($saldoAwal ?? 0, 0, ',', '.') }}</div>
    </div>
    <div class="card">
        <div class="card-title">Total Debit</div>
        <div class="card-value">Rp {{ number_format($totalDebit ?? 0, 0, ',', '.') }}</div>
    </div>
    <div class="card">
        <div class="card-title">Total Kredit</div>
        <div class="card-value">Rp {{ number_format($totalKredit ?? 0, 0, ',', '.') }}</div>
    </div>
    <div class="card">
        <div class="card-title">Saldo Akhir</div>
        <div class="card-value">Rp {{ number_format($saldoAkhir ?? 0, 0, ',', '.') }}</div>
    </div>
</div>

<table>
    <thead>
        <tr>
            <th style="width: 130px;">Tanggal</th>
            <th>Keterangan</th>
            <th style="width: 160px;">Customer</th>
            <th style="width: 130px;">Rekening</th>
            <th style="width: 120px;" class="text-right">Debit</th>
            <th style="width: 120px;" class="text-right">Kredit</th>
            <th style="width: 120px;" class="text-right">Saldo</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="6"><strong>Saldo Awal</strong></td>
            <td class="text-right"><strong>Rp {{ number_format($saldoAwal ?? 0, 0, ',', '.') }}</strong></td>
        </tr>

        @forelse($transactionsCol as $trx)
            @php
                $debit = (float) ($trx['debit'] ?? 0);
                $kredit = (float) ($trx['kredit'] ?? 0);
                $saldo = (float) ($trx['saldo'] ?? 0);
                $tanggal = $trx['tanggal'] ?? null;
            @endphp
            <tr>
                <td>{{ $tanggal ? \Carbon\Carbon::parse($tanggal)->format('d/m/Y H:i') : '-' }}</td>
                <td>{{ $trx['keterangan'] ?? '-' }}</td>
                <td>{{ $trx['customer'] ?? '-' }}</td>
                <td>{{ $trx['rekening'] ?? '-' }}</td>
                <td class="text-right">{{ $debit > 0 ? 'Rp ' . number_format($debit, 0, ',', '.') : '-' }}</td>
                <td class="text-right">{{ $kredit > 0 ? 'Rp ' . number_format($kredit, 0, ',', '.') : '-' }}</td>
                <td class="text-right"><strong>Rp {{ number_format($saldo, 0, ',', '.') }}</strong></td>
            </tr>
        @empty
            <tr>
                <td colspan="7" class="text-center muted">Tidak ada data pada filter ini.</td>
            </tr>
        @endforelse

        <tr>
            <td colspan="4"><strong>{{ $isSingleDay ? 'Total Hari Ini' : 'Total Periode' }}</strong></td>
            <td class="text-right"><strong>Rp {{ number_format($totalDebit ?? 0, 0, ',', '.') }}</strong></td>
            <td class="text-right"><strong>Rp {{ number_format($totalKredit ?? 0, 0, ',', '.') }}</strong></td>
            <td class="text-right"><strong>Rp {{ number_format($saldoAkhir ?? 0, 0, ',', '.') }}</strong></td>
        </tr>
    </tbody>
</table>

<div class="footer">
    © {{ date('Y') }} Rio Ardiansyah - aplikasipercetakan.com
</div>

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