<?php
require_once 'includes/header.php';

if (!$user) {
    header('Location: login.php');
    exit;
}

$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $amount = (float)($_POST['amount'] ?? 0);
    $address = $_POST['address'] ?? '';
    $security_password = $_POST['security_password'] ?? '';
    
    if ($amount <= 0) {
        $error = mt('Error');
    } elseif ($amount > $user['balance_usdt']) {
        $error = mt('Error');
    } elseif (!password_verify($security_password, $user['security_password'])) {
        $error = mt('Current password incorrect.');
    } else {
        try {
            db()->prepare("UPDATE users SET balance_usdt = balance_usdt - ? WHERE id = ?")->execute([$amount, $user['id']]);
            $success = mt('Success');
            $user['balance_usdt'] -= $amount;
        } catch (Exception $e) {
            $error = mt('Error');
        }
    }
}
?>

<div class="container my-5 py-5">
    <div class="row justify-content-center">
        <div class="col-md-6">
            <div class="card bg-dark border-secondary p-4 shadow-lg overflow-hidden" style="border-radius: 24px;">
                <div class="d-flex align-items-center mb-4">
                    <button onclick="history.back()" class="btn btn-dark rounded-circle me-3 border-secondary" style="width: 40px; height: 40px;">
                        <i class="fas fa-arrow-left"></i>
                    </button>
                    <h3 class="fw-bold mb-0 text-white"><i class="fas fa-arrow-up me-2 text-danger"></i> <?php echo mt('Withdraw'); ?></h3>
                </div>
                
                <?php if ($error): ?>
                    <div class="alert alert-danger bg-danger bg-opacity-10 border-0 text-danger" style="border-radius: 12px;"><?php echo $error; ?></div>
                <?php endif; ?>
                <?php if ($success): ?>
                    <div class="alert alert-success bg-success bg-opacity-10 border-0 text-success" style="border-radius: 12px;"><?php echo $success; ?></div>
                <?php endif; ?>

                <div class="bg-secondary bg-opacity-10 p-4 rounded-4 mb-4 border border-secondary border-opacity-25">
                    <div class="small text-muted mb-1"><?php echo mt('Available'); ?></div>
                    <h4 class="fw-bold text-white mb-0"><?php echo number_format($user['balance_usdt'], 2); ?> <span class="fs-6 text-muted">USDT</span></h4>
                </div>

                <form method="POST">
                    <div class="mb-3">
                        <label class="form-label text-muted small"><?php echo mt('Withdrawal Address'); ?></label>
                        <input type="text" name="address" class="form-control bg-dark text-white border-secondary py-3 px-3 rounded-3" placeholder="USDT (TRC20)" required>
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label text-muted small"><?php echo mt('Amount'); ?></label>
                        <div class="input-group">
                            <input type="number" name="amount" class="form-control bg-dark text-white border-secondary py-3 px-3 rounded-start-3" placeholder="Min 10.00" step="0.01" required>
                            <span class="input-group-text bg-dark border-secondary text-white rounded-end-3">USDT</span>
                        </div>
                    </div>

                    <div class="mb-4">
                        <label class="form-label text-muted small"><?php echo mt('Trading Password'); ?></label>
                        <input type="password" name="security_password" class="form-control bg-dark text-white border-secondary py-3 px-3 rounded-3" placeholder="6-digit" pattern="\d{6}" maxlength="6" required>
                    </div>

                    <button type="submit" class="btn btn-danger w-100 py-3 fw-bold fs-5 rounded-3 shadow-sm"><?php echo mt('Withdraw'); ?></button>
                </form>
            </div>
            
            <div class="mt-4 p-4 bg-secondary bg-opacity-10 rounded-4 border border-secondary border-opacity-25">
                <h6 class="text-white fw-bold mb-3 small text-uppercase" style="letter-spacing: 1px;"><?php echo mt('Notes'); ?></h6>
                <ul class="text-muted small mb-0 ps-3">
                    <li class="mb-2">Withdrawals are processed within 10-30 minutes.</li>
                    <li class="mb-2">Minimum withdrawal amount is 10 USDT.</li>
                    <li>Ensure the receiving address supports the TRC20 network.</li>
                </ul>
            </div>
        </div>
    </div>
</div>

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