<?php
/** @var array $branch */
/** @var array $transactions */
/** @var float $totalRecharged */
/** @var float $totalSpent */
/** @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 Header -->
    <div style="background:linear-gradient(135deg,#2c1e4a,#6c3fe8);color:#fff;border-radius:12px;padding:20px;margin-bottom:20px;">
        <div class="d-flex justify-content-between align-items-center flex-wrap">
            <div>
                <h2 class="mb-1"><i class="fas fa-history"></i> Transaction History</h2>
                <p class="mb-0 opacity-90">
                    <strong><?= htmlspecialchars($branch['name'] ?? '') ?></strong>
                    (Code: <?= htmlspecialchars($branch['branch_code'] ?? '') ?>)
                </p>
            </div>
            <a href="<?= $bp ?>/super-admin/wallet/recharge?id=<?= (int)$branch['id'] ?>" 
               class="btn btn-success">
                <i class="fas fa-plus-circle"></i> Recharge
            </a>
        </div>
    </div>

    <!-- Stats -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card border-success text-center">
                <div class="card-body">
                    <h4 class="text-success mb-0">₹<?= number_format($currentBalance) ?></h4>
                    <small class="text-muted">Current Balance</small>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-primary text-center">
                <div class="card-body">
                    <h4 class="text-primary mb-0">₹<?= number_format($totalRecharged) ?></h4>
                    <small class="text-muted">Total Recharged</small>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-warning text-center">
                <div class="card-body">
                    <h4 class="text-warning mb-0">₹<?= number_format($totalSpent) ?></h4>
                    <small class="text-muted">Total Spent</small>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-info text-center">
                <div class="card-body">
                    <h4 class="text-info mb-0"><?= count($transactions) ?></h4>
                    <small class="text-muted">Total Transactions</small>
                </div>
            </div>
        </div>
    </div>

    <!-- Transactions Table -->
    <div class="card">
        <div class="card-header">
            <h5 class="mb-0"><i class="fas fa-list"></i> All Transactions</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 found</p>
            </div>
            <?php else: ?>
            <div class="table-responsive">
                <table class="table table-hover mb-0" id="txnTable">
                    <thead class="table-light">
                        <tr>
                            <th>Date & Time</th>
                            <th>Type</th>
                            <th>Amount</th>
                            <th>Balance After</th>
                            <th>Description</th>
                            <th>Reference</th>
                            <th>Txn ID</th>
                            <th>By</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';
                            $refLink = '';
                            if (!empty($t['certificate_no'])) {
                                $refLink = '<a href="' . $bp . '/super-admin/documents/' . (int)$t['reference_id'] . '" target="_blank" style="font-size:0.78rem;">' . htmlspecialchars($t['certificate_no']) . '</a>';
                                if (!empty($t['student_name'])) {
                                    $refLink .= '<br><small class="text-muted">' . htmlspecialchars($t['student_name']) . '</small>';
                                }
                            }
                        ?>
                        <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.78rem;"><?= $refLink ?: '—' ?></td>
                            <td style="font-size:0.72rem;font-family:monospace;"><?= htmlspecialchars($t['txn_id'] ?? '—') ?></td>
                            <td style="font-size:0.72rem;"><?= htmlspecialchars($t['created_by'] ?? 'System') ?></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<script>
// Simple search filter
document.addEventListener('DOMContentLoaded', function() {
    const table = document.getElementById('txnTable');
    if (!table) return;
    
    // Add search box
    const searchBox = document.createElement('input');
    searchBox.type = 'text';
    searchBox.className = 'form-control mb-3';
    searchBox.placeholder = '🔍 Search by description, txn ID, student name...';
    searchBox.style.maxWidth = '400px';
    table.parentElement.insertBefore(searchBox, table.parentElement.firstChild);
    
    searchBox.addEventListener('input', function() {
        const query = this.value.toLowerCase();
        const rows = table.querySelectorAll('tbody tr');
        rows.forEach(row => {
            const text = row.textContent.toLowerCase();
            row.style.display = text.includes(query) ? '' : 'none';
        });
    });
});
</script>
