<?php helper(['letter', 'logo']); ?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Detail Tabungan Siswa</title>
    <style>
        body { font-family: Arial, sans-serif; font-size: 11px; padding: 0; margin: 0; }
        .content { padding: 20px; }
        h2 { text-align: center; font-size: 16px; margin-bottom: 4px; }
        .sub-title { text-align: center; font-size: 12px; color: #555; margin-bottom: 20px; }
        .info-table { width: 100%; margin-bottom: 20px; font-size: 11px; }
        .info-table td { padding: 3px 0; vertical-align: top; }
        .info-table td:first-child { width: 120px; }
        table.data { width: 100%; border-collapse: collapse; margin-bottom: 20px; }
        table.data thead { display: table-header-group; }
        table.data tr { page-break-inside: avoid; }
        table.data th, table.data td { border: 1px solid #000; padding: 5px 8px; text-align: left; }
        table.data th { background: #f0f0f0; font-weight: bold; text-align: center; }
        table.data td.num { text-align: right; }
        table.data td.center { text-align: center; }
        .summary { width: 100%; margin-top: 20px; border-collapse: collapse; }
        .summary td { border: 1px solid #000; padding: 8px; font-weight: bold; }
        .summary td.label { width: 60%; }
        .summary td.value { width: 40%; text-align: right; }
        .footer { margin-top: 40px; text-align: right; font-size: 11px; }
        .signature-box { display: inline-block; text-align: center; }
        .signature-box .name { margin-top: 50px; font-weight: bold; }
        @media print {
            body { padding: 0; }
            .no-print { display: none; }
        }
    </style>
</head>
<body>

<?= view('letter/_kop', ['schoolSettings' => $schoolSettings, 'letterSettings' => $letterSettings]) ?>

<div class="content">
    <h2>DETAIL TABUNGAN SISWA</h2>
    <p class="sub-title">Periode <?= esc($period['year_name']) ?> — <?= esc($period['semester']) ?></p>

    <table class="info-table">
        <tr>
            <td>Nama</td>
            <td>: <?= esc($student['full_name']) ?></td>
        </tr>
        <tr>
            <td>NIS</td>
            <td>: <?= esc($student['nis'] ?? '-') ?></td>
        </tr>
        <tr>
            <td>Kelas</td>
            <td>: <?= esc($classroom['classroom_name'] ?? '-') ?> <?= esc($classroom['grade_name'] ?? '') ?></td>
        </tr>
        <tr>
            <td>Periode</td>
            <td>: <?= esc($period['year_name']) ?> — <?= esc($period['semester']) ?></td>
        </tr>
    </table>

    <table class="data">
        <thead>
            <tr>
                <th style="width:30px">No</th>
                <th>Tanggal</th>
                <th style="width:80px">Tipe</th>
                <th>Setoran (Rp)</th>
                <th>Penarikan (Rp)</th>
                <th>Saldo (Rp)</th>
                <th>Keterangan</th>
                <th>Petugas</th>
            </tr>
        </thead>
        <tbody>
            <?php if (empty($transactions)): ?>
            <tr>
                <td colspan="8" class="center">Belum ada transaksi</td>
            </tr>
            <?php else: ?>
            <?php $no = 1; foreach ($transactions as $t): ?>
            <tr>
                <td class="center"><?= $no++ ?></td>
                <td><?= date('d/m/Y', strtotime($t['transaction_date'])) ?></td>
                <td class="center"><?= $t['transaction_type'] === 'setoran' ? 'Setoran' : 'Penarikan' ?></td>
                <td class="num"><?= $t['transaction_type'] === 'setoran' ? number_format($t['amount'], 0, ',', '.') : '-' ?></td>
                <td class="num"><?= $t['transaction_type'] === 'penarikan' ? number_format($t['amount'], 0, ',', '.') : '-' ?></td>
                <td class="num"><?= number_format($t['balance_after'], 0, ',', '.') ?></td>
                <td><?= esc($t['notes'] ?? '-') ?></td>
                <td><?= esc($t['recorded_by_name'] ?? '-') ?></td>
            </tr>
            <?php endforeach; ?>
            <?php endif; ?>
        </tbody>
    </table>

    <table class="summary">
        <tr>
            <td class="label">Total Setoran</td>
            <td class="value">Rp <?= number_format($totals['setoran'], 0, ',', '.') ?></td>
        </tr>
        <tr>
            <td class="label">Total Penarikan</td>
            <td class="value">Rp <?= number_format($totals['penarikan'], 0, ',', '.') ?></td>
        </tr>
        <tr>
            <td class="label">Saldo Akhir</td>
            <td class="value">Rp <?= number_format($totals['balance'], 0, ',', '.') ?></td>
        </tr>
    </table>

    <div class="footer">
        <div class="signature-box">
            <div>Petugas Tabungan</div>
            <div class="name"><?= esc($tabunganOfficerName ?: 'Belum ditentukan') ?></div>
        </div>
    </div>
</div>

</body>
</html>
