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

// ─────────────────────────────────────────────
// Threshold — এই অঙ্কের চেয়ে বেশি withdraw request হলেই "বড়" ধরা হবে
// প্রয়োজনে এই সংখ্যাটা বদলে নিতে পারো
// ─────────────────────────────────────────────
$threshold = isset($_GET['threshold']) ? floatval($_GET['threshold']) : 5000;

$stmt = $conn->prepare("
    SELECT h.shonu, h.balakedara, h.motta, h.dinankavannuracisi, h.madari, h.tike,
           s.mobile
    FROM hintegedukolli h
    LEFT JOIN shonu_subjects s ON s.id = h.balakedara
    WHERE h.sthiti = 0 AND h.motta >= ?
    ORDER BY h.motta DESC
");
$stmt->bind_param("d", $threshold);
$stmt->execute();
$result = $stmt->get_result();
$alerts = [];
while ($row = $result->fetch_assoc()) { $alerts[] = $row; }
$stmt->close();

$totalPendingLarge = array_sum(array_column($alerts, 'motta'));
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Large Withdrawal Alerts - 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">
    <link rel="stylesheet" href="admin-gold-theme.css">
    <style>
        body { background: #0A0A0A; padding: 20px; }
        .alert-card {
            background: linear-gradient(135deg, #1F1408, #171512);
            border: 1.5px solid #E8544A;
            border-radius: 14px;
            padding: 16px 18px;
            margin-bottom: 12px;
            box-shadow: 0 0 16px rgba(232,84,74,.15);
        }
        .amount-big { font-size: 20px; font-weight: 800; color: #E8544A; }
        .meta { color: #8A8270; font-size: 12px; }
        .threshold-box {
            background: #171512; border: 1px solid #3A3226; color: #F5EFDC;
            border-radius: 10px; padding: 8px 14px;
        }
    </style>
</head>
<body>
<div class="container" style="max-width:720px;">
    <h3 style="color:#D4AF37;">⚠️ Large Withdrawal Alerts</h3>
    <p style="color:#8A8270; font-size:12.5px; margin-bottom:16px;">যেসব Withdraw request এখনো Pending (অনুমোদন হয়নি) এবং অঙ্কে বড়</p>

    <form method="GET" style="display:flex; gap:8px; margin-bottom:20px; align-items:center;">
        <label style="color:#8A8270; font-size:13px;">Threshold (৳):</label>
        <input type="number" name="threshold" class="threshold-box" value="<?= htmlspecialchars($threshold) ?>" style="width:150px;">
        <button type="submit" class="btn" style="background:#D4AF37; color:#1A1400; font-weight:700; border-radius:10px;">Filter</button>
    </form>

    <div style="background:#171512; border:1px solid rgba(212,175,55,.3); border-radius:12px; padding:14px 18px; margin-bottom:20px;">
        <span style="color:#8A8270; font-size:12.5px;">এই মুহূর্তে Pending বড় Withdraw মোট:</span>
        <div style="color:#D4AF37; font-size:22px; font-weight:800;">৳<?= number_format($totalPendingLarge, 2) ?>
            <span style="font-size:13px; color:#8A8270; font-weight:400;">(<?= count($alerts) ?>টা request)</span>
        </div>
    </div>

    <?php if (empty($alerts)): ?>
        <div style="text-align:center; color:#1FB979; padding:30px;">
            ✅ ৳<?= number_format($threshold) ?>-এর বেশি কোনো Pending Withdraw নেই। সব নিয়ন্ত্রণে আছে।
        </div>
    <?php else: ?>
        <?php foreach ($alerts as $a): ?>
            <div class="alert-card">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <div class="amount-big">৳<?= number_format($a['motta'], 2) ?></div>
                        <div class="meta">
                            📱 <?= htmlspecialchars($a['mobile'] ?: 'N/A') ?>
                            &nbsp;|&nbsp; Method: <?= htmlspecialchars($a['madari']) ?>
                            &nbsp;|&nbsp; সময়: <?= htmlspecialchars($a['dinankavannuracisi']) ?>
                        </div>
                    </div>
                    <a href="manage_withdraw.php" class="btn btn-sm" style="background:#D4AF37; color:#1A1400; font-weight:700; border-radius:8px;">
                        Review করো →
                    </a>
                </div>
            </div>
        <?php endforeach; ?>
    <?php endif; ?>

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