<?php
/** @var array $branches */
/** @var string $csrfToken */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
?>
<div class="container-fluid py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 class="mb-1"><i class="fas fa-wallet"></i> Branch Wallet Management</h2>
            <p class="text-muted mb-0">Recharge branch wallets and view transaction history</p>
        </div>
    </div>

    <!-- Stats Summary -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card border-success">
                <div class="card-body text-center">
                    <h5 class="text-success mb-0">₹<?= number_format(array_sum(array_column($branches, 'wallet_balance'))) ?></h5>
                    <small class="text-muted">Total Wallet Balance</small>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-primary">
                <div class="card-body text-center">
                    <h5 class="text-primary mb-0"><?= count($branches) ?></h5>
                    <small class="text-muted">Total Branches</small>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-warning">
                <div class="card-body text-center">
                    <h5 class="text-warning mb-0"><?= count(array_filter($branches, fn($b) => ($b['wallet_balance'] ?? 0) < 200)) ?></h5>
                    <small class="text-muted">Low Balance (< ₹200)</small>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-info">
                <div class="card-body text-center">
                    <h5 class="text-info mb-0"><?= array_sum(array_column($branches, 'cert_count')) ?></h5>
                    <small class="text-muted">Certificates Generated</small>
                </div>
            </div>
        </div>
    </div>

    <!-- Branches Table -->
    <div class="card">
        <div class="card-header">
            <h5 class="mb-0"><i class="fas fa-list"></i> All Branches</h5>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>#</th>
                            <th>Branch Name</th>
                            <th>Branch Code</th>
                            <th>Admin Email</th>
                            <th class="text-center">Wallet Balance</th>
                            <th class="text-center">Total Recharged</th>
                            <th class="text-center">Total Spent</th>
                            <th class="text-center">Certs</th>
                            <th class="text-center">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($branches as $i => $b): 
                            $balance = (float)($b['wallet_balance'] ?? 0);
                            $balanceColor = $balance < 200 ? 'text-danger' : 'text-success';
                            $recharged = (float)($b['total_recharged'] ?? 0);
                            $spent = (float)($b['total_spent'] ?? 0);
                        ?>
                        <tr>
                            <td><?= $i + 1 ?></td>
                            <td><strong><?= htmlspecialchars($b['name'] ?? '') ?></strong></td>
                            <td><code><?= htmlspecialchars($b['branch_code'] ?? '') ?></code></td>
                            <td><?= htmlspecialchars($b['admin_email'] ?? '—') ?></td>
                            <td class="text-center">
                                <strong class="<?= $balanceColor ?>">₹<?= number_format($balance) ?></strong>
                            </td>
                            <td class="text-center text-primary">₹<?= number_format($recharged) ?></td>
                            <td class="text-center text-warning">₹<?= number_format($spent) ?></td>
                            <td class="text-center"><?= (int)($b['cert_count'] ?? 0) ?></td>
                            <td class="text-center">
                                <a href="<?= $bp ?>/super-admin/wallet/recharge?id=<?= (int)$b['id'] ?>" 
                                   class="btn btn-sm btn-success" title="Recharge">
                                    <i class="fas fa-plus-circle"></i> Recharge
                                </a>
                                <a href="<?= $bp ?>/super-admin/wallet/history?id=<?= (int)$b['id'] ?>" 
                                   class="btn btn-sm btn-info text-white" title="History">
                                    <i class="fas fa-history"></i> History
                                </a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
