<?php helper(['letter', 'logo']); ?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Laporan Tabungan Siswa</title>
    <style>
        * { margin:0; padding:0; box-sizing:border-box }
        body { font-family:Arial,sans-serif; font-size:9px; padding:0 }
        .content { padding:0 }
        .header { text-align:center; margin-bottom:20px }
        .header h2 { font-size:15px; margin-bottom:4px }
        .header p { font-size:10px; color:#555 }
        .info { margin-bottom:14px; display:flex; justify-content:space-between }
        .info table { font-size:10px }
        .info table td { padding:2px 8px 2px 0 }
        table.data { width:100%; border-collapse:collapse; margin-bottom:14px; }
        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:4px 5px; text-align:left }
        table.data th { background:#f0f0f0; font-weight:bold; font-size:8px; text-align:center }
        table.data td.num { text-align:right }
        table.data td.center { text-align:center }
        .total-row { font-weight:bold; background:#f9f9f9 }
        .footer { text-align:right; margin-top:28px; font-size:10px; page-break-inside:avoid }
        .signature-box { display:inline-block; width:180px; text-align:center; vertical-align:top }
        .signature-box .space { height:48px }
        .signature-box .name { font-weight:bold; border-top:1px solid #000; padding-top:3px }
        @media print {
            body { padding:20px }
            .no-print { display:none }
        }
    </style>
</head>
<body>

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

<div class="content">
<div class="header">
    <h2>LAPORAN TABUNGAN SISWA</h2>
    <p><?= esc($activePeriod['year_name']) ?> — <?= esc($activePeriod['semester']) ?></p>
    <?php if ($classroom): ?>
    <p>Kelas: <?= esc($classroom['grade_name']) ?> <?= esc($classroom['name']) ?></p>
    <?php endif; ?>
</div>

<div class="info">
    <table>
        <tr><td>Periode</td><td>: <?= esc($activePeriod['year_name']) ?> / <?= esc($activePeriod['semester']) ?></td></tr>
        <tr><td>Tanggal Cetak</td><td>: <?= date('d/m/Y') ?></td></tr>
    </table>
</div>

<table class="data">
    <thead>
        <tr>
            <th style="width:30px">No</th>
            <th>NIS</th>
            <th>Nama Siswa</th>
            <th>Kelas</th>
            <th>Total Setoran (Rp)</th>
            <th>Total Penarikan (Rp)</th>
            <th>Saldo (Rp)</th>
        </tr>
    </thead>
    <tbody>
        <?php if (empty($report)): ?>
        <tr>
            <td colspan="7" class="center">Belum ada data tabungan</td>
        </tr>
        <?php else: ?>
        <?php $no = 1; $totalSetoran = 0; $totalPenarikan = 0; $totalSaldo = 0; ?>
        <?php foreach ($report as $r): ?>
        <?php $totalSetoran += $r['total_setoran']; $totalPenarikan += $r['total_penarikan']; $totalSaldo += $r['balance']; ?>
        <tr>
            <td class="center"><?= $no++ ?></td>
            <td><?= esc($r['nis']) ?></td>
            <td><?= esc($r['full_name']) ?></td>
            <td><?= esc($r['classroom_name']) ?></td>
            <td class="num"><?= number_format($r['total_setoran'], 0, ',', '.') ?></td>
            <td class="num"><?= number_format($r['total_penarikan'], 0, ',', '.') ?></td>
            <td class="num"><?= number_format($r['balance'], 0, ',', '.') ?></td>
        </tr>
        <?php endforeach; ?>
        <tr class="total-row">
            <td colspan="4" class="center">TOTAL</td>
            <td class="num"><?= number_format($totalSetoran, 0, ',', '.') ?></td>
            <td class="num"><?= number_format($totalPenarikan, 0, ',', '.') ?></td>
            <td class="num"><?= number_format($totalSaldo, 0, ',', '.') ?></td>
        </tr>
        <?php endif; ?>
    </tbody>
</table>

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

</div>

</body>
</html>
