<?php
/**
 * Rekap Dana Pensiun - Keseluruhan
 * Koperasi Management System
 */

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

$pageTitle = 'Rekap Dana Pensiun';
$pageSubtitle = 'Ringkasan Dana Pensiun Keseluruhan';

// Get summary data
$stmt = $db->query("
    SELECT 
        COALESCE(SUM(CASE WHEN s.tipe='setoran' THEN s.jumlah ELSE 0 END), 0) as grand_total_setoran,
        COALESCE(SUM(CASE WHEN s.tipe='penarikan' THEN s.jumlah ELSE 0 END), 0) as grand_total_penarikan
    FROM simpanan s
    WHERE s.jenis = 'dana_pensiun'
");
$summary = $stmt->fetch();

$grand_setoran = $summary['grand_total_setoran'] ?? 0;
$grand_penarikan = $summary['grand_total_penarikan'] ?? 0;
$grand_saldo = $grand_setoran - $grand_penarikan;

// Get per-anggota breakdown
$stmt = $db->query("
    SELECT 
        a.id, 
        a.no_anggota, 
        a.nama,
        COALESCE(SUM(CASE WHEN s.tipe='setoran' THEN s.jumlah ELSE 0 END), 0) as total_setoran,
        COALESCE(SUM(CASE WHEN s.tipe='penarikan' THEN s.jumlah ELSE 0 END), 0) as total_penarikan
    FROM anggota a
    LEFT JOIN simpanan s ON s.anggota_id = a.id AND s.jenis = 'dana_pensiun'
    WHERE a.status = 'aktif'
    GROUP BY a.id, a.no_anggota, a.nama
    HAVING COALESCE(SUM(CASE WHEN s.tipe='setoran' THEN s.jumlah ELSE 0 END), 0) > 0 
        OR COALESCE(SUM(CASE WHEN s.tipe='penarikan' THEN s.jumlah ELSE 0 END), 0) > 0
    ORDER BY a.nama
");
$data = $stmt->fetchAll();

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 Halaman Utama
    </a>
    <a href="simpanan_dana_pensiun_rekap_umum_export.php" 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>

<!-- Summary Cards -->
<div class="row mb-4">
    <div class="col-md-4 mb-3">
        <div class="card">
            <div class="card-body">
                <div class="text-muted" style="font-size: 0.9rem; margin-bottom: 8px;">Total Setoran</div>
                <div class="h3 fw-bold" style="color: var(--success-600);">
                    <?= formatRupiah($grand_setoran) ?>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-4 mb-3">
        <div class="card">
            <div class="card-body">
                <div class="text-muted" style="font-size: 0.9rem; margin-bottom: 8px;">Total Penarikan</div>
                <div class="h3 fw-bold" style="color: var(--danger-600);">
                    <?= formatRupiah($grand_penarikan) ?>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-4 mb-3">
        <div class="card">
            <div class="card-body">
                <div class="text-muted" style="font-size: 0.9rem; margin-bottom: 8px;">Saldo Total</div>
                <div class="h3 fw-bold" style="color: var(--primary-600);">
                    <?= formatRupiah($grand_saldo) ?>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Detail Table -->
<div class="card">
    <div class="card-header">
        <h3><i class="fas fa-table"></i> Dana Pensiun per Anggota</h3>
    </div>
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>No. Anggota</th>
                    <th>Nama</th>
                    <th style="text-align: right;">Total Setoran</th>
                    <th style="text-align: right;">Total Penarikan</th>
                    <th style="text-align: right;">Saldo</th>
                    <th style="text-align: center;">Aksi</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($data)): ?>
                    <tr><td colspan="6" class="text-center text-muted" style="padding: 30px;">Tidak ada data dana pensiun</td></tr>
                <?php else: ?>
                    <?php foreach ($data as $row): 
                        $saldo_anggota = $row['total_setoran'] - $row['total_penarikan'];
                    ?>
                    <tr>
                        <td><?= htmlspecialchars($row['no_anggota']) ?></td>
                        <td>
                            <a href="simpanan_dana_pensiun_rekap.php?id=<?= $row['id'] ?>" style="text-decoration: none;">
                                <?= htmlspecialchars($row['nama']) ?>
                            </a>
                        </td>
                        <td style="text-align: right; color: var(--success-600); font-weight: bold;">
                            <?= formatRupiah($row['total_setoran']) ?>
                        </td>
                        <td style="text-align: right; color: var(--danger-600); font-weight: bold;">
                            <?= formatRupiah($row['total_penarikan']) ?>
                        </td>
                        <td style="text-align: right; font-weight: bold; color: <?= $saldo_anggota >= 0 ? 'var(--success-600)' : 'var(--danger-600)' ?>;">
                            <?= formatRupiah($saldo_anggota) ?>
                        </td>
                        <td style="text-align: center;">
                            <a href="simpanan_dana_pensiun_rekap.php?id=<?= $row['id'] ?>" class="btn btn-sm btn-outline" title="Lihat Detail">
                                <i class="fas fa-eye"></i>
                            </a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
            <tfoot>
                <tr style="background: var(--primary-50);">
                    <td colspan="2" style="font-weight: bold;">TOTAL</td>
                    <td style="text-align: right; font-weight: bold; color: var(--success-600);">
                        <?= formatRupiah($grand_setoran) ?>
                    </td>
                    <td style="text-align: right; font-weight: bold; color: var(--danger-600);">
                        <?= formatRupiah($grand_penarikan) ?>
                    </td>
                    <td style="text-align: right; font-weight: bold; color: var(--primary-600);">
                        <?= formatRupiah($grand_saldo) ?>
                    </td>
                    <td></td>
                </tr>
            </tfoot>
        </table>
    </div>
</div>

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