<?php
require_once 'db/config.php';
require_once 'includes/i18n.php';
session_start();

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';
    
    $pdo = db();
    $stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
    $stmt->execute([$username]);
    $user = $stmt->fetch();
    
    if ($user && password_verify($password, $user['password'])) {
        // Capture and update IP
        $user_ip = $_SERVER['REMOTE_ADDR'];
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $user_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        }
        $pdo->prepare("UPDATE users SET last_ip = ? WHERE id = ?")->execute([$user_ip, $user['id']]);

        $_SESSION['user_id'] = $user['id'];
        $_SESSION['username'] = $user['username'];
        $_SESSION['uid'] = $user['uid'];
        header("Location: index.php");
        exit;
    } else {
        $error = "Invalid username or password.";
    }
}
?>

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

<main style="background: #0b0e11; min-height: calc(100vh - 64px); display: flex; align-items: center; justify-content: center; padding: 40px 20px;">
    <div style="width: 100%; max-width: 480px; background: var(--card-bg); padding: 50px; border-radius: 32px; border: 1px solid var(--border-color); box-shadow: 0 20px 40px rgba(0,0,0,0.4);">
        <h2 style="font-size: 2.2rem; font-weight: 800; margin-bottom: 10px; text-align: center; color: white;">Welcome Back</h2>
        <p style="text-align: center; color: var(--text-muted); margin-bottom: 40px;">Log in to your account to continue trading</p>

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

        <form method="POST">
            <div style="margin-bottom: 25px;">
                <label style="display: block; margin-bottom: 10px; color: var(--text-muted); font-size: 14px;">Account</label>
                <div style="position: relative;">
                    <i class="fas fa-user" style="position: absolute; left: 15px; top: 15px; color: #555;"></i>
                    <input type="text" name="username" required placeholder="Enter your email or phone" style="width: 100%; padding: 15px 15px 15px 45px; background: #161a1e; border: 1px solid var(--border-color); color: white; border-radius: 12px; font-size: 1rem; outline: none; box-sizing: border-box;">
                </div>
            </div>
            <div style="margin-bottom: 30px;">
                <label style="display: block; margin-bottom: 10px; color: var(--text-muted); font-size: 14px;">Password</label>
                <div style="position: relative;">
                    <i class="fas fa-lock" style="position: absolute; left: 15px; top: 15px; color: #555;"></i>
                    <input type="password" name="password" required placeholder="Enter your password" style="width: 100%; padding: 15px 15px 15px 45px; background: #161a1e; border: 1px solid var(--border-color); color: white; border-radius: 12px; font-size: 1rem; outline: none; box-sizing: border-box;">
                </div>
            </div>
            <div style="display: flex; justify-content: flex-end; margin-bottom: 30px;">
                <a href="#" style="color: var(--primary-color); text-decoration: none; font-size: 0.85rem;">Forgot Password?</a>
            </div>
            <button type="submit" class="btn-primary" style="width: 100%; padding: 18px; font-weight: 800; font-size: 1.1rem; border-radius: 16px; box-shadow: 0 10px 20px rgba(0,82,255,0.2);"><?php echo __('nav_login'); ?></button>
        </form>
        <div style="text-align: center; margin-top: 30px; border-top: 1px solid var(--border-color); padding-top: 30px;">
            <span style="color: var(--text-muted);">Don't have an account?</span> <a href="register.php" style="color: var(--primary-color); text-decoration: none; font-weight: bold;"><?php echo __('nav_register'); ?></a>
        </div>
    </div>
</main>

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