<?php
/**
 * Super Admin Dashboard — Gmind Institute
 * Shows ERP overview: branches, wallets, applications, franchise plans
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
?>
<div class="admin-stats-grid">
    <div class="admin-stat-card">
        <div class="admin-stat-icon" style="background:#e8f5e9;color:#2e7d32;">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="24" height="24"><path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0118 0z"/><circle cx="12" cy="10" r="3"/></svg>
        </div>
        <div class="admin-stat-info">
            <span class="admin-stat-value"><?= number_format($totalBranches ?? 0) ?></span>
            <span class="admin-stat-label">Total Branches</span>
        </div>
    </div>
    <div class="admin-stat-card">
        <div class="admin-stat-icon" style="background:#e3f2fd;color:#1565c0;">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="24" height="24"><path d="M17 21v-2a4 4 0 00-4-4H5a4 4 0 00-4 4v2"/><circle cx="9" cy="7" r="4"/></svg>
        </div>
        <div class="admin-stat-info">
            <span class="admin-stat-value"><?= number_format($totalStudents ?? 0) ?></span>
            <span class="admin-stat-label">Total Students</span>
        </div>
    </div>
    <div class="admin-stat-card">
        <div class="admin-stat-icon" style="background:#fff3e0;color:#e65100;">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="24" height="24"><path d="M12 1v22M17 5H9.5a3.5 3.5 0 000 7h5a3.5 3.5 0 010 7H6"/></svg>
        </div>
        <div class="admin-stat-info">
            <span class="admin-stat-value">₹<?= number_format($totalWalletBalance ?? 0, 2) ?></span>
            <span class="admin-stat-label">Total Wallet Balance</span>
        </div>
    </div>
    <div class="admin-stat-card">
        <div class="admin-stat-icon" style="background:#fce4ec;color:#c62828;">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="24" height="24"><rect x="3" y="4" width="18" height="18" rx="2"/><line x1="16" y1="2" x2="16" y2="6"/><line x1="8" y1="2" x2="8" y2="6"/><line x1="3" y1="10" x2="21" y2="10"/></svg>
        </div>
        <div class="admin-stat-info">
            <span class="admin-stat-value"><?= number_format($pendingApplications ?? 0) ?></span>
            <span class="admin-stat-label">Pending Applications</span>
        </div>
    </div>
</div>

<div class="admin-section" style="margin-top:24px;">
    <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:16px;">
        <h2 style="margin:0;font-size:18px;font-weight:600;">Branch Overview</h2>
        <a href="<?= $bp ?>/admin/branch-applications" class="admin-btn admin-btn-primary" style="font-size:13px;padding:6px 14px;">View Applications</a>
    </div>
    <div class="admin-table-wrapper">
        <table class="admin-table">
            <thead>
                <tr>
                    <th>Branch Name</th>
                    <th>City</th>
                    <th>Wallet Balance</th>
                    <th>Active</th>
                    <th>Verified</th>
                </tr>
            </thead>
            <tbody>
                <?php if (!empty($branchOverview)): ?>
                    <?php foreach ($branchOverview as $br): ?>
                    <tr>
                        <td><?= htmlspecialchars($br['name'], ENT_QUOTES, 'UTF-8') ?></td>
                        <td><?= htmlspecialchars($br['city'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
                        <td>₹<?= number_format((float)($br['wallet_balance'] ?? 0), 2) ?></td>
                        <td><?= !empty($br['active']) ? '<span style="color:green;">Yes</span>' : '<span style="color:red;">No</span>' ?></td>
                        <td><?= !empty($br['is_verified']) ? '<span style="color:green;">Yes</span>' : '<span style="color:orange;">Pending</span>' ?></td>
                    </tr>
                    <?php endforeach; ?>
                <?php else: ?>
                    <tr><td colspan="5" style="text-align:center;color:#999;">No branches found</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<div style="display:grid;grid-template-columns:1fr 1fr;gap:20px;margin-top:24px;">
    <!-- Recent Applications -->
    <div class="admin-section">
        <h2 style="font-size:16px;font-weight:600;margin-bottom:12px;">Recent Applications</h2>
        <?php if (!empty($recentApplications)): ?>
            <?php foreach ($recentApplications as $app): ?>
            <div style="padding:10px;border:1px solid #e0e0e0;border-radius:6px;margin-bottom:8px;">
                <strong><?= htmlspecialchars($app['name'], ENT_QUOTES, 'UTF-8') ?></strong>
                <span style="color:#666;margin-left:8px;"><?= htmlspecialchars($app['city'] ?? '', ENT_QUOTES, 'UTF-8') ?></span>
                <span style="float:right;font-size:12px;padding:2px 8px;border-radius:10px;background:<?= $app['status'] === 'pending' ? '#fff3e0' : ($app['status'] === 'approved' ? '#e8f5e9' : '#ffebee') ?>;color:<?= $app['status'] === 'pending' ? '#e65100' : ($app['status'] === 'approved' ? '#2e7d32' : '#c62828') ?>;"><?= ucfirst($app['status']) ?></span>
            </div>
            <?php endforeach; ?>
        <?php else: ?>
            <p style="color:#999;">No applications yet</p>
        <?php endif; ?>
    </div>

    <!-- Recent Wallet Transactions -->
    <div class="admin-section">
        <h2 style="font-size:16px;font-weight:600;margin-bottom:12px;">Recent Wallet Transactions</h2>
        <?php if (!empty($recentTransactions)): ?>
            <?php foreach ($recentTransactions as $tx): ?>
            <div style="padding:10px;border:1px solid #e0e0e0;border-radius:6px;margin-bottom:8px;">
                <strong><?= htmlspecialchars($tx['branch_name'] ?? 'Unknown', ENT_QUOTES, 'UTF-8') ?></strong>
                <span style="float:right;font-weight:600;color:<?= $tx['type'] === 'credit' ? '#2e7d32' : '#c62828' ?>;">
                    <?= $tx['type'] === 'credit' ? '+' : '-' ?>₹<?= number_format((float)$tx['amount'], 2) ?>
                </span>
                <br>
                <small style="color:#666;"><?= htmlspecialchars($tx['description'] ?? '', ENT_QUOTES, 'UTF-8') ?></small>
            </div>
            <?php endforeach; ?>
        <?php else: ?>
            <p style="color:#999;">No transactions yet</p>
        <?php endif; ?>
    </div>
</div>

<!-- Quick Links -->
<div style="margin-top:24px;display:flex;gap:12px;flex-wrap:wrap;">
    <a href="<?= $bp ?>/admin/franchise-plans" class="admin-btn admin-btn-secondary" style="font-size:13px;">Franchise Plans</a>
    <a href="<?= $bp ?>/admin/branch-wallet" class="admin-btn admin-btn-secondary" style="font-size:13px;">Branch Wallets</a>
    <a href="<?= $bp ?>/admin/announcements" class="admin-btn admin-btn-secondary" style="font-size:13px;">Announcements</a>
    <a href="<?= $bp ?>/admin/branch-branding" class="admin-btn admin-btn-secondary" style="font-size:13px;">Branding Materials</a>
</div>
