<?php
session_start();
if (!isset($_SESSION['unohs'])) { header("Location: index.php"); exit(); }
include "conn.php";

$statusFilter = $_GET['status'] ?? 'all';
$where = $statusFilter !== 'all' ? "WHERE d.status = '" . $conn->real_escape_string($statusFilter) . "'" : '';

$orders = [];
$result = $conn->query("
    SELECT d.*, s.mobile
    FROM deposit_orders d
    LEFT JOIN shonu_subjects s ON s.id = d.user_id
    $where
    ORDER BY d.created_at DESC
    LIMIT 100
");
while ($row = $result->fetch_assoc()) { $orders[] = $row; }
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Auto Deposit List - RAJ365 Admin</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
    <style>
        body { background: #0A0A0A; color: #F5EFDC; padding: 20px; font-family: sans-serif; }
        .filters { display: flex; gap: 8px; margin-bottom: 16px; }
        .filters a { padding: 6px 14px; border-radius: 20px; background: #171512; color: #9691B8; text-decoration: none; font-size: 13px; border: 1px solid #3A3226; }
        .filters a.active { background: #D4AF37; color: #1A1400; font-weight: 700; }
        table { width: 100%; border-collapse: collapse; }
        th, td { padding: 10px; text-align: left; border-bottom: 1px solid #241C42; font-size: 13px; }
        th { color: #D4AF37; }
        .badge { padding: 3px 10px; border-radius: 999px; font-size: 11px; font-weight: 700; }
        .badge-success { background: rgba(31,185,121,.15); color: #1FB979; }
        .badge-pending { background: rgba(240,196,25,.15); color: #F0C419; }
        .badge-failed, .badge-expired { background: rgba(232,50,60,.15); color: #E8323C; }
    </style>
</head>
<body>
<div class="container">
    <h3 style="color:#D4AF37;">📲 Auto Deposit List (SMS-Based)</h3>

    <div class="filters">
        <a href="?status=all" class="<?= $statusFilter === 'all' ? 'active' : '' ?>">সব</a>
        <a href="?status=success" class="<?= $statusFilter === 'success' ? 'active' : '' ?>">Success</a>
        <a href="?status=pending" class="<?= $statusFilter === 'pending' ? 'active' : '' ?>">Pending</a>
        <a href="?status=failed" class="<?= $statusFilter === 'failed' ? 'active' : '' ?>">Failed</a>
        <a href="?status=expired" class="<?= $statusFilter === 'expired' ? 'active' : '' ?>">Expired</a>
    </div>

    <table>
        <tr>
            <th>Order ID</th><th>User</th><th>Amount</th><th>Gateway</th>
            <th>Transaction ID</th><th>Status</th><th>Time</th>
        </tr>
        <?php foreach ($orders as $o): ?>
        <tr>
            <td><?= htmlspecialchars($o['order_id']) ?></td>
            <td><?= htmlspecialchars($o['mobile'] ?? 'N/A') ?></td>
            <td>৳<?= number_format($o['amount'], 2) ?></td>
            <td><?= strtoupper($o['gateway']) ?></td>
            <td><?= htmlspecialchars($o['matched_trxid'] ?? '-') ?></td>
            <td><span class="badge badge-<?= $o['status'] ?>"><?= ucfirst($o['status']) ?></span></td>
            <td><?= $o['created_at'] ?></td>
        </tr>
        <?php endforeach; ?>
    </table>

    <a href="dashboard.php" style="color:#D4AF37;">← Dashboard-এ ফিরে যাও</a>
</div>
</body>
</html>
