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



if (isset($_POST['verify_user'])) {
    $email = trim($_POST['loginEmail']);
    $password = $_POST['loginPassword'];

    if (empty($email) || empty($password)) {
        echo "Please fill in both fields.";
        exit;
    }

    // Fetch user by email
    $stmt = $conn->prepare("SELECT * FROM tbl_resident WHERE email = ? AND password = ?");
    $stmt->bind_param("ss", $email, $password);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result && $result->num_rows > 0) {
        $row = $result->fetch_assoc();

            
            $_SESSION['resident_id'] = $row['resident_id'];
            $_SESSION['resident_name'] = $row['first_name'] . " " . $row['last_name'];
           
            // Set user status to '1' (active/logged in)
            $update_stmt = $conn->prepare("UPDATE tbl_resident SET status = '1' WHERE resident_id = ?");
            $update_stmt->bind_param("i", $row['resident_id']);
            $update_stmt->execute();

            echo "success";
       
    } else {
        echo "Invalid email or password";
    }
}
?>
