<?php
/**
 * Admin Transactions
 */
require_once 'includes/header.php';

$filter = $_GET['type'] ?? 'all';
$where = "1=1";
if ($filter !== 'all') {
    $where = "type = '$filter'";
}

$transactions = dbFetchAll("SELECT t.*, u.email, u.first_name FROM transactions t LEFT JOIN users u ON t.user_id = u.id WHERE $where ORDER BY t.created_at DESC LIMIT 100");

$stats = [
    'deposits' => dbFetch("SELECT COALESCE(SUM(amount),0) as s FROM transactions WHERE type = 'deposit' AND status = 'completed'")['s'] ?? 0,
    'withdrawals' => dbFetch("SELECT COALESCE(SUM(amount),0) as s FROM transactions WHERE type = 'withdrawal' AND status = 'completed'")['s'] ?? 0,
    'trades' => dbFetch("SELECT COALESCE(SUM(amount),0) as s FROM transactions WHERE type IN ('trade_win','trade_loss')")['s'] ?? 0
];
?>

<div class="page-header">
    <h1 class="page-title">All Transactions</h1>
</div>

<div class="row mb-4">
    <div class="col-md-4">
        <div class="stat-card">
            <div class="stat-icon green"><i class="fas fa-arrow-down"></i></div>
            <div class="stat-value"><?= CURRENCY_SYMBOL ?><?= number_format($stats['deposits']) ?></div>
            <div class="stat-label">Total Deposits</div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="stat-card">
            <div class="stat-icon red"><i class="fas fa-arrow-up"></i></div>
            <div class="stat-value"><?= CURRENCY_SYMBOL ?><?= number_format($stats['withdrawals']) ?></div>
            <div class="stat-label">Total Withdrawals</div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="stat-card">
            <div class="stat-icon blue"><i class="fas fa-exchange-alt"></i></div>
            <div class="stat-value"><?= CURRENCY_SYMBOL ?><?= number_format($stats['trades']) ?></div>
            <div class="stat-label">Trade Volume</div>
        </div>
    </div>
</div>

<div class="card">
    <div class="card-header">
        <div class="d-flex gap-2">
            <a href="?type=all" class="btn btn-sm <?= $filter === 'all' ? 'btn-primary' : 'btn-secondary' ?>">All</a>
            <a href="?type=deposit" class="btn btn-sm <?= $filter === 'deposit' ? 'btn-primary' : 'btn-secondary' ?>">Deposits</a>
            <a href="?type=withdrawal" class="btn btn-sm <?= $filter === 'withdrawal' ? 'btn-primary' : 'btn-secondary' ?>">Withdrawals</a>
            <a href="?type=trade_win" class="btn btn-sm <?= $filter === 'trade_win' ? 'btn-primary' : 'btn-secondary' ?>">Wins</a>
            <a href="?type=trade_loss" class="btn btn-sm <?= $filter === 'trade_loss' ? 'btn-primary' : 'btn-secondary' ?>">Losses</a>
        </div>
    </div>
    <div class="card-body p-0">
        <div class="table-responsive" style="max-height: 500px; overflow-y: auto;">
            <table class="table">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>User</th>
                        <th>Type</th>
                        <th>Amount</th>
                        <th>Status</th>
                        <th>Date</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($transactions as $tx): ?>
                    <tr>
                        <td class="font-mono"><?= $tx['transaction_id'] ?? $tx['id'] ?></td>
                        <td><?= htmlspecialchars($tx['email'] ?? 'Unknown') ?></td>
                        <td>
                            <span class="badge badge-<?= strpos($tx['type'], 'deposit') !== false || $tx['type'] === 'trade_win' ? 'success' : 'danger' ?>">
                                <?= ucfirst(str_replace('_', ' ', $tx['type'])) ?>
                            </span>
                        </td>
                        <td class="font-mono"><?= CURRENCY_SYMBOL ?><?= number_format($tx['amount'] ?? 0, 2) ?></td>
                        <td>
                            <span class="badge badge-<?= $tx['status'] === 'completed' ? 'success' : ($tx['status'] === 'pending' ? 'warning' : 'danger') ?>">
                                <?= ucfirst($tx['status'] ?? 'pending') ?>
                            </span>
                        </td>
                        <td><?= date('d M Y H:i', strtotime($tx['created_at'])) ?></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php require_once 'includes/footer.php'; ?>
