<?php
require '../includes/db.php';
require 'includes/admin_auth.php';

$type = $_GET['type'] ?? '';
$status = $_GET['status'] ?? '';

$where = [];
$params = [];

if ($type) {
    $where[] = "t.type = ?";
    $params[] = $type;
}
if ($status) {
    $where[] = "t.status = ?";
    $params[] = $status;
}

$filterSQL = count($where) ? 'WHERE ' . implode(' AND ', $where) : '';

$query = "SELECT t.*, u.name FROM transactions t JOIN users u ON t.user_id = u.id $filterSQL ORDER BY t.id DESC";
$stmt = $pdo->prepare($query);
$stmt->execute($params);
$transactions = $stmt->fetchAll();
?>

<!DOCTYPE html>
<html>
<head>
    <title>Admin Transactions</title>
    <meta charset="UTF-8">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css" rel="stylesheet">
    <style>
        body {
            background-color: #f8f9fa;
            font-family: 'Segoe UI', sans-serif;
        }
        .page-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
        }
        .filter-card {
            background-color: #fff;
            border-radius: 10px;
            box-shadow: 0 2px 6px rgba(0,0,0,0.08);
            padding: 20px;
        }
        .badge-success {
            background-color: #28a745;
        }
        .badge-pending {
            background-color: #ffc107;
            color: #212529;
        }
        .badge-failed {
            background-color: #dc3545;
        }
        .table thead {
            background-color: #343a40;
            color: #fff;
        }
    </style>
</head>
<body class="p-4">

    <div class="container">
        <div class="page-header">
            <h2><i class="bi bi-credit-card-2-front"></i> All Transactions</h2>
            <a href="dashboard.php" class="btn btn-secondary"><i class="bi bi-house-door"></i> Back to Dashboard</a>
        </div>

        <div class="filter-card mb-4">
            <form method="GET" class="row g-3">
                <div class="col-md-4">
                    <select name="type" class="form-select">
                        <option value="">All Types</option>
                        <option value="payin" <?= $type == 'payin' ? 'selected' : '' ?>>Pay-In</option>
                        <option value="payout" <?= $type == 'payout' ? 'selected' : '' ?>>Pay-Out</option>
                    </select>
                </div>
                <div class="col-md-4">
                    <select name="status" class="form-select">
                        <option value="">All Statuses</option>
                        <option value="pending" <?= $status == 'pending' ? 'selected' : '' ?>>Pending</option>
                        <option value="success" <?= $status == 'success' ? 'selected' : '' ?>>Success</option>
                        <option value="failed" <?= $status == 'failed' ? 'selected' : '' ?>>Failed</option>
                    </select>
                </div>
                <div class="col-md-4 d-grid">
                    <button class="btn btn-dark"><i class="bi bi-filter"></i> Apply Filters</button>
                </div>
            </form>
        </div>

        <div class="table-responsive">
            <table class="table table-bordered table-hover align-middle">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>User</th>
                        <th>Type</th>
                        <th>Amount</th>
                        <th>Fee</th>
                        <th>Status</th>
                        <th>PayType</th>
                        <th>Ref</th>
                        <th>Date</th>
                        <th>Note</th>
                        <th>CallBack</th>
                    </tr>
                </thead>
                <tbody>
                <?php foreach ($transactions as $txn): ?>
                    <tr>
                        <td><?= $txn['id'] ?></td>
                        <td><?= htmlspecialchars($txn['name']) ?></td>
                        <td><span class="badge bg-info text-dark"><?= ucfirst($txn['type']) ?></span></td>
                        <td>₹<?= number_format($txn['amount'], 2) ?></td>
                        <td>₹<?= number_format($txn['fee'], 2) ?></td>
                        <td>
                            <?php
$statusClass = 'secondary';
if ($txn['status'] === 'success') {
    $statusClass = 'success';
} elseif ($txn['status'] === 'pending') {
    $statusClass = 'pending';
} elseif ($txn['status'] === 'failed') {
    $statusClass = 'failed';
}
?>
<span class="badge badge-<?= $statusClass ?>"><?= ucfirst($txn['status']) ?></span>

                        </td>
                        <td><?= htmlspecialchars($txn['payment_gateway']) ?></td>
                        <td>
    <?= htmlspecialchars($txn['reference'] ?? $txn['txn_id'] ?? '') ?>
</td>

                        <td><?= $txn['created_at'] ?></td>
                        <td>
   
    <?php if (!empty($txn['user_note'])): ?>
        <small class="text-muted"><?= htmlspecialchars($txn['user_note']) ?></small>
    <?php endif; ?>
</td>
<td>
    <?php if ($txn['webhook_sent'] == 1): ?>
        <span style="color: green;">&#10004;</span> <!-- Green Tick -->
    <?php else: ?>
        <span style="color: red;">&#10060;</span> <!-- Red Cross -->
    <?php endif; ?>
</td>

                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>

</body>
</html>
