<?php
session_start();
include 'connection.php';

if (isset($_POST['registering_resident'])) {
    $firstName = strtoupper(trim($_POST['firstName']));
    $lastName = strtoupper(trim($_POST['lastName']));
    $registerEmail = strtolower(trim($_POST['registerEmail'])); // emails are better lowercase
    $registerPassword = $_POST['registerPassword'];
    $confirmPassword = $_POST['confirmPassword'];
    $address = strtoupper(trim($_POST['address']));

    if (empty($firstName) || empty($lastName) || empty($registerEmail) || empty($registerPassword) || empty($confirmPassword) || empty($address)) {
        echo "Please fill all  empty feilds!";
        exit;
        // code...
    }
    // ✅ 1. Check if passwords match
    if ($registerPassword !== $confirmPassword) {
        echo "Passwords do not match!";
        exit;
    }

    // ✅ 2. Check if email already exists
    $check = $conn->query("SELECT * FROM tbl_resident WHERE email = '$registerEmail'");
    if ($check->num_rows > 0) {
        echo "Email is already registered!";
        exit;
    }


    // ✅ 4. Insert into database
    $query = "INSERT INTO tbl_resident (first_name, last_name, email, address, password) 
              VALUES ('$firstName', '$lastName', '$registerEmail', '$address', ' $confirmPassword')";

    if ($conn->query($query)) {
        echo "Registration successful!";
    } else {
        echo "Registration failed: " . $conn->error;
    }
}
?>
