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

// 1. Ambil ID Guru yang login
$id_guru_login = $_SESSION['id_guru'];

// 2. Validasi Akses: Hanya Wali Kelas yang boleh akses
$cek_walikelas = mysqli_query($koneksi, "SELECT id_rombel FROM rombel WHERE wali_kelas = '$id_guru_login'");

if (mysqli_num_rows($cek_walikelas) == 0 && $_SESSION['role'] !== 'admin') {
    die("<div class='container mt-5'><div class='alert alert-danger'>Akses Ditolak! Halaman ini hanya untuk Wali Kelas.</div></div>");
}

$data_rombel = mysqli_fetch_assoc($cek_walikelas);
$id_r = $data_rombel['id_rombel'];

include 'layout.php';
?>

<div class="container-fluid mt-4">
    <div class="card p-4 shadow-sm">
        <h3><i class="fa fa-money-bill-wave"></i> Kelola Tabungan Siswa</h3>
        <?php
// Hitung total saldo kelas
$q_total = mysqli_query($koneksi, "SELECT SUM(saldo) as total_saldo FROM tabungan 
                                  JOIN anggota_rombel ar ON tabungan.id_siswa = ar.id_siswa 
                                  WHERE ar.id_rombel = '$id_r'");
$total = mysqli_fetch_assoc($q_total);
?>

<div class="alert alert-success d-flex align-items-center" role="alert">
    <i class="fa fa-wallet fa-2x me-3"></i>
    <div>
        <h5 class="mb-0">Total Tabungan Kelas:</h5>
        <h3 class="mb-0">Rp <?= number_format($total['total_saldo'] ?? 0, 0, ',', '.') ?></h3>
    </div>
</div>
        <p class="text-muted">Gunakan form di bawah untuk mencatat transaksi Setor atau Tarik tunai.</p>
        <hr>

        <?php if(isset($_GET['pesan'])): ?>
            <div class="alert alert-info alert-dismissible fade show">
                Transaksi berhasil diproses!
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        <?php endif; ?>

        <div class="table-responsive">
            <table class="table table-bordered table-striped table-hover">
                <thead class="table-dark">
                    <tr>
                    <th class="text-center">No</th>
                    <th>Nama Siswa</th>
                    <th>Saldo Akhir</th>
                    <th>Aksi Transaksi</th>
                    <th>Detail</th> </tr>
                </thead>
                <tbody>
                    <?php 
                    // Query mengambil siswa dan saldo (Left Join agar siswa saldo 0 tetap tampil)
                    $sql_siswa = "SELECT s.id_siswa, s.nama, IFNULL(t.saldo, 0) as saldo 
                                  FROM master_siswa s
                                  JOIN anggota_rombel ar ON s.id_siswa = ar.id_siswa
                                  LEFT JOIN tabungan t ON s.id_siswa = t.id_siswa
                                  WHERE ar.id_rombel = '$id_r'
                                  ORDER BY s.nama ASC";
                    
                    $q_siswa = mysqli_query($koneksi, $sql_siswa);
                    $no = 1;
                    while($s = mysqli_fetch_array($q_siswa)) { ?>
                        <tr>
                            <td class="text-center"><?= $no++ ?></td>
                            <td class="fw-bold"><?= htmlspecialchars($s['nama']) ?></td>
                            <td class="text-primary fw-bold">Rp <?= number_format($s['saldo'], 0, ',', '.') ?></td>
                            <td>
                                <form action="proses_tabungan.php" method="POST" class="d-inline-flex gap-1 mb-1">
                                    <input type="hidden" name="id_siswa" value="<?= $s['id_siswa'] ?>">
                                    <input type="hidden" name="jenis" value="Setor">
                                    <input type="date" name="tanggal" value="<?= date('Y-m-d') ?>" class="form-control form-control-sm" style="width:130px" required>
                                    <input type="number" name="jumlah" placeholder="Rp" required class="form-control form-control-sm" style="width:100px">
                                    <button type="submit" class="btn btn-sm btn-success"><i class="fa fa-plus"></i> Setor</button>
                                </form>

                                <form action="proses_tabungan.php" method="POST" class="d-inline-flex gap-1">
                                    <input type="hidden" name="id_siswa" value="<?= $s['id_siswa'] ?>">
                                    <input type="hidden" name="jenis" value="Tarik">
                                    <input type="date" name="tanggal" value="<?= date('Y-m-d') ?>" class="form-control form-control-sm" style="width:130px" required>
                                    <input type="number" name="jumlah" placeholder="Rp" required class="form-control form-control-sm" style="width:100px">
                                    <button type="submit" class="btn btn-sm btn-danger"><i class="fa fa-minus"></i> Tarik</button>
                                </form>
                            </td>
                        </tr>
                        <tr>
                            <td>Rp <?= number_format($s['saldo'], 0, ',', '.') ?></td>
                            
                            <td>
                <a href="histori_tabungan.php?id_siswa=<?= $s['id_siswa'] ?>" class="btn btn-sm btn-info text-white">
                    <i class="fa fa-history"></i> Histori
                </a>
            </td>
        </tr>
                    <?php } ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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