<?php
class RoomType {
    // database connection and table name
    private $conn;
    private $table_name = "vehicle_category";

    // object properties
    public $id;
    public $room_type;
    public $description;
    public $room_rate;
    public $max_occupancy;
    public $amenities;
    public $extra_bed_rate;
	public $entry_by;
	public $update_by;

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

    // Create a new room type
    function create() {
        $query = "INSERT INTO " . $this->table_name . " 
                  SET vehicle_category=:room_type, description=:description, 
                      rental_rate=:room_rate, seats=:max_occupancy, 
                      features=:amenities, extra_driver_rate=:extra_bed_rate, entry_by=:entry_by ";
        $stmt = $this->conn->prepare($query);

        // Bind values
        $stmt->bindParam(":room_type", $this->room_type);
        $stmt->bindParam(":description", $this->description);
        $stmt->bindParam(":room_rate", $this->room_rate);
        $stmt->bindParam(":max_occupancy", $this->max_occupancy);
        $stmt->bindParam(":amenities", $this->amenities);
        $stmt->bindParam(":extra_bed_rate", $this->extra_bed_rate);
		$stmt->bindParam(":entry_by", $this->entry_by);

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

    // Update an existing room type
    function update() {
        $query = "UPDATE " . $this->table_name . " 
                  SET vehicle_category=:room_type, description=:description, 
                      rental_rate=:room_rate, seats=:max_occupancy, 
                      features=:amenities, extra_driver_rate=:extra_bed_rate, update_by=:update_by  
                  WHERE id=:id";
        $stmt = $this->conn->prepare($query);

        // Bind values
        $stmt->bindParam(":id", $this->id);
        $stmt->bindParam(":room_type", $this->room_type);
        $stmt->bindParam(":description", $this->description);
        $stmt->bindParam(":room_rate", $this->room_rate);
        $stmt->bindParam(":max_occupancy", $this->max_occupancy);
        $stmt->bindParam(":amenities", $this->amenities);
        $stmt->bindParam(":extra_bed_rate", $this->extra_bed_rate);
		$stmt->bindParam(":update_by", $this->update_by);

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

    // Delete a room type
    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;
    }

    // Get a single room type by ID
    function getOneRow() {
        $query = "SELECT id,
                    vehicle_category AS room_type,
                    description,
                    rental_rate AS room_rate,
                    seats AS max_occupancy,
                    features AS amenities,
                    extra_driver_rate AS extra_bed_rate
                  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->room_type = $row['room_type'];
            $this->description = $row['description'];
            $this->room_rate = $row['room_rate'];
            $this->max_occupancy = $row['max_occupancy'];
            $this->amenities = $row['amenities'];
            $this->extra_bed_rate = $row['extra_bed_rate'];
        }
    }

    // Get all room types
    function getAllRows() {
        $query = "SELECT id,
                    vehicle_category AS room_type,
                    description,
                    rental_rate AS room_rate,
                    seats AS max_occupancy,
                    features AS amenities,
                    extra_driver_rate AS extra_bed_rate
                  FROM " . $this->table_name . " ORDER BY id";
        $stmt = $this->conn->prepare($query);
        $stmt->execute();
        return $stmt;
    }

    // Check if a room type exists by name
    function exist() {
        $query = "SELECT * FROM " . $this->table_name . " WHERE vehicle_category = ? LIMIT 0,1";
        $stmt = $this->conn->prepare($query);
        $this->room_type = htmlspecialchars(strip_tags($this->room_type));

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