<?php
class Guest {

    private $conn;
    private $table_name = "customer";

    public $id;
    public $guest_name; 
    public $contact_number; 
    public $email; 
    public $country; 
    public $photo; 
    public $ppnrc; 
    public $address; 
    public $inactive; 
    public $entry_by; 
    public $update_by; 
    
    

    public function __construct($db) {
        $this->conn = $db;
    }

    public function create() {
        $query = "INSERT INTO " . $this->table_name . " 
                  SET customer_name = :guest_name, contact_number = :contact_number, 
                      document_no = :ppnrc, email = :email, photo=:photo, 
                      country = :country, address = :address,
                      entry_by = :entry_by";

        $stmt = $this->conn->prepare($query);

        $stmt->bindParam(":guest_name", $this->guest_name);
        $stmt->bindParam(":contact_number", $this->contact_number); 
        $stmt->bindParam(":ppnrc", $this->ppnrc);
        $stmt->bindParam(":email", $this->email);
        $stmt->bindParam(":country", $this->country);
        $stmt->bindParam(":photo", $this->photo);
        $stmt->bindParam(":address", $this->address);
        $stmt->bindParam(":entry_by", $this->entry_by);

        if ($stmt->execute()) {
            $this->id = $this->conn->lastInsertId();
            return true;
        }
        return false;
    }

    function update() {
        $query = "UPDATE " . $this->table_name . " 
                  SET customer_name = :guest_name, contact_number = :contact_number, 
                      document_no = :ppnrc, email = :email, address = :address,
                      country = :country, inactive = :inactive, update_by=:update_by  
                  WHERE id=:id";
        $stmt = $this->conn->prepare($query);

        $stmt->bindParam(":id", $this->id);
        $stmt->bindParam(":guest_name", $this->guest_name); 
        $stmt->bindParam(":contact_number", $this->contact_number); 
        $stmt->bindParam(":ppnrc", $this->ppnrc);
        $stmt->bindParam(":email", $this->email);
        $stmt->bindParam(":address", $this->address);
        $stmt->bindParam(":country", $this->country);
        $stmt->bindParam(":inactive", $this->inactive);
        $stmt->bindParam(":update_by", $this->update_by);

        if ($stmt->execute()) {
            return true;
        }
        return false;
    }

    function delete() {
        $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
        $stmt = $this->conn->prepare($query);
        $this->id = htmlspecialchars(strip_tags($this->id));
        $stmt->bindParam(1, $this->id);
        if ($stmt->execute()) {
            return true;
        }
        return false;
    }

    function getOneRow() {
        $query = "SELECT id,
                    customer_name AS guest_name,
                    contact_number,
                    document_no AS ppnrc,
                    email,
                    country,
                    photo,
                    address,
                    inactive
                  FROM " . $this->table_name . "  WHERE id = ?";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $this->id);
        $stmt->execute();
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        if ($row) {
            $this->id = $row['id'];
            $this->guest_name = $row['guest_name'];
            $this->contact_number = $row['contact_number'];
            $this->ppnrc = $row['ppnrc'];
            $this->email = $row['email'];
            $this->country = $row['country'];
            $this->photo = $row['photo'];
            $this->address = $row['address'];
            $this->inactive = $row['inactive'];
        }
    }

    function getAllRows() {
        $query = "SELECT id,
                    customer_name AS guest_name,
                    contact_number,
                    document_no AS ppnrc,
                    email,
                    country,
                    photo,
                    address,
                    inactive
                  FROM " . $this->table_name . " ORDER BY id desc";
        $stmt = $this->conn->prepare($query);
        $stmt->execute();
        return $stmt;
    }

    // public function getRoomByType($roomTypeId) {
    //     $query = "SELECT guest_name, email, commission
    //               FROM " . $this->table_name . " 
    //               LEFT JOIN contact_number ON room.contact_number_id = contact_number.id 
    //               WHERE room.contact_number_id = :contact_number_id 
    //               ORDER BY guest_name";
    
    //     $stmt = $this->conn->prepare($query);
    //     $stmt->bindParam(':contact_number_id', $roomTypeId);
    //     $stmt->execute();
    //     return $stmt;
    // }

    function exist() {
        $query = "SELECT * FROM " . $this->table_name . " WHERE customer_name = ? and document_no = ? LIMIT 0,1";
        $stmt = $this->conn->prepare($query);
        $this->guest_name = htmlspecialchars(strip_tags($this->guest_name));
        $this->ppnrc = htmlspecialchars(strip_tags($this->ppnrc));

        $stmt->bindParam(1, $this->guest_name);
        $stmt->bindParam(2, $this->ppnrc);
        $stmt->execute();
        if ($stmt->rowCount() > 0) {
            return true;
        }
        return false;
    }
}
?>
