<?php
require_once __DIR__ . '/auth.php';
$message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $firstName = trim($_POST['first_name'] ?? '');
    $lastName = trim($_POST['last_name'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $password = $_POST['password'] ?? '';
    $passwordConfirm = $_POST['password_confirm'] ?? '';

    if ($password !== $passwordConfirm) {
        $message = 'Passwords do not match. Please try again.';
    } elseif (strlen($password) < 6) {
        $message = 'Password must be at least 6 characters.';
    } else {
        $result = registerUser($firstName, $lastName, $email, $password);
        if ($result === true) {
            header('Location: login.php?registered=1');
            exit;
        }
        $message = $result;
    }
}
?>
<!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">Register</li>
        </ol>
    </div>
</div>

<section class="padding-tb-80px background-white">
    <div class="container">
        <div class="auth-card">
            <h2>Register as a Travelsspeed subagent</h2>
            <?php if ($message): ?>
                <div class="alert alert-danger"><?= htmlentities($message) ?></div>
            <?php endif; ?>
            <p class="auth-note">Create your account to start earning with our affiliate program.</p>
            <form action="Register.php" method="post">
                <div class="form-row">
                    <div class="form-group col-md-6">
                        <label>First name</label>
                        <input type="text" name="first_name" class="form-control" placeholder="First name" required>
                    </div>
                    <div class="form-group col-md-6">
                        <label>Last name</label>
                        <input type="text" name="last_name" class="form-control" placeholder="Last name" required>
                    </div>
                </div>
                <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-row">
                    <div class="form-group col-md-6">
                        <label>Password</label>
                        <input type="password" name="password" class="form-control" placeholder="Enter password" required>
                    </div>
                    <div class="form-group col-md-6">
                        <label>Confirm password</label>
                        <input type="password" name="password_confirm" class="form-control" placeholder="Confirm password" required>
                    </div>
                </div>
                <button type="submit" class="btn btn-primary">Create account</button>
            </form>
            <p class="auth-note mt-3">Already have an account? <a href="login.php">Login here</a></p>
        </div>
    </div>
</section>

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