<?php
// Checks if the form has been submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $email = isset($_POST['email']) ? trim($_POST['email']) : '';
    $password = isset($_POST['password']) ? trim($_POST['password']) : '';
    $confirmPassword = isset($_POST['confirm_password']) ? trim($_POST['confirm_password']) : '';

    // Basic validation
    if (empty($email) || empty($password)) {
        // Redirect back to signup.php with an error message
        header("Location: signup.php?error=emptyfields");
        exit();
    } elseif ($password !== $confirmPassword) {
        // Redirects back to signup.php with an error message
        header("Location: signup.php?error=passwordcheck");
        exit();
    } else {

        // Redirects to thankyousignup.php upon successful registration
        header("Location: thankyousignup.php");
        exit();
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Signup</title>
    <link rel="stylesheet" href="main.css"> 
</head>
<body>
    <div class="form-container">
        <?php
        // Checks for error messages in the URL and display them
        if (isset($_GET['error'])) {
            if ($_GET['error'] == 'emptyfields') {
                echo '<p class="error">Please fill in all required fields.</p>';
            } elseif ($_GET['error'] == 'passwordcheck') {
                echo '<p class="error">Passwords do not match.</p>';
            }
        }
        ?>
        <form action="signup.php" method="post">
            <h2>Signup</h2>
            <div class="form-group">
                <label for="email">Email:</label>
                <input type="email" id="email" name="email" required>
            </div>
            <div class="form-group">
                <label for="password">Password:</label>
                <input type="password" id="password" name="password" required>
            </div>
            <div class="form-group">
                <label for="confirm_password">Confirm Password:</label>
                <input type="password" id="confirm_password" name="confirm_password" required>
            </div>
            <div class="form-group">
                <input type="submit" value="Signup">
            </div>
        </form>
    </div>
</body>
</html>
