<?php
// --- 1. SYSTEM GUARD & CACHE START ---
$guard_path = file_exists('includes/system_guard.php') ? 'includes/system_guard.php' : '../includes/system_guard.php';
if (file_exists($guard_path)) { require_once $guard_path; }

session_start();

// Database Connection
$db_path = file_exists('includes/db.php') ? 'includes/db.php' : '../includes/db.php';
if (file_exists($db_path)) {
    require $db_path;
} else {
    $conn = new mysqli('localhost', 'root', '', 'bajipari');
}

// 2. Track Visitor
if (function_exists('track_visitor')) { track_visitor($conn); }

// 3. Start Cache
$cache_file = "";
if (function_exists('start_cache_system')) {
    $cache_file = start_cache_system(true);
}

// Check Login
if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit();
}

// 4. Fetch Settings & Promotions
$settings = $conn->query("SELECT * FROM settings WHERE id=1")->fetch_assoc();
$uid = $_SESSION['user_id'];

// Promotions Fetch
$promotions = [];
$promo_q = $conn->query("SELECT id, title FROM promotions ORDER BY id DESC");
if ($promo_q && $promo_q->num_rows > 0) {
    while ($row = $promo_q->fetch_assoc()) {
        $promotions[] = $row;
    }
}

// Pending Count
$pending_count = 0;
$table_check = $conn->query("SHOW TABLES LIKE 'transactions_fake'");
if($table_check && $table_check->num_rows > 0) {
    $res = $conn->query("SELECT COUNT(*) as cnt FROM transactions_fake WHERE user_id=$uid AND type='deposit' AND status='pending'");
    if($res) { $pending_count = $res->fetch_assoc()['cnt']; }
}
?>

<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>Funds - <?php echo $settings['site_name']; ?></title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">

    <style>
        body { 
            background-color: #121619; 
            color: #d1d5db;
            font-family: 'Roboto', sans-serif;
            padding-bottom: 140px; 
            -webkit-tap-highlight-color: transparent;
        }

        /* --- Tabs --- */
        .tab-active { background-color: #facc15; color: black; font-weight: bold; border-radius: 4px 0 0 4px; }
        .tab-inactive { background-color: #1c2227; color: white; font-weight: bold; border-radius: 0 4px 4px 0; }

        /* --- Promotion Box --- */
        .promo-box {
            background-color: #232b32;
            border-radius: 4px;
            display: flex; justify-content: space-between; align-items: center;
            padding: 10px 12px;
            font-size: 13px;
            color: white;
            margin-bottom: 20px;
            border: 1px solid #2f3840;
            position: relative;
            cursor: pointer;
        }
        
        /* Dropdown Menu */
        .promo-dropdown {
            display: none;
            position: absolute;
            top: 100%; left: 0; width: 100%;
            background-color: #232b32;
            border: 1px solid #2f3840;
            border-top: none;
            border-radius: 0 0 4px 4px;
            z-index: 50;
            max-height: 200px;
            overflow-y: auto;
        }
        .promo-dropdown.show { display: block; }
        .promo-item {
            padding: 10px 12px;
            border-bottom: 1px solid #2f3840;
            color: #d1d5db;
            transition: background 0.2s;
        }
        .promo-item:hover { background-color: #2f3b43; color: #facc15; }
        .promo-item:last-child { border-bottom: none; }

        /* --- Section Titles --- */
        .sec-title { border-left: 3px solid #facc15; padding-left: 8px; font-size: 15px; color: #e5e7eb; margin-bottom: 12px; font-weight: 500; }

        /* --- Payment Method Cards --- */
        .pay-card {
            background-color: #1c2227; border: 1px solid #2f3840; border-radius: 6px;
            height: 75px; display: flex; flex-direction: column; align-items: center; justify-content: center;
            position: relative; cursor: pointer; transition: all 0.2s;
        }
        .pay-card img { height: 35px; width: auto; margin-bottom: 4px; object-fit: contain; }
        .pay-card span { font-size: 11px; color: #9ca3af; font-weight: 500; }
        .pay-card.active { border: 1px solid #facc15; background-color: #1c2227; }
        .pay-card.active::after {
            content: '\f00c'; font-family: 'Font Awesome 5 Free'; font-weight: 900;
            position: absolute; bottom: 0; right: 0; background: #facc15; color: black;
            font-size: 8px; padding: 2px 5px; border-top-left-radius: 6px;
        }

        /* Red Badges */
        .badge-offer {
            position: absolute; top: -8px; right: -5px; background-color: #ef4444; color: white;
            font-size: 10px; font-weight: bold; padding: 1px 5px; border-radius: 2px; z-index: 10;
        }
        .badge-offer::before {
            content: ''; position: absolute; bottom: -4px; left: 3px; border-top: 4px solid #ef4444; border-right: 4px solid transparent;
        }

        /* Sub Button */
        .sub-btn-outline {
            border: 1px solid #facc15; color: #facc15; text-align: center; padding: 10px;
            font-size: 13px; border-radius: 6px; margin-top: 15px; width: 45%; cursor: pointer; font-weight: 500;
        }

        /* Amount Buttons */
        .amt-btn {
            background-color: #232a31; border: 1px solid #2f3840; color: white;
            padding: 12px 0; border-radius: 4px; font-size: 13px; font-weight: 600; transition: 0.2s;
        }
        .amt-btn.active { border-color: #facc15; color: #facc15; }

        /* Input Field */
        .input-wrap {
            background-color: #232a31; border-radius: 4px; display: flex; justify-content: space-between; align-items: center;
            padding: 12px 15px; margin-top: 15px; border: 1px solid #2f3840;
        }
        .input-wrap input {
            background: transparent; border: none; outline: none; text-align: right; color: #fff; font-size: 18px; width: 100%; font-weight: bold;
        }
        .input-wrap input::placeholder { color: #4b5563; }

        /* Submit Button */
        .submit-area {
            position: fixed; bottom: 60px; left: 0; width: 100%; background-color: #121619; padding: 10px 15px; z-index: 40;
        }
        .btn-submit {
            background-color: #facc15; color: black; width: 100%; padding: 12px; border-radius: 6px; font-weight: bold;
            font-size: 16px; text-align: center; border: none; transition: 0.3s;
        }
        .btn-submit:disabled { background-color: #eab308; opacity: 0.5; cursor: not-allowed; }

        /* Bottom Nav */
        .bottom-nav { background-color: #000; border-top: 1px solid #222; }
        .nav-item.active { color: #facc15; } .nav-item { color: #6b7280; }
        .nav-deposit-icon {
            width: 45px; height: 45px; background-color: #232a31; border-radius: 50%;
            display: flex; align-items: center; justify-content: center; border: 4px solid #121619;
            margin-top: -20px; color: white; box-shadow: 0 -2px 5px rgba(0,0,0,0.5);
        }
        .nav-item.active .nav-deposit-icon { color: #facc15; }
        .nav-item.active .nav-deposit-icon i { filter: drop-shadow(0 0 5px rgba(250, 204, 21, 0.5)); }
    </style>
</head>
<body>

    <div class="bg-[#121619] p-4 pb-2 sticky top-0 z-50">
        <div class="flex justify-between items-center mb-4 relative">
            <h2 class="text-white text-lg font-bold absolute left-1/2 transform -translate-x-1/2">Funds</h2>
            <div class="flex-1"></div>
            <button onclick="window.history.back()" class="text-white text-xl"><i class="fas fa-times"></i></button>
        </div>
        
        <div class="flex">
            <div class="flex-1 text-center py-2.5 tab-active cursor-pointer">Deposit</div>
            <a href="withdraw.php" class="flex-1 text-center py-2.5 tab-inactive block decoration-none">Withdrawal</a>
        </div>
    </div>

    <form action="payment_process.php" method="GET" id="depositForm">
        
        <div class="px-4 mt-4 pb-10"> 

            <div class="relative">
                <div class="promo-box" onclick="togglePromoDropdown()">
                    <div class="flex items-center gap-2">
                        <i class="fas fa-certificate text-white animate-pulse"></i> 
                        <span class="font-bold text-gray-200">Select Promotion</span>
                    </div>
                    <div class="flex items-center gap-2 text-gray-400 cursor-pointer text-xs">
                        <span id="selectedPromoText">Regular Deposit</span> <i class="fas fa-caret-down"></i>
                    </div>
                </div>
                
                <input type="hidden" name="promo_id" id="promoIdInput" value="">

                <div id="promoDropdown" class="promo-dropdown">
                    <div class="promo-item" onclick="selectPromo('Regular Deposit', '')">Regular Deposit</div>
                    <?php foreach($promotions as $promo): ?>
                        <div class="promo-item" onclick="selectPromo('<?php echo htmlspecialchars($promo['title']); ?>', '<?php echo $promo['id']; ?>')">
                            <?php echo htmlspecialchars($promo['title']); ?>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>

            <div class="mb-6">
               <div class="sec-title">Payment Method</div>
    
    <div class="grid grid-cols-3 gap-3">
        
        <label class="relative group">
            <input type="radio" name="method" value="bkash" class="hidden" checked onchange="updateUI('bKash')">
            <div class="pay-card active">
                <?php if(!empty($settings['deposit_bonus_percent']) && $settings['deposit_bonus_percent'] > 0): ?>
                    <div class="badge-offer">+<?php echo $settings['deposit_bonus_percent']; ?>%</div>
                <?php endif; ?>
                
                <img src="https://freelogopng.com/images/all_img/1656234745bkash-app-logo-png.png" alt="bKash">
                <span>bKash</span>
            </div>
        </label>

        <label class="relative group">
            <input type="radio" name="method" value="nagad" class="hidden" onchange="updateUI('Nagad')">
            <div class="pay-card">
                <?php if(!empty($settings['deposit_bonus_percent']) && $settings['deposit_bonus_percent'] > 0): ?>
                    <div class="badge-offer">+<?php echo $settings['deposit_bonus_percent']; ?>%</div>
                <?php endif; ?>

                <img src="https://freelogopng.com/images/all_img/1679248828Nagad-Logo-PNG.png" alt="Nagad">
                <span>Nagad</span>
            </div>
        </label>

        <label class="relative group">
            <input type="radio" name="method" value="rocket" class="hidden" onchange="updateUI('Rocket')">
            <div class="pay-card">
                <?php if(!empty($settings['deposit_bonus_percent']) && $settings['deposit_bonus_percent'] > 0): ?>
                    <div class="badge-offer">+<?php echo $settings['deposit_bonus_percent']; ?>%</div>
                <?php endif; ?>

                <img src="https://seeklogo.com/images/D/dutch-bangla-rocket-logo-B4D1CC458D-seeklogo.com.png" alt="Rocket">
                <span>Rocket</span>
            </div>
        </label>

        <label class="relative group">
            <input type="radio" name="method" value="upay" class="hidden" onchange="updateUI('UPay')">
            <div class="pay-card">
                <?php if(!empty($settings['deposit_bonus_percent']) && $settings['deposit_bonus_percent'] > 0): ?>
                    <div class="badge-offer">+<?php echo $settings['deposit_bonus_percent']; ?>%</div>
                <?php endif; ?>

                <img src="https://images.seeklogo.com/logo-png/64/1/upay-logo-png_seeklogo-642533.png" alt="UPay">
                <span>UPay</span>
            </div>
        </label>

        <label class="relative group">
            <input type="radio" name="method" value="usdt" class="hidden" onchange="updateUI('USDT TRC20')">
            <div class="pay-card">
                <img src="https://icons.iconarchive.com/icons/cjdowner/cryptocurrency-flat/512/Tether-USDT-icon.png" alt="USDT">
                <span>USDT TRC20</span>
            </div>
        </label>

        <label class="relative group">
            <input type="radio" name="method" value="btc" class="hidden" onchange="updateUI('BTC')">
            <div class="pay-card">
                <i class="fab fa-bitcoin text-[#f7931a] text-3xl mb-1"></i>
                <span>BTC</span>
            </div>
        </label>

        <label class="relative group">
            <input type="radio" name="method" value="eth" class="hidden" onchange="updateUI('ETH')">
            <div class="pay-card">
                <i class="fab fa-ethereum text-gray-400 text-3xl mb-1"></i>
                <span>ETH</span>
            </div>
        </label>
    </div>

    <div id="methodLabelBtn" class="sub-btn-outline">
        bKash Payment
    </div>

            <input type="hidden" name="channel" value="channel-auto">

            <div>
                <div class="flex justify-between items-end mb-3">
                    <div class="sec-title mb-0">Amount</div>
                    <div class="text-[10px] text-gray-500">৳ 200.00 - ৳ 30,000.00</div>
                </div>

                <div class="grid grid-cols-4 gap-2">
                    <?php $amounts = [200, 500, 1000, 5000, 10000, 15000, 20000, 25000];
                    foreach($amounts as $index => $amt): 
                        $activeClass = ($index === 0) ? 'active' : '';
                    ?>
                    <button type="button" class="amt-btn <?php echo $activeClass; ?>" onclick="setAmount(<?php echo $amt; ?>, this)">
                        <?php echo number_format($amt); ?>
                    </button>
                    <?php endforeach; ?>
                    <button type="button" class="amt-btn" onclick="setAmount(30000, this)">30,000</button>
                </div>

                <div class="input-wrap">
                    <span class="text-gray-400 font-bold text-lg">৳</span>
                    <input type="number" name="amount" id="amountInput" value="200" placeholder="0.00" min="200" max="30000" required>
                </div>
            </div>
            
        </div>

        <div class="submit-area">
            <button type="submit" id="submitBtn" class="btn-submit">Submit</button>
        </div>

    </form>

    <div class="fixed bottom-0 left-0 w-full h-[60px] bottom-nav z-50 flex justify-between items-center px-4">
        <a href="index.php" class="flex flex-col items-center justify-center nav-item w-1/4">
            <i class="fas fa-home text-xl mb-1"></i>
            <span class="text-[10px]">Home</span>
        </a>
        <a href="promotions.php" class="flex flex-col items-center justify-center nav-item w-1/4">
            <i class="fas fa-gift text-xl mb-1"></i>
            <span class="text-[10px]">Promotions</span>
        </a>
        <a href="deposit.php" class="flex flex-col items-center justify-center nav-item active w-1/4">
            <div class="nav-deposit-icon">
                 <i class="fas fa-wallet text-lg"></i>
            </div>
            <span class="text-[10px] mt-1 text-white">Deposit</span>
        </a>
        <a href="account.php" class="flex flex-col items-center justify-center nav-item w-1/4">
            <i class="fas fa-user text-xl mb-1"></i>
            <span class="text-[10px]">My Account</span>
        </a>
    </div>

    <script>
        // --- Promo Dropdown Logic ---
        function togglePromoDropdown() {
            const dd = document.getElementById('promoDropdown');
            dd.classList.toggle('show');
        }

        function selectPromo(title, id) {
            document.getElementById('selectedPromoText').innerText = title;
            document.getElementById('promoIdInput').value = id;
            document.getElementById('promoDropdown').classList.remove('show');
        }

        // Close dropdown if clicked outside
        window.onclick = function(event) {
            if (!event.target.closest('.promo-box')) {
                var dropdowns = document.getElementsByClassName("promo-dropdown");
                for (var i = 0; i < dropdowns.length; i++) {
                    var openDropdown = dropdowns[i];
                    if (openDropdown.classList.contains('show')) {
                        openDropdown.classList.remove('show');
                    }
                }
            }
        }

        // Method UI Update
        function updateUI(methodName) {
            document.querySelectorAll('.pay-card').forEach(card => card.classList.remove('active'));
            const checkedRadio = document.querySelector('input[name="method"]:checked');
            if(checkedRadio) {
                checkedRadio.nextElementSibling.classList.add('active');
            }
            document.getElementById('methodLabelBtn').innerText = methodName + " Payment";
        }

        // Amount Logic
        const amountInput = document.getElementById('amountInput');
        const submitBtn = document.getElementById('submitBtn');

        function setAmount(amount, btn) {
            amountInput.value = amount;
            document.querySelectorAll('.amt-btn').forEach(b => b.classList.remove('active'));
            btn.classList.add('active');
            validateForm();
        }

        amountInput.addEventListener('input', () => {
             document.querySelectorAll('.amt-btn').forEach(b => b.classList.remove('active'));
             validateForm();
        });

        function validateForm() {
            const val = parseInt(amountInput.value);
            if(val >= 200 && val <= 30000) {
                submitBtn.disabled = false;
                submitBtn.style.opacity = "1";
            } else {
                submitBtn.disabled = true;
                submitBtn.style.opacity = "0.5";
            }
        }
        
        validateForm();
    </script>
</body>
</html>
