<?php
session_start();
require_once '../koneksi.php';
require_once '../auth.php';
cekAdminAtauGuru();

$id_siswa = isset($_GET['id_siswa']) ? mysqli_real_escape_string($koneksi, $_GET['id_siswa']) : 0;

// Ambil data siswa
$query_siswa = mysqli_query($koneksi, "SELECT nama FROM master_siswa WHERE id_siswa = '$id_siswa'");
$siswa = mysqli_fetch_assoc($query_siswa);

// Ambil riwayat transaksi
$riwayat = mysqli_query($koneksi, "SELECT * FROM riwayat_tabungan WHERE id_siswa = '$id_siswa' ORDER BY tanggal DESC");

include 'layout.php';
?>

<div class="container-fluid mt-4">
    <div class="card p-4 shadow-sm">
        <div class="d-flex justify-content-between align-items-center">
            <h3>Histori Transaksi: <?= htmlspecialchars($siswa['nama'] ?? 'Siswa Tidak Ditemukan') ?></h3>
            <a href="tabungan_siswa.php" class="btn btn-secondary">Kembali</a>
        </div>
        <hr>

        <div class="table-responsive">
            <table class="table table-bordered table-striped">
                <thead class="table-light">
                    <tr>
                        <th>Tanggal</th>
                        <th>Jenis</th>
                        <th>Jumlah</th>
                        <th>Keterangan</th>
                    </tr>
                </thead>
                <tbody>
                    <?php while($row = mysqli_fetch_assoc($riwayat)) { ?>
                    <tr>
                        <td><?= date('d-m-Y', strtotime($row['tanggal'])) ?></td>
                        <td>
                            <span class="badge <?= ($row['jenis'] == 'Setor') ? 'bg-success' : 'bg-danger' ?>">
                                <?= $row['jenis'] ?>
                            </span>
                        </td>
                        <td>Rp <?= number_format($row['jumlah'], 0, ',', '.') ?></td>
                        <td><?= htmlspecialchars($row['keterangan']) ?></td>
                    </tr>
                    <?php } ?>
                    <?php if(mysqli_num_rows($riwayat) == 0): ?>
                        <tr><td colspan="4" class="text-center">Belum ada transaksi</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
<div class="d-flex justify-content-between align-items-center">
    
    <div>
        <a href="cetak_histori.php?id_siswa=<?= $id_siswa ?>" class="btn btn-primary" target="_blank">
            <i class="fa fa-print"></i> Cetak PDF
        </a>
       
    </div>
</div>

<?php include 'layout_footer.php'; ?>