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

// Pastikan yang login adalah Siswa
if ($_SESSION['role'] !== 'siswa') {
    die("Akses ditolak!");
}

$id_siswa_login = $_SESSION['id_siswa']; // Pastikan session id_siswa tersedia

// 1. Ambil Data Siswa dan Nama Kelas
$query_siswa = mysqli_query($koneksi, "
    SELECT s.nama as nama_siswa, r.kelas 
    FROM master_siswa s
    JOIN anggota_rombel ar ON s.id_siswa = ar.id_siswa
    JOIN rombel r ON ar.id_rombel = r.id_rombel
    WHERE s.id_siswa = '$id_siswa_login'
");
$siswa = mysqli_fetch_assoc($query_siswa);

$nama_siswa = $siswa['nama_siswa'] ?? 'Siswa';
$nama_kelas = $siswa['kelas'] ?? 'Belum ada kelas';

// 2. Ambil Saldo
$query_saldo = mysqli_query($koneksi, "SELECT saldo FROM tabungan WHERE id_siswa = '$id_siswa_login'");
$data_saldo = mysqli_fetch_assoc($query_saldo);
$saldo = $data_saldo['saldo'] ?? 0;

// 3. Ambil Riwayat Transaksi
$riwayat = mysqli_query($koneksi, "SELECT * FROM riwayat_tabungan WHERE id_siswa = '$id_siswa_login' ORDER BY tanggal DESC");

include 'layout_footer.php'; // Sesuaikan dengan layout dashboard siswa Anda
?>

<div class="container-fluid mt-4">
    <div class="card bg-primary text-white mb-4 shadow-sm">
        <div class="card-body">
            <h5>Saldo Tabungan Anda</h5>
            <h2 class="display-5">Rp <?= number_format($saldo, 0, ',', '.') ?></h2>
        </div>
    </div>

<div class="mb-3">
    <a href="dashboard.php" class="btn btn-outline-secondary btn-lg shadow-sm">
        <i class="fas fa-arrow-left"></i> Kembali ke Dashboard
    </a>
</div>

    <div class="card p-4 shadow-sm">
        <h5>Riwayat Transaksi</h5>
        <hr>
        <div class="table-responsive">
            <table class="table table-bordered table-striped">
                <thead class="table-dark">
                    <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 } ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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