<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="utf-8">
    <title>{{ $title ?? 'Neraca' }}</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;
        }
        .section-title {
            margin: 18px 0 8px;
            font-size: 13px;
            font-weight: 700;
            color: #111827;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 16px;
        }
        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;
        }
        .row-total td {
            font-weight: 700;
            background: #f9fafb;
        }
        .row-grand td {
            font-weight: 700;
            background: #eef2ff;
        }
        .negative {
            color: #b91c1c;
        }
        .status {
            margin-top: 8px;
            font-size: 11px;
            color: #4b5563;
        }
        .footer {
            margin-top: 18px;
            font-size: 11px;
            color: #6b7280;
            text-align: center;
        }
        @media print {
            body {
                margin: 12mm;
            }
        }
    </style>
</head>
<body>
@php
    $formatRupiah = fn ($value) => 'Rp ' . number_format((float) $value, 0, ',', '.');
    $s = $summary ?? [];

    $kasKecil = (float) ($s['kas_kecil'] ?? 0);
    $kasTunai = (float) ($s['kas_tunai'] ?? 0);
    $kasBank = (float) ($s['kas_bank'] ?? 0);
    $piutangUsaha = (float) ($s['piutang_usaha'] ?? 0);
    $persediaan = (float) ($s['persediaan'] ?? 0);
    $asetLancarLainnya = (float) ($s['aset_lancar_lainnya'] ?? 0);
    $totalAsetLancar = (float) ($s['total_aset_lancar'] ?? 0);

    $asetTetapBruto = (float) ($s['aset_tetap_bruto'] ?? 0);
    $akumulasiPenyusutan = (float) ($s['akumulasi_penyusutan'] ?? 0);
    $asetTetapBersih = (float) ($s['aset_tetap_bersih'] ?? 0);
    $totalAset = (float) ($s['total_aset'] ?? 0);

    $hutangUsaha = (float) ($s['hutang_usaha'] ?? 0);
    $utangGaji = (float) ($s['utang_gaji'] ?? 0);
    $utangLainLain = (float) ($s['utang_lain_lain'] ?? 0);
    $totalKewajiban = (float) ($s['total_kewajiban'] ?? 0);

    $modalPemilik = (float) ($s['modal_pemilik'] ?? 0);
    $prive = (float) ($s['prive'] ?? 0);
    $labaDitahan = (float) ($s['laba_ditahan'] ?? 0);
    $totalEkuitas = (float) ($s['total_ekuitas'] ?? 0);

    $totalPasiva = (float) ($s['total_pasiva'] ?? 0);
    $selisih = (float) ($s['selisih'] ?? ($totalAset - $totalPasiva));
    $isBalanced = abs($selisih) < 1;
@endphp

<div class="header">
    <div class="title">Neraca</div>
    <div class="meta">Posisi per: {{ \Carbon\Carbon::parse($asOfDate)->format('d M Y') }}</div>
    <div class="meta">Status: {{ $isBalanced ? 'Balance' : 'Belum Balance' }}</div>
    <div class="meta">Dicetak: {{ now()->format('d/m/Y H:i') }}</div>
</div>

<div class="summary">
    <div class="card">
        <div class="card-title">Total Aset</div>
        <div class="card-value">{{ $formatRupiah($totalAset) }}</div>
    </div>
    <div class="card">
        <div class="card-title">Total Kewajiban</div>
        <div class="card-value">{{ $formatRupiah($totalKewajiban) }}</div>
    </div>
    <div class="card">
        <div class="card-title">Total Ekuitas</div>
        <div class="card-value">{{ $formatRupiah($totalEkuitas) }}</div>
    </div>
    <div class="card">
        <div class="card-title">Selisih</div>
        <div class="card-value {{ !$isBalanced ? 'negative' : '' }}">{{ $formatRupiah($selisih) }}</div>
    </div>
</div>

<div class="section-title">Aset Lancar</div>
<table>
    <thead>
        <tr>
            <th>Uraian</th>
            <th style="width: 180px;" class="text-right">Nilai</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Kas Kecil</td>
            <td class="text-right">{{ $formatRupiah($kasKecil) }}</td>
        </tr>
        <tr>
            <td>Kas Tunai</td>
            <td class="text-right">{{ $formatRupiah($kasTunai) }}</td>
        </tr>
        <tr>
            <td>Kas Bank</td>
            <td class="text-right">{{ $formatRupiah($kasBank) }}</td>
        </tr>
        <tr>
            <td>Piutang Usaha</td>
            <td class="text-right">{{ $formatRupiah($piutangUsaha) }}</td>
        </tr>
        <tr>
            <td>Persediaan</td>
            <td class="text-right">{{ $formatRupiah($persediaan) }}</td>
        </tr>
        <tr>
            <td>Aset Lancar Lainnya</td>
            <td class="text-right">{{ $formatRupiah($asetLancarLainnya) }}</td>
        </tr>
        <tr class="row-total">
            <td>Total Aset Lancar</td>
            <td class="text-right">{{ $formatRupiah($totalAsetLancar) }}</td>
        </tr>
    </tbody>
</table>

<div class="section-title">Aset Tetap</div>
<table>
    <thead>
        <tr>
            <th>Uraian</th>
            <th style="width: 180px;" class="text-right">Nilai</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Aset Tetap Bruto</td>
            <td class="text-right">{{ $formatRupiah($asetTetapBruto) }}</td>
        </tr>
        <tr>
            <td>Akumulasi Penyusutan</td>
            <td class="text-right negative">({{ $formatRupiah($akumulasiPenyusutan) }})</td>
        </tr>
        <tr class="row-total">
            <td>Aset Tetap Bersih</td>
            <td class="text-right">{{ $formatRupiah($asetTetapBersih) }}</td>
        </tr>
        <tr class="row-grand">
            <td>TOTAL ASET</td>
            <td class="text-right">{{ $formatRupiah($totalAset) }}</td>
        </tr>
    </tbody>
</table>

<div class="section-title">Kewajiban</div>
<table>
    <thead>
        <tr>
            <th>Uraian</th>
            <th style="width: 180px;" class="text-right">Nilai</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Hutang Usaha</td>
            <td class="text-right">{{ $formatRupiah($hutangUsaha) }}</td>
        </tr>
        <tr>
            <td>Utang Gaji</td>
            <td class="text-right">{{ $formatRupiah($utangGaji) }}</td>
        </tr>
        <tr>
            <td>Utang Lain-lain</td>
            <td class="text-right">{{ $formatRupiah($utangLainLain) }}</td>
        </tr>
        <tr class="row-total">
            <td>Total Kewajiban</td>
            <td class="text-right">{{ $formatRupiah($totalKewajiban) }}</td>
        </tr>
    </tbody>
</table>

<div class="section-title">Ekuitas</div>
<table>
    <thead>
        <tr>
            <th>Uraian</th>
            <th style="width: 180px;" class="text-right">Nilai</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Modal Pemilik</td>
            <td class="text-right">{{ $formatRupiah($modalPemilik) }}</td>
        </tr>
        <tr>
            <td>Prive</td>
            <td class="text-right negative">({{ $formatRupiah($prive) }})</td>
        </tr>
        <tr>
            <td>Laba Ditahan</td>
            <td class="text-right {{ $labaDitahan < 0 ? 'negative' : '' }}">{{ $formatRupiah($labaDitahan) }}</td>
        </tr>
        <tr class="row-total">
            <td>Total Ekuitas</td>
            <td class="text-right">{{ $formatRupiah($totalEkuitas) }}</td>
        </tr>
        <tr class="row-grand">
            <td>TOTAL KEWAJIBAN + EKUITAS</td>
            <td class="text-right">{{ $formatRupiah($totalPasiva) }}</td>
        </tr>
    </tbody>
</table>

<div class="status">
    Catatan: Neraca dianggap balance jika Total Aset = Total Kewajiban + Ekuitas.
</div>

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

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