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

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';

    if (empty($username) || empty($password)) {
        $error = mt('Please fill in all fields.');
    } else {
        try {
            $stmt = db()->prepare("SELECT * FROM users WHERE username = ?");
            $stmt->execute([$username]);
            $user = $stmt->fetch();
            
            if ($user && password_verify($password, $user['password_hash'])) {
                $_SESSION['user_id'] = $user['id'];
                header('Location: index.php');
                exit;
            } else {
                $error = mt('Invalid username or password.');
            }
        } catch (Exception $e) {
            $error = mt('Login failed.');
        }
    }
}
?>

<div class="container my-5 py-5">
    <div class="row justify-content-center">
        <div class="col-md-5">
            <div class="card bg-dark border-secondary p-4 shadow-lg" style="border-radius: 20px;">
                <h2 class="text-center mb-4 fw-bold text-white"><?php echo mt('Login to BITCrypto'); ?></h2>
                <?php if ($error): ?>
                    <div class="alert alert-danger border-0 small"><?php echo $error; ?></div>
                <?php endif; ?>
                <form method="POST">
                    <div class="mb-3">
                        <label class="form-label text-muted small"><?php echo mt('Username'); ?></label>
                        <input type="text" name="username" class="form-control bg-dark text-white border-secondary py-2" required placeholder="<?php echo mt('Enter username'); ?>">
                    </div>
                    <div class="mb-3">
                        <label class="form-label text-muted small"><?php echo mt('Password'); ?></label>
                        <input type="password" name="password" class="form-control bg-dark text-white border-secondary py-2" required placeholder="<?php echo mt('Enter password'); ?>">
                    </div>
                    <div class="mb-4 d-flex justify-content-between align-items-center">
                        <div class="form-check">
                            <input type="checkbox" class="form-check-input" id="remember">
                            <label class="form-check-label small text-muted" for="remember"><?php echo mt('Remember me'); ?></label>
                        </div>
                        <a href="#" class="small text-primary text-decoration-none fw-bold"><?php echo mt('Forgot password?'); ?></a>
                    </div>
                    <button type="submit" class="btn btn-primary w-100 py-3 fw-bold mb-3" style="background-color: var(--okx-blue); border: none; border-radius: 12px;"><?php echo mt('Login'); ?></button>
                    <div class="text-center">
                        <span class="text-muted small"><?php echo mt("Don't have an account?"); ?></span> 
                        <a href="register.php" class="text-primary text-decoration-none fw-bold small ms-1"><?php echo mt('Register'); ?></a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<style>
    .form-control:focus {
        background-color: #2b2f36;
        border-color: var(--okx-blue);
        box-shadow: none;
        color: #fff;
    }
</style>

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