<?php
/** @var array $transactions */
/** @var string $dateFrom */
/** @var string $dateTo */
/** @var string $typeFilter */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
?>
<style>
.filter-card { background:#fff; border-radius:12px; padding:18px; margin-bottom:16px; box-shadow:0 2px 8px rgba(0,0,0,0.06); }
.filter-row { display:grid; grid-template-columns:repeat(auto-fit,minmax(160px,1fr)); gap:12px; align-items:end; }
.txn-table { width: 100%; border-collapse: collapse; font-size: 0.86rem; }
.txn-table th { background: #f8f9fa; padding: 12px; text-align: left; border-bottom: 2px solid #dee2e6; }
.txn-table td { padding: 10px; border-bottom: 1px solid #f0f0f0; vertical-align: middle; }
.badge-credit { background: #d4edda; color: #155724; padding: 4px 10px; border-radius: 12px; font-size: 0.75rem; font-weight: 600; }
.badge-debit { background: #f8d7da; color: #721c24; padding: 4px 10px; border-radius: 12px; font-size: 0.75rem; font-weight: 600; }
.badge-offline { background: #fff3cd; color: #856404; padding: 4px 10px; border-radius: 12px; font-size: 0.75rem; font-weight: 600; }
</style>

<div class="container-fluid py-4">
    <div style="margin-bottom:14px;">
        <a href="<?= $bp ?>/branch/wallet" style="color:#16a085;text-decoration:none;font-size:0.85rem;">← Back to Wallet</a>
    </div>
    
    <h2 style="color:#0d3b3b;margin-bottom:15px;"><i class="fas fa-history"></i> Transaction History</h2>

    <!-- Filters -->
    <div class="filter-card">
        <form method="GET" action="<?= $bp ?>/branch/wallet/history">
            <div class="filter-row">
                <div>
                    <label style="display:block;font-size:0.78rem;font-weight:600;color:#0d3b3b;margin-bottom:4px;">From Date</label>
                    <input type="date" name="date_from" value="<?= htmlspecialchars($dateFrom) ?>" style="width:100%;padding:8px;border:1px solid #ddd;border-radius:6px;">
                </div>
                <div>
                    <label style="display:block;font-size:0.78rem;font-weight:600;color:#0d3b3b;margin-bottom:4px;">To Date</label>
                    <input type="date" name="date_to" value="<?= htmlspecialchars($dateTo) ?>" style="width:100%;padding:8px;border:1px solid #ddd;border-radius:6px;">
                </div>
                <div>
                    <label style="display:block;font-size:0.78rem;font-weight:600;color:#0d3b3b;margin-bottom:4px;">Type</label>
                    <select name="type" style="width:100%;padding:8px;border:1px solid #ddd;border-radius:6px;">
                        <option value="">All Types</option>
                        <option value="credit" <?= $typeFilter==='credit'?'selected':'' ?>>Credit (Recharge)</option>
                        <option value="debit" <?= $typeFilter==='debit'?'selected':'' ?>>Debit (Spent)</option>
                    </select>
                </div>
                <div style="display:flex;gap:8px;">
                    <button type="submit" style="padding:8px 18px;background:#16a085;color:#fff;border:none;border-radius:6px;font-weight:600;cursor:pointer;">🔍 Filter</button>
                    <a href="<?= $bp ?>/branch/wallet/history" style="padding:8px 14px;background:#e0e0e0;color:#333;text-decoration:none;border-radius:6px;font-size:0.84rem;font-weight:600;display:inline-block;">✕ Clear</a>
                </div>
            </div>
        </form>
    </div>

    <!-- Table -->
    <div style="background:#fff;border-radius:12px;overflow:hidden;box-shadow:0 2px 8px rgba(0,0,0,0.06);">
        <?php if (empty($transactions)): ?>
            <div style="text-align:center;padding:50px;color:#999;">
                <i class="fas fa-inbox fa-3x mb-3"></i>
                <p>No transactions found for selected filters</p>
            </div>
        <?php else: ?>
            <div style="overflow-x:auto;">
                <table class="txn-table">
                    <thead>
                        <tr>
                            <th>Date & Time</th>
                            <th>Type</th>
                            <th>Mode</th>
                            <th>Amount</th>
                            <th>Balance After</th>
                            <th>Description / Certificate Ref</th>
                            <th>Student Name</th>
                            <th>Course</th>
                            <th>Txn ID</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($transactions as $t): 
                            $isCredit = ($t['type'] ?? '') === 'credit';
                            $modeBadge = '';
                            if (strpos($t['description'] ?? '', 'offline') !== false || strpos($t['description'] ?? '', 'Offline') !== false) {
                                $modeBadge = '<span class="badge-offline">Offline</span>';
                            } elseif ($isCredit) {
                                $modeBadge = '<span class="badge-credit">Recharge</span>';
                            } else {
                                $modeBadge = '<span class="badge-debit">Wallet</span>';
                            }
                        ?>
                        <tr>
                            <td style="font-size:0.78rem;">
                                <?= !empty($t['created_at']) ? date('d M Y, g:i A', strtotime($t['created_at'])) : '—' ?>
                            </td>
                            <td>
                                <?php if ($isCredit): ?>
                                    <span class="badge-credit"><i class="fas fa-arrow-down"></i> Credit</span>
                                <?php else: ?>
                                    <span class="badge-debit"><i class="fas fa-arrow-up"></i> Debit</span>
                                <?php endif; ?>
                            </td>
                            <td><?= $modeBadge ?></td>
                            <td style="font-weight:700;color:<?= $isCredit ? '#16a085' : '#e84118' ?>;">
                                <?= $isCredit ? '+' : '-' ?>₹<?= number_format((float)$t['amount'], 2) ?>
                            </td>
                            <td>₹<?= number_format((float)($t['balance_after'] ?? 0), 2) ?></td>
                            <td style="font-size:0.82rem;">
                                <?= htmlspecialchars($t['description'] ?? '—') ?>
                                <?php if (!empty($t['certificate_no'])): ?>
                                    <br><code style="font-size:0.72rem;color:#16a085;"><?= htmlspecialchars($t['certificate_no']) ?></code>
                                <?php endif; ?>
                            </td>
                            <td style="font-size:0.82rem;"><?= htmlspecialchars($t['student_name'] ?? '—') ?></td>
                            <td style="font-size:0.78rem;"><?= htmlspecialchars($t['course_name'] ?? '—') ?></td>
                            <td style="font-size:0.72rem;font-family:monospace;"><?= htmlspecialchars($t['txn_id'] ?? '—') ?></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>
