<?php
/**
 * Rekap Simpanan Wajib per Anggota
 * Koperasi Management System
 */

require_once __DIR__ . '/../config/database.php';
$db = getDB();

$id = $_GET['id'] ?? 0;
$stmt = $db->prepare("SELECT * FROM anggota WHERE id = ?");
$stmt->execute([$id]);
$anggota = $stmt->fetch();

if (!$anggota) {
    die("Anggota tidak ditemukan.");
}

$pageTitle = 'Rekap Simpanan Wajib';
$pageSubtitle = 'Riwayat simpanan wajib: ' . htmlspecialchars($anggota['nama']);

// Get all mandatory savings for this member
$stmt = $db->prepare("
    SELECT * FROM simpanan 
    WHERE anggota_id = ? AND jenis = 'wajib' 
    ORDER BY tanggal DESC
");
$stmt->execute([$id]);
$history = $stmt->fetchAll();

// Total
$total = 0;
foreach ($history as $h) {
    $total += ($h['tipe'] === 'setoran' ? $h['jumlah'] : -$h['jumlah']);
}

include __DIR__ . '/../includes/header.php';
?>

<div class="mb-4">
    <a href="simpanan_wajib.php" class="btn btn-outline">
        <i class="fas fa-arrow-left"></i> Kembali ke Daftar
    </a>
</div>

<div class="row">
    <div class="col-md-4">
        <div class="card">
            <div class="card-body text-center">
                <div class="member-avatar avatar-<?= ($anggota['id'] % 6) + 1 ?> mx-auto mb-3" style="width: 80px; height: 80px; font-size: 2rem;">
                    <?= strtoupper(substr($anggota['nama'], 0, 2)) ?>
                </div>
                <h2><?= htmlspecialchars($anggota['nama']) ?></h2>
                <p class="text-muted"><?= htmlspecialchars($anggota['no_anggota']) ?></p>
                <div class="mt-4 p-3 bg-light rounded">
                    <div class="text-sm text-muted">Total Simpanan Wajib</div>
                    <div class="h2 fw-bold text-success mt-1"><?= formatRupiah($total) ?></div>
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-md-8">
        <div class="card">
            <div class="card-header">
                <h3><i class="fas fa-history"></i> Riwayat Pembayaran</h3>
            </div>
            <div class="table-responsive">
                <table class="data-table">
                    <thead>
                        <tr>
                            <th>Tanggal</th>
                            <th>Bulan (Keterangan)</th>
                            <th>Jumlah</th>
                            <th>Petugas</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($history)): ?>
                            <tr><td colspan="4" class="text-center text-muted" style="padding: 30px;">Belum ada riwayat pembayaran</td></tr>
                        <?php else: ?>
                            <?php foreach ($history as $h): ?>
                            <tr>
                                <td><?= formatTanggal($h['tanggal']) ?></td>
                                <td><?= htmlspecialchars($h['keterangan'] ?: '-') ?></td>
                                <td class="fw-bold text-success"><?= formatRupiah($h['jumlah']) ?></td>
                                <td>
                                    <?php
                                    if ($h['created_by']) {
                                        $uStmt = $db->prepare("SELECT username FROM users WHERE id = ?");
                                        $uStmt->execute([$h['created_by']]);
                                        echo htmlspecialchars($uStmt->fetchColumn() ?: '-');
                                    } else {
                                        echo '-';
                                    }
                                    ?>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<?php include __DIR__ . '/../includes/footer.php'; ?>
