<?php
/**
 * Rekap Simpanan Sukarela - Summary Keseluruhan
 * Koperasi Management System
 */

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

$pageTitle = 'Rekap Simpanan Sukarela';
$pageSubtitle = 'Summary keseluruhan simpanan sukarela';

// Per anggota summary
$stmt = $db->prepare("
    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 = 'sukarela'
    WHERE a.status = 'aktif'
    GROUP BY a.id, a.no_anggota, a.nama
    ORDER BY a.nama ASC
");
$stmt->execute();
$anggota_list = $stmt->fetchAll();

// Calculate totals
$grand_total_setoran = 0;
$grand_total_penarikan = 0;
foreach ($anggota_list as $ag) {
    $grand_total_setoran += $ag['total_setoran'];
    $grand_total_penarikan += $ag['total_penarikan'];
}
$grand_saldo = $grand_total_setoran - $grand_total_penarikan;

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

<div class="mb-4" style="display: flex; gap: 10px; flex-wrap: wrap;">
    <a href="simpanan_sukarela.php" class="btn btn-outline">
        <i class="fas fa-arrow-left"></i> Kembali ke Daftar
    </a>
    <a href="simpanan_sukarela_rekap_umum_export.php" class="btn btn-primary" style="background: linear-gradient(135deg, #10b981 0%, #059669 100%); border: none;">
        <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">
        <div class="card">
            <div class="card-body text-center">
                <h3 style="color: var(--warning-500); margin-bottom: 10px;"><i class="fas fa-arrow-down"></i></h3>
                <div class="text-muted">Total Setoran</div>
                <div class="h3 fw-bold" style="color: var(--success-600);"><?= formatRupiah($grand_total_setoran) ?></div>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card">
            <div class="card-body text-center">
                <h3 style="color: var(--danger-500); margin-bottom: 10px;"><i class="fas fa-arrow-up"></i></h3>
                <div class="text-muted">Total Penarikan</div>
                <div class="h3 fw-bold" style="color: var(--danger-600);"><?= formatRupiah($grand_total_penarikan) ?></div>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card">
            <div class="card-body text-center">
                <h3 style="color: var(--primary-500); margin-bottom: 10px;"><i class="fas fa-wallet"></i></h3>
                <div class="text-muted">Saldo Total</div>
                <div class="h3 fw-bold" style="color: var(--primary-700);"><?= formatRupiah($grand_saldo) ?></div>
            </div>
        </div>
    </div>
</div>

<!-- Detail Table -->
<div class="card">
    <div class="card-header">
        <h3><i class="fas fa-table"></i> Rincian Per Anggota</h3>
    </div>
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>No. Anggota</th>
                    <th>Nama Anggota</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($anggota_list)): ?>
                    <tr><td colspan="6" class="text-center text-muted" style="padding: 30px;">Tidak ada data anggota</td></tr>
                <?php else: ?>
                    <?php $no = 1; foreach ($anggota_list as $ag): 
                        $saldo = $ag['total_setoran'] - $ag['total_penarikan'];
                    ?>
                    <tr>
                        <td><?= htmlspecialchars($ag['no_anggota']) ?></td>
                        <td><?= htmlspecialchars($ag['nama']) ?></td>
                        <td style="text-align: right;"><?= formatRupiah($ag['total_setoran']) ?></td>
                        <td style="text-align: right;"><?= formatRupiah($ag['total_penarikan']) ?></td>
                        <td style="text-align: right; font-weight: bold; color: <?= $saldo >= 0 ? 'green' : 'red' ?>;">
                            <?= formatRupiah($saldo) ?>
                        </td>
                        <td style="text-align: center;">
                            <a href="simpanan_sukarela_rekap.php?id=<?= $ag['id'] ?>" class="btn btn-icon btn-outline" title="Lihat Detail">
                                <i class="fas fa-eye" style="color: var(--info-500);"></i>
                            </a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
            <tfoot style="background: var(--gray-50); font-weight: bold;">
                <tr>
                    <td colspan="2" style="text-align: right; padding: 16px;">TOTAL:</td>
                    <td style="text-align: right; padding: 16px; background: var(--success-100); border-left: 3px solid var(--success-500);">
                        <?= formatRupiah($grand_total_setoran) ?>
                    </td>
                    <td style="text-align: right; padding: 16px; background: var(--danger-100); border-left: 3px solid var(--danger-500);">
                        <?= formatRupiah($grand_total_penarikan) ?>
                    </td>
                    <td style="text-align: right; padding: 16px; background: var(--primary-100); border-left: 3px solid var(--primary-500);">
                        <?= formatRupiah($grand_saldo) ?>
                    </td>
                    <td></td>
                </tr>
            </tfoot>
        </table>
    </div>
</div>

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