<?php
try {
    $db = Database::getInstance();
    $activeMembers = (int) $db->fetchColumn("SELECT COUNT(*) FROM members WHERE status='active' AND is_deleted=0");
    $pendingApps = (int) $db->fetchColumn("SELECT COUNT(*) FROM applications WHERE status NOT IN ('approved','rejected','cancelled','expired') AND is_deleted=0");
    $overdueInvoices = (int) $db->fetchColumn("SELECT COUNT(*) FROM invoices WHERE status IN ('issued','partially_paid','overdue') AND due_date < CURDATE() AND balance_due > 0 AND is_deleted=0");
    $todayRevenue = (float) $db->fetchColumn("SELECT COALESCE(SUM(amount),0) FROM payments WHERE status='completed' AND payment_date=CURDATE() AND is_deleted=0");
} catch (\Exception $e) {
    $activeMembers = $pendingApps = $overdueInvoices = 0;
    $todayRevenue = 0;
}
?>
<div class="row g-3">
    <div class="col-md-3">
        <div class="card border-primary h-100">
            <div class="card-body">
                <div class="d-flex justify-content-between">
                    <div>
                        <h6 class="text-muted mb-1"><?= lang('reports.active_members') ?></h6>
                        <h3 class="mb-0"><?= number_format($activeMembers) ?></h3>
                    </div>
                    <div class="align-self-center"><i class="fas fa-users fa-2x text-primary opacity-50"></i></div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card border-warning h-100">
            <div class="card-body">
                <div class="d-flex justify-content-between">
                    <div>
                        <h6 class="text-muted mb-1"><?= lang('reports.pending_applications') ?></h6>
                        <h3 class="mb-0"><?= number_format($pendingApps) ?></h3>
                    </div>
                    <div class="align-self-center"><i class="fas fa-file-alt fa-2x text-warning opacity-50"></i></div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card border-danger h-100">
            <div class="card-body">
                <div class="d-flex justify-content-between">
                    <div>
                        <h6 class="text-muted mb-1"><?= lang('reports.overdue_invoices') ?></h6>
                        <h3 class="mb-0"><?= number_format($overdueInvoices) ?></h3>
                    </div>
                    <div class="align-self-center"><i class="fas fa-exclamation-circle fa-2x text-danger opacity-50"></i></div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card border-success h-100">
            <div class="card-body">
                <div class="d-flex justify-content-between">
                    <div>
                        <h6 class="text-muted mb-1"><?= lang('reports.today_revenue') ?></h6>
                        <h3 class="mb-0"><?= number_format($todayRevenue, 2) ?></h3>
                    </div>
                    <div class="align-self-center"><i class="fas fa-money-bill-wave fa-2x text-success opacity-50"></i></div>
                </div>
            </div>
        </div>
    </div>
</div>