<?php
require_once __DIR__ . '/auth.php';
$message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = trim($_POST['email'] ?? '');
    $password = $_POST['password'] ?? '';
    $user = authenticateUser($email, $password);
    if ($user) {
        $_SESSION['user'] = $user;
        header('Location: dashboard.php');
        exit;
    }
    $message = 'Invalid email or password. Please try again.';
}
$successMessage = '';
if (isset($_GET['registered'])) {
    $successMessage = 'Registration complete. Please login to continue.';
}
?>
<!DOCTYPE html>
<html lang="en-US">
<?php include "pages/header.php"; ?>

<div id="page-title" class="text-grey background-overlay background-img-dark" style="background-image: url('images/page-title-9.jpg');">
    <div class="text-center">
        <ol class="breadcrumb z-index-2 position-relative no-background padding-30px margin-0px border-top-1 border-grey-4">
            <li><a href="index.php" class="text-white">Home</a></li>
            <li class="active">Login</li>
        </ol>
    </div>
</div>

<section class="padding-tb-80px background-white">
    <div class="container">
        <div class="auth-card">
            <h2>Login to your affiliate account</h2>
            <?php if ($successMessage): ?>
                <div class="alert alert-success"><?= htmlentities($successMessage) ?></div>
            <?php endif; ?>
            <?php if ($message): ?>
                <div class="alert alert-danger"><?= htmlentities($message) ?></div>
            <?php endif; ?>
            <p class="auth-note">Enter your email and password to access the subagent portal.</p>
            <form action="login.php" method="post">
                <div class="form-group">
                    <label>Email address</label>
                    <input type="email" name="email" class="form-control" placeholder="name@example.com" required>
                </div>
                <div class="form-group">
                    <label>Password</label>
                    <input type="password" name="password" class="form-control" placeholder="Enter password" required>
                </div>
                <button type="submit" class="btn btn-primary">Login now</button>
            </form>
            <p class="auth-note mt-3">New to Travelsspeed? <a href="Register.php">Create an affiliate account</a></p>
        </div>
    </div>
</section>

<?php include "pages/footer.php"; ?>
</body>
</html>
