<?php
// siswa/dashboard.php
include_once 'init_siswa.php'; // Pastikan membuat file init_siswa.php untuk proteksi session
$page_title = 'Dashboard Siswa';
$page_icon = 'bi-speedometer2';
include 'header_siswa.php'; // Header khusus desain siswa

// Ambil ID Siswa dari session yang diset saat login
$id_siswa = $_SESSION['id_user'];

try {
    // 1. Ambil Data Utama Siswa & Saldo (Sesuai struktur db_mms26)
    $stmt_siswa = $pdo->prepare("SELECT s.*, k.nama_kelas 
                                 FROM siswa s 
                                 LEFT JOIN kelas k ON s.id_kelas = k.id_kelas 
                                 WHERE s.id_siswa = ?");
    $stmt_siswa->execute([$id_siswa]);
    $data_siswa = $stmt_siswa->fetch();

    // 2. Ringkasan Saldo (Hanya yang status 'valid')
    $total_saldo = $data_siswa['saldo'] ?? 0;

    // 3. Hitung Total Setoran (Siswa tersebut)
    $stmt_setoran = $pdo->prepare("SELECT SUM(jumlah) FROM transaksi WHERE id_siswa = ? AND jenis_transaksi = 'setoran' AND status = 'valid'");
    $stmt_setoran->execute([$id_siswa]);
    $total_setoran = $stmt_setoran->fetchColumn() ?? 0;

    // 4. Hitung Total Penarikan (Siswa tersebut)
    $stmt_penarikan = $pdo->prepare("SELECT SUM(jumlah) FROM transaksi WHERE id_siswa = ? AND jenis_transaksi = 'penarikan' AND status = 'valid'");
    $stmt_penarikan->execute([$id_siswa]);
    $total_penarikan = $stmt_penarikan->fetchColumn() ?? 0;

    // 5. Ambil 5 Transaksi Terakhir
    $stmt_recent = $pdo->prepare("SELECT * FROM transaksi WHERE id_siswa = ? ORDER BY tanggal_transaksi DESC LIMIT 5");
    $stmt_recent->execute([$id_siswa]);
    $recent_trf = $stmt_recent->fetchAll();

} catch (PDOException $e) {
    echo '<div class="alert alert-danger">Terjadi kesalahan sistem: ' . $e->getMessage() . '</div>';
}
?>

<div class="row g-3 mb-4">
    <div class="col-md-4">
        <div class="card bg-primary text-white shadow-sm border-0 h-100">
            <div class="card-body">
                <h6 class="text-white-50">Saldo Tabungan</h6>
                <h2 class="fw-bold mb-0">Rp <?= number_format($total_saldo, 0, ',', '.') ?></h2>
                <i class="bi bi-wallet2 position-absolute end-0 bottom-0 m-3 opacity-25 fs-1"></i>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card bg-success text-white shadow-sm border-0 h-100">
            <div class="card-body">
                <h6 class="text-white-50">Total Setoran</h6>
                <h3 class="fw-bold mb-0">Rp <?= number_format($total_setoran, 0, ',', '.') ?></h3>
                <i class="bi bi-arrow-down-left-circle position-absolute end-0 bottom-0 m-3 opacity-25 fs-1"></i>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card bg-danger text-white shadow-sm border-0 h-100">
            <div class="card-body">
                <h6 class="text-white-50">Total Penarikan</h6>
                <h3 class="fw-bold mb-0">Rp <?= number_format($total_penarikan, 0, ',', '.') ?></h3>
                <i class="bi bi-arrow-up-right-circle position-absolute end-0 bottom-0 m-3 opacity-25 fs-1"></i>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-lg-8">
        <div class="card border-0 shadow-sm mb-4">
            <div class="card-header bg-white fw-bold">
                <i class="bi bi-clock-history me-2"></i>5 Transaksi Terakhir
            </div>
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead class="table-light">
                            <tr>
                                <th>Tanggal</th>
                                <th>Jenis</th>
                                <th>Jumlah</th>
                                <th>Status</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($recent_trf as $tr): ?>
                            <tr>
                                <td class="small"><?= date('d/m/Y H:i', strtotime($tr['tanggal_transaksi'])) ?></td>
                                <td><span class="badge bg-<?= ($tr['jenis_transaksi'] == 'setoran' || $tr['jenis_transaksi'] == 'transfer_masuk') ? 'success' : 'danger' ?>"><?= ucfirst($tr['jenis_transaksi']) ?></span></td>
                                <td class="fw-bold">Rp <?= number_format($tr['jumlah'], 0, ',', '.') ?></td>
                                <td>
                                    <?php if($tr['status'] == 'valid'): ?>
                                        <span class="text-success"><i class="bi bi-check-circle-fill"></i></span>
                                    <?php else: ?>
                                        <span class="text-warning small italic">Pending</span>
                                    <?php endif; ?>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <div class="col-lg-4">
        <div class="card border-0 shadow-sm bg-light">
            <div class="card-body text-center py-4">
                <div class="mb-3">
                    <i class="bi bi-person-circle display-4 text-primary"></i>
                </div>
                <h5 class="fw-bold"><?= htmlspecialchars($data_siswa['nama_siswa']) ?></h5>
                <p class="text-muted mb-0"><?= $data_siswa['nis'] ?> | Kelas: <?= $data_siswa['nama_kelas'] ?></p>
                <hr>
                <div class="d-grid gap-2">
                    <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#modalTransfer">
                        <i class="bi bi-send me-2"></i>Transfer Saldo
                    </button>
                    <button class="btn btn-outline-primary" data-bs-toggle="modal" data-bs-target="#modalTarik">
                        <i class="bi bi-cash-stack me-2"></i>Tarik Tunai
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>

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