<?php
class CheckInGuest {

    private $conn;
    private $table_name = "rental_customer";

    public $id;
    public $checkin_id; 
    public $guest_id; 

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

    public function create() {
        $query = "INSERT INTO " . $this->table_name . " 
                  SET pickup_id = :checkin_id, customer_id = :guest_id";

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

        $stmt->bindParam(":checkin_id", $this->checkin_id);
        $stmt->bindParam(":guest_id", $this->guest_id); 

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

    function update() {
        $query = "UPDATE " . $this->table_name . " 
                  SET guest_name = :guest_name, contact_number = :contact_number, 
                      commission = :commission, phone = :phone,
                      email = :email, address = :address,
                      country = :country, remark = :remark, 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(":commission", $this->commission);
        $stmt->bindParam(":phone", $this->phone);
        $stmt->bindParam(":email", $this->email);
        $stmt->bindParam(":address", $this->address);
        $stmt->bindParam(":country", $this->country);
        $stmt->bindParam(":remark", $this->remark);
        $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 * 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->commission = $row['commission'];
            $this->phone = $row['phone'];
            $this->email = $row['email'];
            $this->address = $row['address'];
            $this->country = $row['country'];
            $this->remark = $row['remark'];
            $this->inactive = $row['inactive'];
        }
    }

    function getAllRows() {
        $query = "SELECT * 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 guest_name = ? and ppnrc = ? 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;
    }

    function getCheckInDetail(){
        $query = "SELECT customer.*
        FROM rental_customer
        LEFT JOIN customer ON rental_customer.customer_id=customer.id
        WHERE pickup_id=:checkin_id
        ORDER BY rental_customer.id";

        $stmt = $this->conn->prepare( $query );
        $stmt->bindParam(":checkin_id", $this->checkin_id);
        $stmt->execute();
        return $stmt;
    }

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