<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>

<div class="card">
    <div class="card-header d-flex align-items-center justify-content-between flex-wrap gap-2">
        <div class="d-flex align-items-center gap-2">
            <i class="bi bi-piggy-bank text-primary"></i>
            <span class="fw-semibold">Detail Tabungan Siswa</span>
        </div>
        <div class="d-flex gap-2">
            <a href="/tabungan/print-detail/<?= $student['id'] ?>?period_id=<?= $period['id'] ?>" target="_blank" class="btn btn-sm btn-outline-primary">
                <i class="bi bi-printer me-1"></i>Cetak PDF
            </a>
            <a href="/tabungan/report" class="btn btn-sm btn-outline-secondary">
                <i class="bi bi-arrow-left me-1"></i>Kembali
            </a>
        </div>
    </div>
    <div class="card-body">
        <form method="get" class="row g-2 mb-4">
            <div class="col-md-5">
                <label class="form-label small text-muted mb-1">Periode Akademik</label>
                <select name="period_id" class="form-select form-select-sm" onchange="this.form.submit()">
                    <?php foreach ($periods as $p): ?>
                    <option value="<?= $p['id'] ?>" <?= ($period['id'] == $p['id']) ? 'selected' : '' ?>>
                        <?= esc($p['year_name']) ?> — <?= esc($p['semester']) ?>
                    </option>
                    <?php endforeach; ?>
                </select>
            </div>
        </form>

        <div class="row g-3 mb-4">
            <div class="col-md-6">
                <div class="card border-0 shadow-sm h-100">
                    <div class="card-body">
                        <h6 class="text-muted mb-3">Informasi Siswa</h6>
                        <table class="table table-sm table-borderless mb-0">
                            <tr>
                                <td style="width:120px">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>
                    </div>
                </div>
            </div>
            <div class="col-md-6">
                <div class="row g-2">
                    <div class="col-12">
                        <div class="card border-0 shadow-sm bg-success bg-opacity-10">
                            <div class="card-body py-2">
                                <div class="small text-success">Total Setoran</div>
                                <div class="fw-bold text-success">Rp <?= number_format($totals['setoran'], 0, ',', '.') ?></div>
                            </div>
                        </div>
                    </div>
                    <div class="col-12">
                        <div class="card border-0 shadow-sm bg-danger bg-opacity-10">
                            <div class="card-body py-2">
                                <div class="small text-danger">Total Penarikan</div>
                                <div class="fw-bold text-danger">Rp <?= number_format($totals['penarikan'], 0, ',', '.') ?></div>
                            </div>
                        </div>
                    </div>
                    <div class="col-12">
                        <div class="card border-0 shadow-sm bg-primary bg-opacity-10">
                            <div class="card-body py-2">
                                <div class="small text-primary">Saldo Akhir</div>
                                <div class="fw-bold text-primary">Rp <?= number_format($totals['balance'], 0, ',', '.') ?></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <h6 class="fw-semibold mb-3">Riwayat Transaksi</h6>
        <div class="table-responsive">
            <table class="table table-sm table-hover table-bordered">
                <thead class="table-light">
                    <tr>
                        <th class="text-center" style="width:40px">No</th>
                        <th>Tanggal</th>
                        <th>Tipe</th>
                        <th class="text-end">Setoran (Rp)</th>
                        <th class="text-end">Penarikan (Rp)</th>
                        <th class="text-end">Saldo (Rp)</th>
                        <th>Keterangan</th>
                        <th>Petugas</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($transactions)): ?>
                    <tr>
                        <td colspan="8" class="text-center text-muted py-4">Belum ada transaksi</td>
                    </tr>
                    <?php else: ?>
                    <?php $no = 1; foreach ($transactions as $t): ?>
                    <tr>
                        <td class="text-center"><?= $no++ ?></td>
                        <td><?= date('d/m/Y', strtotime($t['transaction_date'])) ?></td>
                        <td>
                            <?php if ($t['transaction_type'] === 'setoran'): ?>
                            <span class="badge bg-success">Setoran</span>
                            <?php else: ?>
                            <span class="badge bg-danger">Penarikan</span>
                            <?php endif; ?>
                        </td>
                        <td class="text-end text-success">
                            <?= $t['transaction_type'] === 'setoran' ? number_format($t['amount'], 0, ',', '.') : '-' ?>
                        </td>
                        <td class="text-end text-danger">
                            <?= $t['transaction_type'] === 'penarikan' ? number_format($t['amount'], 0, ',', '.') : '-' ?>
                        </td>
                        <td class="text-end fw-bold"><?= 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>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
