<?php
include 'header.php';

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

require_once 'db/config.php';
$db = db();
$stmt = $db->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$_SESSION['user_id']]);
$user = $stmt->fetch();

$message = '';
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $amount = $_POST['amount'];
    $address = $_POST['address'];
    $network = $_POST['network'];
    $trading_pass = $_POST['trading_password'];

    if ($trading_pass !== $user['trading_password']) {
        $error = "Incorrect trading password";
    } elseif ($amount > $user['balance']) {
        $error = "Insufficient balance";
    } elseif ($amount < 10) {
        $error = "Minimum withdrawal amount is 10 USDT";
    } else {
        // Process withdrawal (simplified)
        $db->beginTransaction();
        try {
            $stmt = $db->prepare("UPDATE users SET balance = balance - ? WHERE id = ?");
            $stmt->execute([$amount, $_SESSION['user_id']]);
            
            // Log as a special type of order or transaction
            $stmt = $db->prepare("INSERT INTO orders (user_id, type, amount, currency, account_info, status) VALUES (?, 'usdt', ?, 'USDT', ?, 'pending')");
            $stmt->execute([$_SESSION['user_id'], $amount, "Network: $network, Address: $address"]);
            
            $db->commit();
            $message = "Withdrawal request submitted successfully. Please wait for audit.";
            // Refresh user data
            $stmt = $db->prepare("SELECT * FROM users WHERE id = ?");
            $stmt->execute([$_SESSION['user_id']]);
            $user = $stmt->fetch();
        } catch (Exception $e) {
            $db->rollBack();
            $error = "System error, please try again later";
        }
    }
}
?>

<main style="padding: 40px 20px; background: #0b0e11; min-height: calc(100vh - 64px);">
    <div style="max-width: 800px; margin: 0 auto;">
        
        <a href="profile.php" class="back-btn"><i class="fas fa-arrow-left"></i> <?php echo __('nav_profile'); ?></a>

        <div style="margin-bottom: 40px;">
            <h1 style="font-size: 2.5rem; font-weight: bold; margin-bottom: 10px;"><?php echo __('withdraw_assets'); ?></h1>
            <p style="color: var(--text-muted);"><?php echo __('withdraw_tip'); ?></p>
        </div>

        <?php if($message): ?>
            <div style="background: rgba(14,203,129,0.1); color: var(--success-color); padding: 15px; border-radius: 8px; margin-bottom: 20px; border: 1px solid var(--success-color);">
                <i class="fas fa-check-circle"></i> <?php echo $message; ?>
            </div>
        <?php endif; ?>

        <?php if($error): ?>
            <div style="background: rgba(246,70,93,0.1); color: var(--danger-color); padding: 15px; border-radius: 8px; margin-bottom: 20px; border: 1px solid var(--danger-color);">
                <i class="fas fa-exclamation-circle"></i> <?php echo $error; ?>
            </div>
        <?php endif; ?>

        <div style="display: grid; grid-template-columns: 1.5fr 1fr; gap: 30px;">
            <div style="background: var(--card-bg); padding: 35px; border-radius: 24px; border: 1px solid var(--border-color);">
                <form method="POST">
                    <div style="margin-bottom: 25px;">
                        <label style="display: block; margin-bottom: 10px; color: var(--text-muted); font-size: 14px;"><?php echo __('select_network'); ?></label>
                        <select name="network" style="width: 100%; padding: 14px; background: #161a1e; border: 1px solid var(--border-color); color: white; border-radius: 12px; outline: none;">
                            <option value="TRC20">USDT - TRC20 (Recommended)</option>
                            <option value="ERC20">USDT - ERC20</option>
                            <option value="BEP20">USDT - BEP20 (BSC)</option>
                        </select>
                    </div>

                    <div style="margin-bottom: 25px;">
                        <label style="display: block; margin-bottom: 10px; color: var(--text-muted); font-size: 14px;"><?php echo __('withdraw_address'); ?></label>
                        <input type="text" name="address" placeholder="Paste your USDT wallet address" required style="width: 100%; padding: 14px; background: #161a1e; border: 1px solid var(--border-color); color: white; border-radius: 12px; outline: none;">
                    </div>

                    <div style="margin-bottom: 25px;">
                        <label style="display: block; margin-bottom: 10px; color: var(--text-muted); font-size: 14px;"><?php echo __('amount'); ?></label>
                        <div style="position: relative;">
                            <input type="number" name="amount" id="withdraw-amount" placeholder="Min. 10" step="0.01" required style="width: 100%; padding: 14px; background: #161a1e; border: 1px solid var(--border-color); color: white; border-radius: 12px; outline: none;">
                            <span onclick="document.getElementById('withdraw-amount').value = '<?php echo $user['balance']; ?>'" style="position: absolute; right: 20px; top: 50%; transform: translateY(-50%); color: var(--primary-color); font-weight: bold; cursor: pointer; font-size: 12px;"><?php echo __('max'); ?></span>
                        </div>
                        <div style="margin-top: 8px; font-size: 12px; color: var(--text-muted);">
                            <?php echo __('available_balance'); ?>: <span style="color: white; font-weight: bold;"><?php echo number_format($user['balance'], 2); ?> USDT</span>
                        </div>
                    </div>

                    <div style="margin-bottom: 30px;">
                        <label style="display: block; margin-bottom: 10px; color: var(--text-muted); font-size: 14px;"><?php echo __('trading_password'); ?></label>
                        <input type="password" name="trading_password" placeholder="Enter 6-digit trading password" required style="width: 100%; padding: 14px; background: #161a1e; border: 1px solid var(--border-color); color: white; border-radius: 12px; outline: none;">
                    </div>

                    <button type="submit" class="btn-primary" style="width: 100%; padding: 16px; font-size: 1.1rem; border-radius: 12px; background: var(--danger-color);"><?php echo __('submit_withdrawal'); ?></button>
                </form>
            </div>

            <div>
                <div style="background: rgba(240,185,11,0.05); padding: 25px; border-radius: 20px; border: 1px solid rgba(240,185,11,0.1); margin-bottom: 20px;">
                    <h4 style="color: #f0b90b; margin: 0 0 15px; display: flex; align-items: center; gap: 10px;"><i class="fas fa-exclamation-triangle"></i> <?php echo __('withdrawal_tips'); ?></h4>
                    <ul style="color: var(--text-muted); font-size: 13px; line-height: 1.8; padding-left: 20px; margin: 0;">
                        <li><?php echo __('withdrawal_tip_1'); ?></li>
                        <li><?php echo __('withdrawal_tip_2'); ?></li>
                        <li><?php echo __('withdrawal_tip_3'); ?></li>
                        <li><?php echo __('withdrawal_tip_4'); ?></li>
                    </ul>
                </div>

                <div style="background: var(--card-bg); padding: 25px; border-radius: 20px; border: 1px solid var(--border-color);">
                    <h4 style="margin: 0 0 15px;"><?php echo __('recent_history'); ?></h4>
                    <div style="color: var(--text-muted); font-size: 13px; text-align: center; padding: 20px 0;">
                        <?php echo __('no_records'); ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</main>

<?php include 'footer.php'; ?>