<?php
class ResCustomers
{
    public $name;
    public $email;
    public $password;
    public $phone;
    public $address;

    public function create()
    {
        global $db;

        // Hash the password
        $hashedPassword = password_hash($this->password, PASSWORD_DEFAULT);

        // SQL query
        $sql = "INSERT INTO res_customers (name, email, password, phone,  address) 
                VALUES ('$this->name', '$this->email', '$hashedPassword', '$this->phone', '$this->address')";

        $result = $db->query($sql);

        if ($result) {
            return "Registration successful!";
        } else {
            return "Error: " . $db->error;
        }
    }

    // Optional: login method
    public function login($email, $password)
    {
        global $db;

        $sql = "SELECT * FROM res_customers WHERE email='$email' LIMIT 1";
        $result = $db->query($sql);

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

            if (password_verify($password, $user['password'])) {
                // Successful login
                $_SESSION['user'] = $user['name'];
                return true;
            } else {
                return false; // Wrong password
            }
        } else {
            return false; // User not found
        }
    }

public function getByEmail($email) {
    global $db;
    $sql = "SELECT * FROM res_customers WHERE email='$email' LIMIT 1";
    $result = $db->query($sql);
    return $result ? $result->fetch_assoc() : false;
}



}
?>
