<?php $this->layout('main'); ?>

<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2>Super Admin Dashboard</h2>
        <span class="text-muted"><?= date('l, F j, Y') ?></span>
    </div>

    <!-- Stats -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card bg-primary text-white h-100">
                <div class="card-body">
                    <h5 class="card-title">Total Organizations</h5>
                    <h3 class="card-text"><?= $total_orgs ?? 0 ?></h3>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-info text-white h-100">
                <div class="card-body">
                    <h5 class="card-title">Total Users</h5>
                    <h3 class="card-text"><?= $total_users ?? 0 ?></h3>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-secondary text-white h-100">
                <div class="card-body">
                    <h5 class="card-title">Total Projects</h5>
                    <h3 class="card-text"><?= $total_projects ?? 0 ?></h3>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-success text-white h-100">
                <div class="card-body">
                    <h5 class="card-title">Platform Fees Revenue</h5>
                    <h3 class="card-text"><?= formatCurrency((float)($revenue_fees ?? 0)) ?></h3>
                </div>
            </div>
        </div>
    </div>

    <!-- Row 2: Chart & Health -->
    <div class="row mb-4">
        <div class="col-md-8">
            <div class="card h-100">
                <div class="card-header">Monthly Registrations</div>
                <div class="card-body">
                    <canvas id="registrationsChart"></canvas>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card h-100">
                <div class="card-header">System Health</div>
                <div class="card-body text-center">
                    <h1 class="display-1 text-success"><i class="bi bi-check-circle-fill"></i></h1>
                    <h4>Status: <?= htmlspecialchars($system_health ?? 'Unknown') ?></h4>
                </div>
            </div>
        </div>
    </div>

    <!-- Row 3: Tables -->
    <div class="row">
        <div class="col-md-8">
            <div class="card h-100">
                <div class="card-header">Recent Organizations (10)</div>
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead>
                            <tr>
                                <th>Name</th>
                                <th>Plan</th>
                                <th>Joined Date</th>
                                <th>Status</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($recent_orgs ?? [] as $org): ?>
                                <tr>
                                    <td><?= htmlspecialchars($org['name']) ?></td>
                                    <td><?= htmlspecialchars($org['plan_name'] ?? '-') ?></td>
                                    <td><?= htmlspecialchars($org['created_at']) ?></td>
                                    <td>
                                        <?php
                                            if ($org['status'] === 'pending') {
                                                $badgeClass = 'bg-warning text-dark'; $badgeLabel = 'Pending';
                                            } elseif ($org['status'] === 'rejected') {
                                                $badgeClass = 'bg-danger'; $badgeLabel = 'Rejected';
                                            } elseif ((int)$org['is_active'] === 0) {
                                                $badgeClass = 'bg-danger'; $badgeLabel = 'Suspended';
                                            } else {
                                                $badgeClass = 'bg-success'; $badgeLabel = 'Active';
                                            }
                                        ?>
                                        <span class="badge <?= $badgeClass ?>"><?= $badgeLabel ?></span>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card h-100">
                <div class="card-header">Pending Subscription Charges</div>
                <ul class="list-group list-group-flush">
                    <?php foreach ($pending_charges ?? [] as $charge): ?>
                        <li class="list-group-item d-flex justify-content-between align-items-center">
                            <?= htmlspecialchars($charge['org_name']) ?>
                            <span><?= formatCurrency((float)$charge['charge_amount'], $charge['currency'] ?? DEFAULT_CURRENCY) ?></span>
                        </li>
                    <?php endforeach; ?>
                </ul>
            </div>
        </div>
    </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
    // Initialize chart here
</script>
