<?php
/**
 * Rekap Dana Pensiun 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 Dana Pensiun';
$pageSubtitle = 'Riwayat dana pensiun: ' . htmlspecialchars($anggota['nama']);

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

// Total setoran dan penarikan
$total_setoran = 0;
$total_penarikan = 0;
foreach ($history as $h) {
    if ($h['tipe'] === 'setoran') {
        $total_setoran += $h['jumlah'];
    } else {
        $total_penarikan += $h['jumlah'];
    }
}
$saldo = $total_setoran - $total_penarikan;

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

<div class="mb-4" style="display: flex; gap: 10px; flex-wrap: wrap;">
    <a href="simpanan_dana_pensiun.php" class="btn btn-outline">
        <i class="fas fa-arrow-left"></i> Kembali ke Daftar
    </a>
    <a href="simpanan_dana_pensiun_rekap_export.php?id=<?= $id ?>" class="btn btn-primary" style="background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%); border: none; color: white;">
        <i class="fas fa-file-export"></i> Export (PDF/Word/Excel)
    </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">Saldo Dana Pensiun</div>
                    <div class="h2 fw-bold" style="color: <?= $saldo >= 0 ? 'var(--success-600)' : 'var(--danger-600)' ?>;">
                        <?= formatRupiah($saldo) ?>
                    </div>
                </div>
            </div>
        </div>
        <div class="card mt-3">
            <div class="card-body">
                <div style="padding: 12px; background: var(--success-50); border-left: 4px solid var(--success-500); border-radius: 4px; margin-bottom: 12px;">
                    <div style="font-size: 0.85rem; color: var(--success-700);">Total Setoran</div>
                    <div style="font-weight: bold; color: var(--success-700);"><?= formatRupiah($total_setoran) ?></div>
                </div>
                <div style="padding: 12px; background: var(--danger-50); border-left: 4px solid var(--danger-500); border-radius: 4px;">
                    <div style="font-size: 0.85rem; color: var(--danger-700);">Total Penarikan</div>
                    <div style="font-weight: bold; color: var(--danger-700);"><?= formatRupiah($total_penarikan) ?></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 Transaksi</h3>
            </div>
            <div class="table-responsive">
                <table class="data-table">
                    <thead>
                        <tr>
                            <th>Tanggal</th>
                            <th>Tipe</th>
                            <th>Jumlah</th>
                            <th>Keterangan</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($history)): ?>
                            <tr><td colspan="4" class="text-center text-muted" style="padding: 30px;">Belum ada riwayat transaksi</td></tr>
                        <?php else: ?>
                            <?php foreach ($history as $h): ?>
                            <tr>
                                <td><?= formatTanggal($h['tanggal']) ?></td>
                                <td>
                                    <span class="badge <?= $h['tipe'] === 'setoran' ? 'badge-success' : 'badge-danger' ?>">
                                        <?= ucfirst($h['tipe']) ?>
                                    </span>
                                </td>
                                <td style="text-align: right; font-weight: bold;">
                                    <span style="color: <?= $h['tipe'] === 'setoran' ? 'green' : 'red' ?>;">
                                        <?= ($h['tipe'] === 'setoran' ? '+' : '-') . formatRupiah($h['jumlah']) ?>
                                    </span>
                                </td>
                                <td><?= htmlspecialchars($h['keterangan'] ?? '-') ?></td>
                            </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

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