<?php
/** @var array $branch */
/** @var array $transactions */
/** @var string $csrfToken */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$currentBalance = (float)($branch['wallet_balance'] ?? 0);
?>
<div class="container-fluid py-4">
    <div class="mb-3">
        <a href="<?= $bp ?>/super-admin/wallet" class="text-decoration-none" style="color:#6c3fe8;">
            ← Back to Wallet Management
        </a>
    </div>

    <!-- Branch Info + Balance -->
    <div style="background:linear-gradient(135deg,#2c1e4a,#6c3fe8);color:#fff;border-radius:12px;padding:24px;margin-bottom:20px;">
        <div class="d-flex justify-content-between align-items-center flex-wrap">
            <div>
                <h2 class="mb-1"><?= htmlspecialchars($branch['name'] ?? '') ?></h2>
                <p class="mb-0 opacity-90">
                    Code: <strong><?= htmlspecialchars($branch['branch_code'] ?? '') ?></strong> ·
                    Admin: <?= htmlspecialchars($branch['admin_name'] ?? '—') ?>
                </p>
            </div>
            <div class="text-center" style="background:rgba(255,255,255,0.15);padding:20px 40px;border-radius:12px;">
                <div style="font-size:0.82rem;opacity:0.85;">Current Balance</div>
                <div style="font-size:2.5rem;font-weight:700;">₹<?= number_format($currentBalance) ?></div>
            </div>
        </div>
    </div>

    <div class="row">
        <!-- Recharge Form -->
        <div class="col-md-5">
            <div class="card border-success">
                <div class="card-header bg-success text-white">
                    <h5 class="mb-0"><i class="fas fa-plus-circle"></i> Recharge Wallet</h5>
                </div>
                <div class="card-body">
                    <form method="POST" action="<?= $bp ?>/super-admin/wallet/process-recharge">
                        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken) ?>">
                        <input type="hidden" name="branch_id" value="<?= (int)$branch['id'] ?>">
                        
                        <div class="mb-3">
                            <label class="form-label fw-bold">Amount (₹) <span class="text-danger">*</span></label>
                            <input type="number" name="amount" class="form-control form-control-lg" 
                                   placeholder="Enter amount" min="1" step="0.01" required>
                        </div>

                        <div class="mb-3">
                            <label class="form-label">Transaction ID <small class="text-muted">(optional)</small></label>
                            <input type="text" name="txn_id" class="form-control" 
                                   placeholder="e.g., UPI-123456789">
                        </div>

                        <div class="mb-3">
                            <label class="form-label">Description <small class="text-muted">(optional)</small></label>
                            <textarea name="description" class="form-control" rows="2"
                                      placeholder="e.g., Monthly recharge, Bulk deposit"></textarea>
                        </div>

                        <!-- Quick amounts -->
                        <div class="mb-3">
                            <label class="form-label small text-muted">Quick amounts:</label><br>
                            <button type="button" class="btn btn-outline-success btn-sm me-1" onclick="document.querySelector('input[name=amount]').value=500">₹500</button>
                            <button type="button" class="btn btn-outline-success btn-sm me-1" onclick="document.querySelector('input[name=amount]').value=1000">₹1000</button>
                            <button type="button" class="btn btn-outline-success btn-sm me-1" onclick="document.querySelector('input[name=amount]').value=2000">₹2000</button>
                            <button type="button" class="btn btn-outline-success btn-sm me-1" onclick="document.querySelector('input[name=amount]').value=5000">₹5000</button>
                        </div>

                        <button type="submit" class="btn btn-success btn-lg w-100">
                            <i class="fas fa-bolt"></i> Recharge Now
                        </button>
                    </form>
                </div>
            </div>
        </div>

        <!-- Recent Transactions -->
        <div class="col-md-7">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0"><i class="fas fa-history"></i> Recent Transactions (Last 20)</h5>
                </div>
                <div class="card-body p-0">
                    <?php if (empty($transactions)): ?>
                    <div class="text-center py-5 text-muted">
                        <i class="fas fa-receipt fa-3x mb-2"></i>
                        <p>No transactions yet</p>
                    </div>
                    <?php else: ?>
                    <div class="table-responsive">
                        <table class="table table-sm table-hover mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th>Date</th>
                                    <th>Type</th>
                                    <th>Amount</th>
                                    <th>Balance After</th>
                                    <th>Description</th>
                                    <th>Txn ID</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($transactions as $t): 
                                    $isCredit = ($t['type'] ?? '') === 'credit';
                                    $typeColor = $isCredit ? 'success' : 'danger';
                                    $typeIcon = $isCredit ? 'arrow-down' : 'arrow-up';
                                    $amountColor = $isCredit ? 'text-success' : 'text-danger';
                                ?>
                                <tr>
                                    <td style="font-size:0.78rem;"><?= !empty($t['created_at']) ? date('d M Y, g:i A', strtotime($t['created_at'])) : '—' ?></td>
                                    <td>
                                        <span class="badge bg-<?= $typeColor ?>">
                                            <i class="fas fa-<?= $typeIcon ?>"></i>
                                            <?= ucfirst($t['type'] ?? '') ?>
                                        </span>
                                    </td>
                                    <td class="<?= $amountColor ?> fw-bold">
                                        <?= $isCredit ? '+' : '-' ?>₹<?= number_format((float)($t['amount'] ?? 0)) ?>
                                    </td>
                                    <td>₹<?= number_format((float)($t['balance_after'] ?? 0)) ?></td>
                                    <td style="font-size:0.78rem;"><?= htmlspecialchars($t['description'] ?? '—') ?></td>
                                    <td style="font-size:0.72rem;"><?= htmlspecialchars($t['txn_id'] ?? '—') ?></td>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                    <?php endif; ?>
                </div>
                <?php if (!empty($transactions)): ?>
                <div class="card-footer text-center">
                    <a href="<?= $bp ?>/super-admin/wallet/history?id=<?= (int)$branch['id'] ?>" class="btn btn-info btn-sm text-white">
                        <i class="fas fa-list"></i> View Full History
                    </a>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>
