<?php
$page_title = "Withdraw Funds";
require_once '../config/config.php';

require_once '../includes/user_auth.php';
require_once '../includes/functions.php';



$db = Database::getInstance()->connect();
if (!isset($auth)) {
    $auth = new Auth();
}
$user_id = $auth->getCurrentUserId();
if (!$user_id) {
    header("Location: ../login.php");
    exit();
}

// Get user balance and status
$user_stmt = $db->prepare("SELECT balance, account_status, kyc_status FROM users WHERE id = ?");
$user_stmt->execute([$user_id]);
$user = $user_stmt->fetch();

if ($user['account_status'] != 'active') {
    $_SESSION['error'] = "Your account is not active. Please contact support.";
    header('Location: dashboard.php');
    exit();
}

if ($user['kyc_status'] != 'verified') {
    $_SESSION['error'] = "KYC verification required before making withdrawals.";
    header('Location: profile.php');
    exit();
}

// Get withdrawal settings
$settings_stmt = $db->query("SELECT * FROM settings WHERE `key` IN ('min_withdrawal', 'max_withdrawal', 'withdrawal_fee', 'withdrawal_methods')");
$settings = [];
while ($row = $settings_stmt->fetch()) {
    $settings[$row['key']] = $row['value'];
}

$min_withdrawal = floatval($settings['min_withdrawal'] ?? 100.00);
$max_withdrawal = floatval($settings['max_withdrawal'] ?? 1000000.00);
$withdrawal_fee = floatval($settings['withdrawal_fee'] ?? 0.05); // 5%
$withdrawal_methods = ['crypto']; //json_decode($settings['withdrawal_methods'] ?? '["bank","crypto","paypal"]', true);

// Get recent withdrawals
$withdrawals_stmt = $db->prepare("
    SELECT * FROM transactions 
    WHERE user_id = ? AND type = 'withdrawal'
    ORDER BY created_at DESC 
    LIMIT 10
");
$withdrawals_stmt->execute([$user_id]);
$recent_withdrawals = $withdrawals_stmt->fetchAll();

// Handle withdrawal request
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['withdraw'])) {
    $amount = floatval($_POST['amount']);
    $method = $_POST['method'];
    $crypto_wallet_address = trim($_POST['crypto_wallet_address'] ?? '');
    $paypal_wallet_address = trim($_POST['paypal_wallet_address'] ?? '');
    $wallet_address = ($method == 'crypto') ? $crypto_wallet_address : $paypal_wallet_address;
    $bank_name = trim($_POST['bank_name'] ?? '');
    $account_name = trim($_POST['account_name'] ?? '');
    $account_number = trim($_POST['account_number'] ?? '');
    $routing_number = trim($_POST['routing_number'] ?? '');
    $swift_code = trim($_POST['swift_code'] ?? '');
    $notes = trim($_POST['notes'] ?? '');
    
    // Validation
    $errors = [];
    
    if ($amount < $min_withdrawal) {
        $errors[] = "Minimum withdrawal amount is $" . number_format($min_withdrawal, 2);
    }
    
    if ($amount > $max_withdrawal) {
        $errors[] = "Maximum withdrawal amount is $" . number_format($max_withdrawal, 2);
    }
    
    if ($amount > $user['balance']) {
        $errors[] = "Insufficient balance. Available: $" . number_format($user['balance'], 2);
    }
    
    if (!in_array($method, $withdrawal_methods)) {
        $errors[] = "Invalid withdrawal method";
    }
    
    // Method-specific validation
    if ($method == 'crypto' && empty($wallet_address)) {
        $errors[] = "Wallet address is required for crypto withdrawals";
    }
    
    if ($method == 'bank' && (empty($bank_name) || empty($account_name) || empty($account_number))) {
        $errors[] = "Bank details are required for bank transfers";
    }
    
    if (empty($errors)) {
        try {
            $db->beginTransaction();
            
            // Calculate fee and net amount
            $fee = $amount * $withdrawal_fee;
            $net_amount = $amount - $fee;
            
            // Generate reference
            $reference = 'WTH' . time() . rand(1000, 9999);
            
            // Prepare details based on method
            $details = [];
            if ($method == 'crypto') {
                $details = [
                    'wallet_address' => $wallet_address,
                    'network' => $_POST['network'] ?? 'ERC20'
                ];
            } elseif ($method == 'bank') {
                $details = [
                    'bank_name' => $bank_name,
                    'account_name' => $account_name,
                    'account_number' => $account_number,
                    'routing_number' => $routing_number,
                    'swift_code' => $swift_code
                ];
            } elseif ($method == 'paypal') {
                $details = [
                    'paypal_email' => $wallet_address
                ];
            }
            
            if (!empty($notes)) {
                $details['notes'] = $notes;
            }
            
            // Create withdrawal transaction
            $tx_stmt = $db->prepare("
                INSERT INTO transactions (
                    user_id, type, amount, fee, net_amount, method, 
                    description, reference, status, details
                ) VALUES (?, 'withdrawal', ?, ?, ?, ?, ?, ?, 'pending', ?)
            ");
            
            $description = "Withdrawal request via " . ucfirst($method);
            if ($method == 'crypto') {
                $description .= " to " . substr($wallet_address, 0, 8) . '...';
            }
            
            $tx_stmt->execute([
                $user_id, $amount, $fee, $net_amount, $method,
                $description, $reference, json_encode($details)
            ]);
            
            // Update user balance
            $balance_stmt = $db->prepare("UPDATE users SET balance = balance - ? WHERE id = ?");
            $balance_stmt->execute([$amount, $user_id]);
            
            // Log activity
            $log_stmt = $db->prepare("
                INSERT INTO activity_logs (user_id, action, description, ip_address, user_agent, details, created_at)
                VALUES (?, 'withdrawal_request', 'Withdrawal request submitted', ?, ?, ?, NOW())
            ");

            $log_stmt->execute([
                $user_id,
                $_SERVER['REMOTE_ADDR'] ?? '',
                $_SERVER['HTTP_USER_AGENT'] ?? '',
                json_encode([
                    'amount' => $amount,
                    'method' => $method,
                    'reference' => $reference,
                    'net_amount' => $net_amount,
                    'fee' => $fee
                ])
            ]);
            
            $db->commit();
            
            $_SESSION['success'] = "Withdrawal request submitted successfully! Reference: " . $reference;
            header('Location: withdrawal.php');
            exit();
            
        } catch (Exception $e) {
            $db->rollBack();
            $errors[] = "Failed to process withdrawal: " . $e->getMessage();
        }
    }
}




?>


     <?php require_once '../includes/header_user.php'; ?>
     
<div class="container-fluid">
    <!-- Page Header -->
    <div class="mb-6">
        <h1 class="text-2xl font-bold">Withdraw Funds</h1>
        <p class="text-gray-600">Withdraw your earnings to your preferred payment method</p>
    </div>

    <!-- Messages -->
    <?php if (isset($_SESSION['success'])): ?>
    <div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded mb-4">
        <?php echo $_SESSION['success']; unset($_SESSION['success']); ?>
    </div>
    <?php endif; ?>
    
    <?php if (isset($_SESSION['error'])): ?>
    <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4">
        <?php echo $_SESSION['error']; unset($_SESSION['error']); ?>
    </div>
    <?php endif; ?>
    
    <?php if (!empty($errors)): ?>
    <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4">
        <ul class="list-disc list-inside">
            <?php foreach ($errors as $error): ?>
            <li><?php echo $error; ?></li>
            <?php endforeach; ?>
        </ul>
    </div>
    <?php endif; ?>

    <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
        <!-- Withdrawal Form -->
        <div class="lg:col-span-2">
            <div class="bg-white rounded-xl shadow p-6">
                <h3 class="text-lg font-bold mb-4">Request Withdrawal</h3>
                
                <!-- Balance Info -->
                <div class="bg-gradient-to-r from-blue-500 to-purple-600 text-white rounded-lg p-6 mb-6">
                    <div class="flex justify-between items-center">
                        <div>
                            <p class="text-sm">Available Balance</p>
                            <h2 class="text-3xl font-bold">$<?php echo number_format($user['balance'], 2); ?></h2>
                            <p class="text-sm opacity-90">Min: $<?php echo number_format($min_withdrawal, 2); ?> | Max: $<?php echo number_format($max_withdrawal, 2); ?></p>
                        </div>
                        <i class="fas fa-wallet text-4xl opacity-80"></i>
                    </div>
                </div>
                
                <!-- Withdrawal Form -->
                <form method="POST" action="" class="space-y-6">
                    <!-- Amount -->
                    <div>
                        <label class="block text-gray-700 font-medium mb-2">Amount to Withdraw ($)</label>
                        <input type="number" name="amount" step="0.01" min="0"
                               class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 text-lg font-bold"
                               placeholder="0.00"
                               oninput="validateWithdrawalAmount()" id="withdrawAmount">
                        <div id="withdrawalValidation" class="text-sm mt-2"></div>
                        <div class="flex justify-between text-sm text-gray-600 mt-2">
                            <span>Available: $<?php echo number_format($user['balance'], 2); ?></span>
                            <button type="button" onclick="setMaxAmount()" class="text-blue-600 hover:text-blue-800">
                                Withdraw All
                            </button>
                        </div>
                    </div>
                    
                    <!-- Method Selection -->
                    <div>
                        <label class="block text-gray-700 font-medium mb-2">Withdrawal Method</label>
                        <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                            <?php foreach ($withdrawal_methods as $method): 
                                $icons = [
                                    'bank' => 'fas fa-university',
                                    'crypto' => 'fab fa-bitcoin',
                                    'paypal' => 'fab fa-paypal',
                                    'skrill' => 'fas fa-money-bill-wave',
                                    'perfect_money' => 'fas fa-money-check'
                                ];
                                $icon = $icons[$method] ?? 'fas fa-wallet';
                            ?>
                            <label class="flex items-center p-4 border rounded-lg cursor-pointer hover:bg-gray-50">
                                <input type="radio" name="method" value="<?php echo $method; ?>" 
                                       class="mr-3" <?php echo $method == 'crypto' ? 'checked' : ''; ?>
                                       onchange="showMethodFields()">
                                <div>
                                    <i class="<?php echo $icon; ?> text-purple-600 text-xl mb-1"></i>
                                    <p class="font-medium"><?php echo ucfirst(str_replace('_', ' ', $method)); ?></p>
                                </div>
                            </label>
                            <?php endforeach; ?>
                        </div>
                        <p class="text-sm text-gray-500 mt-4">If your preferred withdrawal method is not listed, please <a href="support.php" class="text-purple-600 hover:underline">contact support</a>.</p>
                    </div>
                    
                    <!-- Method-specific fields -->
                    <div id="cryptoFields" class="hidden space-y-4">
                        <div>
                            <label class="block text-gray-700 font-medium mb-2">Wallet Address</label>
                            <input type="text" name="crypto_wallet_address"
                                   class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500"
                                   placeholder="Enter your cryptocurrency wallet address">
                        </div>
                        <div>
                            <label class="block text-gray-700 font-medium mb-2">Cryptocurrency</label>
                            <select name="network" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500">
                            <option value="BTC">Bitcoin</option>
                            <option value="ETH">Ethereum (ERC20)</option>
                            <option value="BCH">Bitcoin Cash</option>
                            <option value="DOGE">Dogecoin</option>
                           <!--   <option value="USDT_ERC20">USDT (ERC20)</option>-->
                           <!--  <option value="USDT_TRC20">USDT (TRC20)</option>-->
                            <!-- <option value="bank">Bank Transfer</option> -->
                            <!-- <option value="paypal">PayPal</option> -->
                            </select>
                        </div>
                    </div>
                    
                    <div id="bankFields" class="space-y-4">
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                            <div>
                                <label class="block text-gray-700 font-medium mb-2">Bank Name</label>
                                <input type="text" name="bank_name"
                                       class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500"
                                       placeholder="e.g., Chase Bank">
                            </div>
                            <div>
                                <label class="block text-gray-700 font-medium mb-2">Account Name</label>
                                <input type="text" name="account_name"
                                       class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500"
                                       placeholder="Name on account">
                            </div>
                        </div>
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                            <div>
                                <label class="block text-gray-700 font-medium mb-2">Account Number</label>
                                <input type="text" name="account_number"
                                       class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500"
                                       placeholder="Bank account number">
                            </div>
                            <div>
                                <label class="block text-gray-700 font-medium mb-2">Routing Number (Optional)</label>
                                <input type="text" name="routing_number"
                                       class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500"
                                       placeholder="For US banks only">
                            </div>
                        </div>
                        <div>
                            <label class="block text-gray-700 font-medium mb-2">SWIFT/BIC Code (Optional)</label>
                            <input type="text" name="swift_code"
                                   class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500"
                                   placeholder="For international transfers">
                        </div>
                    </div>
                    
                    <div id="paypalFields" class="hidden space-y-4">
                        <div>
                            <label class="block text-gray-700 font-medium mb-2">PayPal Email Address</label>
                            <input type="email" name="paypal_wallet_address"
                                   class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500"
                                   placeholder="your-email@paypal.com">
                        </div>
                    </div>
                    
                    <!-- Additional Notes -->
                    <div>
                        <label class="block text-gray-700 font-medium mb-2">Additional Notes (Optional)</label>
                        <textarea name="notes" rows="3"
                                  class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500"
                                  placeholder="Any special instructions..."></textarea>
                    </div>
                    
                    <!-- Summary -->
                    <div class="bg-gray-50 border border-gray-200 rounded-lg p-6">
                        <h4 class="font-bold mb-4">Withdrawal Summary</h4>
                        <div class="space-y-3">
                            <div class="flex justify-between">
                                <span class="text-gray-600">Withdrawal Amount:</span>
                                <span class="font-bold" id="withdrawalAmount">$0.00</span>
                            </div>
                            <div class="flex justify-between">
                                <span class="text-gray-600">Processing Fee (<?php echo ($withdrawal_fee * 100); ?>%):</span>
                                <span class="text-red-600 font-bold" id="withdrawalFee">$0.00</span>
                            </div>
                            <div class="flex justify-between border-t pt-3">
                                <span class="text-gray-600 font-semibold">You Will Receive:</span>
                                <span class="text-green-600 font-bold text-xl" id="netAmount">$0.00</span>
                            </div>
                            
                        </div>
                    </div>
                    
                    <!-- Submit -->
                    <div class="border-t pt-6">
                        <button type="submit" name="withdraw" 
                                class="w-full bg-gradient-to-r from-green-600 to-blue-600 text-white font-bold py-3 px-4 rounded-lg hover:opacity-90">
                            <i class="fas fa-paper-plane mr-2"></i>Submit Withdrawal Request
                        </button>
                        <p class="text-sm text-gray-500 text-center mt-3">
                            By submitting, you agree to our terms and conditions
                        </p>
                    </div>
                </form>
            </div>
        </div>
        
        <!-- Sidebar -->
        <div class="space-y-6">
            <!-- Recent Withdrawals -->
            <div class="bg-white rounded-xl shadow p-6">
                <h3 class="text-lg font-bold mb-4">Recent Withdrawals</h3>
                <div class="space-y-4">
                    <?php if (empty($recent_withdrawals)): ?>
                    <p class="text-gray-500 text-center py-4">No recent withdrawals</p>
                    <?php else: ?>
                        <?php foreach ($recent_withdrawals as $withdrawal): ?>
                        <div class="border-b pb-4 last:border-0 last:pb-0">
                            <div class="flex justify-between items-start">
                                <div>
                                    <p class="font-medium">$<?php echo number_format($withdrawal['amount'], 2); ?></p>
                                    <p class="text-sm text-gray-500"><?php echo date('M d', strtotime($withdrawal['created_at'])); ?></p>
                                </div>
                                <div class="text-right">
                                    <span class="px-2 py-1 rounded-full text-xs font-semibold 
                                        <?php echo $withdrawal['status'] == 'completed' ? 'bg-green-100 text-green-800' :
                                            ($withdrawal['status'] == 'pending' ? 'bg-yellow-100 text-yellow-800' :
                                            'bg-red-100 text-red-800'); ?>">
                                        <?php echo ucfirst($withdrawal['status']); ?>
                                    </span>
                                    <p class="text-xs text-gray-500 mt-1"><?php echo ucfirst($withdrawal['method']); ?></p>
                                </div>
                            </div>
                        </div>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </div>
                <?php if (!empty($recent_withdrawals)): ?>
                <div class="mt-4 text-center">
                    <a href="transactions.php?type=withdrawal" class="text-blue-600 hover:text-blue-800 text-sm">
                        View All Withdrawals →
                    </a>
                </div>
                <?php endif; ?>
            </div>
            
            <!-- Withdrawal Guidelines -->
            <div class="bg-blue-50 border border-blue-200 rounded-xl p-6">
                <h4 class="font-bold text-blue-800 mb-3">
                    <i class="fas fa-info-circle mr-2"></i>Important Information
                </h4>
                <ul class="space-y-2 text-sm text-blue-700">
                    <li>• Minimum withdrawal: $<?php echo number_format($min_withdrawal, 2); ?></li>
                    <li>• Maximum withdrawal: $<?php echo number_format($max_withdrawal, 2); ?></li>
                    <li>• Processing fee: <?php echo ($withdrawal_fee * 100); ?>%</li>
                    <li>• Contact support for urgent requests</li>
                </ul>
            </div>
            
            <!-- Quick Actions -->
            <div class="bg-white rounded-xl shadow p-6">
                <h4 class="font-bold mb-3">Quick Actions</h4>
                <div class="space-y-3">
                    <a href="deposit.php" class="block bg-green-50 hover:bg-green-100 text-green-700 px-4 py-3 rounded-lg">
                        <i class="fas fa-plus-circle mr-2"></i>Make Deposit
                    </a>
                    <a href="transactions.php" class="block bg-blue-50 hover:bg-blue-100 text-blue-700 px-4 py-3 rounded-lg">
                        <i class="fas fa-history mr-2"></i>View Transactions
                    </a>
                    <a href="settings.php" class="block bg-purple-50 hover:bg-purple-100 text-purple-700 px-4 py-3 rounded-lg">
                        <i class="fas fa-cog mr-2"></i>Account Settings
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
const withdrawalFee = <?php echo $withdrawal_fee; ?>;
const availableBalance = <?php echo $user['balance']; ?>;
const minWithdrawal = <?php echo $min_withdrawal; ?>;
const maxWithdrawal = <?php echo $max_withdrawal; ?>;

function setMaxAmount() {
    const maxAmount = Math.min(availableBalance, maxWithdrawal);
    document.querySelector('input[name="amount"]').value = maxAmount.toFixed(2);
    validateWithdrawalAmount();
}

function showMethodFields() {
    const method = document.querySelector('input[name="method"]:checked').value;
    
    // Hide all method fields
    document.getElementById('cryptoFields').classList.add('hidden');
    document.getElementById('bankFields').classList.add('hidden');
    document.getElementById('paypalFields').classList.add('hidden');
    
    // Show selected method fields
    if (method === 'crypto') {
        document.getElementById('cryptoFields').classList.remove('hidden');
    } else if (method === 'bank') {
        document.getElementById('bankFields').classList.remove('hidden');
    } else if (method === 'paypal') {
        document.getElementById('paypalFields').classList.remove('hidden');
    }
}

// Initialize
document.addEventListener('DOMContentLoaded', function() {
    calculateNetAmount();
    showMethodFields();
});

// Input validation
document.querySelector('input[name="amount"]').addEventListener('blur', function() {
    let amount = parseFloat(this.value) || 0;
    
    if (amount < minWithdrawal) {
        alert('Minimum withdrawal amount is $' + minWithdrawal.toFixed(2));
        this.value = minWithdrawal;
    }
    if (amount > availableBalance) {
        alert('Amount exceeds available balance');
        this.value = Math.min(availableBalance, maxWithdrawal);
    }
    
    calculateNetAmount();
});

// Form submission validation
document.querySelector('form').addEventListener('submit', function(e) {
    const amount = parseFloat(document.querySelector('input[name="amount"]').value) || 0;
    const method = document.querySelector('input[name="method"]:checked').value;
    
    if (amount < minWithdrawal) {
        e.preventDefault();
        alert('Minimum withdrawal amount is $' + minWithdrawal.toFixed(2));
        return false;
    }
    
    if (amount > availableBalance) {
        e.preventDefault();
        alert('Insufficient balance');
        return false;
    }
    
    // Method-specific validation
    if (method === 'crypto') {
        const cryptoFields = document.getElementById('cryptoFields');
        // Find wallet address input in crypto fields
        const walletInput = cryptoFields.querySelector('input[name="crypto_wallet_address"]');
        const walletAddress = walletInput ? walletInput.value.trim() : '';
        if (!walletAddress) {
            e.preventDefault();
            alert('Please enter your wallet address');
            return false;
        }
    }
    
    if (method === 'bank') {
        const bankName = document.querySelector('input[name="bank_name"]').value.trim();
        const accountName = document.querySelector('input[name="account_name"]').value.trim();
        const accountNumber = document.querySelector('input[name="account_number"]').value.trim();
        
        if (!bankName || !accountName || !accountNumber) {
            e.preventDefault();
            alert('Please fill all required bank details');
            return false;
        }
    }
    
    if (method === 'paypal') {
        const paypalFields = document.getElementById('paypalFields');
        // Find wallet address input in paypal fields
        const paypalInput = paypalFields.querySelector('input[name="paypal_wallet_address"]');
        const paypalEmail = paypalInput ? paypalInput.value.trim() : '';
        if (!paypalEmail || !paypalEmail.includes('@')) {
            e.preventDefault();
            alert('Please enter a valid PayPal email address');
            return false;
        }
    }
    
    return true;
});

function validateWithdrawalAmount() {
    const amount = parseFloat(document.getElementById('withdrawAmount').value) || 0;
    const minAmount = <?php echo $min_withdrawal; ?>;
    const maxAmount = <?php echo $max_withdrawal; ?>;
    const balance = <?php echo $user['balance']; ?>;
    const validationDiv = document.getElementById('withdrawalValidation');
    
    // Allow empty input
    if (amount === 0 || document.getElementById('withdrawAmount').value === '') {
        validationDiv.innerHTML = '';
        return;
    }
    
    // Check minimum
    if (amount < minAmount) {
        validationDiv.innerHTML = `<span class="text-orange-600">Minimum withdrawal: $${minAmount.toFixed(2)}</span>`;
        return;
    }
    
    // Check maximum
    if (amount > maxAmount) {
        validationDiv.innerHTML = `<span class="text-orange-600">Maximum withdrawal: $${maxAmount.toFixed(2)}</span>`;
        return;
    }
    
    // Check balance
    if (amount > balance) {
        validationDiv.innerHTML = `<span class="text-red-600">Exceeds available balance: $${balance.toFixed(2)}</span>`;
        return;
    }
    
    // Valid amount
    validationDiv.innerHTML = '<span class="text-green-600">✓ Valid amount</span>';
    calculateNetAmount();
}

function calculateNetAmount() {
    const amount = parseFloat(document.getElementById('withdrawAmount').value) || 0;
    const feePercent = <?php echo $withdrawal_fee * 100; ?>;
    const fee = amount * (feePercent / 100);
    const net = amount - fee;
    
    // Update summary display
    const amountDisplay = document.getElementById('withdrawalAmount');
    const feeDisplay = document.getElementById('withdrawalFee');
    const netDisplay = document.getElementById('netAmount');
    
    if (amountDisplay) amountDisplay.textContent = '$' + amount.toFixed(2);
    if (feeDisplay) feeDisplay.textContent = fee > 0 ? '$' + fee.toFixed(2) : '$0.00';
    if (netDisplay) netDisplay.textContent = net > 0 ? '$' + net.toFixed(2) : '$0.00';
}

// Initialize on page load
document.addEventListener('DOMContentLoaded', function() {
    validateWithdrawalAmount();
    showMethodFields();
});
</script>

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