<?php
namespace classs;
use PDO;
use Exception;
class ManagerHotels
{
    private $conn;

    public function __construct($conn)
    {
        $this->conn = $conn;
    }
    public function getAll()
    {
        $sql = "SELECT * FROM hotels";
        $stmt = $this->conn->query($sql);
        return ($stmt->rowCount() > 0) ? $stmt->fetchAll(PDO::FETCH_ASSOC) : array();
    }
    
    public function getById($id)
    {
        // $sql = "SELECT * FROM reservations WHERE id = :reservation_id";
        $sql = "SELECT *
                FROM hotels 
                WHERE ID = :id";
        $stmt = $this->conn->prepare($sql);
        $stmt->bindParam(':id', $id);
        $stmt->execute();

        return ($stmt->rowCount() > 0) ? $stmt->fetch(PDO::FETCH_ASSOC) : null;
    }

    public function getCarsForCurrentMonthWithNames()
    {
        $currentMonth = date('Y-m');

        $sql = "SELECT h.*, d.*
                FROM hotels h
                LEFT JOIN drivers d ON d.id_hotels = h.ID";

        $stmt = $this->conn->prepare($sql);
        $stmt->execute();

        return ($stmt->rowCount() > 0) ? $stmt->fetchAll(PDO::FETCH_ASSOC) : array();
    }
    public function insertHotelData($id_cost_center, $name_hot, $phone, $email, $stars, $country, $city, $address, $time_in, $link, $disc, $time_out, $sessionUser, $id_level_acc) {
        try {
            
            // الاستعداد للاستعلام
            $stmt = $this->conn->prepare("INSERT INTO hotels (id_cost_center, hotel_name, contact_phone, contact_email, stars, country, city, location, check_in_time, website_url, description, check_out_time, user_id_insert, id_level_acc) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

            // ربط الباراميترات
            $stmt->bindParam(1, $id_cost_center);
            $stmt->bindParam(2, $name_hot);
            $stmt->bindParam(3, $phone);
            $stmt->bindParam(4, $email);
            $stmt->bindParam(5, $stars);
            $stmt->bindParam(6, $country);
            $stmt->bindParam(7, $city);
            $stmt->bindParam(8, $address);
            $stmt->bindParam(9, $time_in);
            $stmt->bindParam(10, $link);
            $stmt->bindParam(11, $disc);
            $stmt->bindParam(12, $time_out);
            $stmt->bindParam(13, $sessionUser);
            $stmt->bindParam(14, $id_level_acc);

            // إذا كنت تحتاج إلى استرداد معرف السجل الجديد، يمكنك استخدام
            // $lastInsertedId = $con->lastInsertId();

            // return "تمت إضافة البيانات بنجاح!";

            $restmt = $stmt->execute();
            $get_id = $this->conn->lastInsertId();
            logToHistory('insert', 'hotels', $get_id, '', '', 'ID');
            return $restmt;
        } catch (Exception $e) {
            echo "فشل في إدخال البيانات: " . $e->getMessage();
        }
    }

    
    public function updateCar($car_id, $name, $num, $number_passengers)
    {
        // Add your validation and sanitization here before executing the SQL query

        $sql = "UPDATE hotels
                SET vehicle_model = :vehicle_model, vehicle_plate_number = :vehicle_plate_number, seating_capacity = :seating_capacity
                WHERE ID = :car_id";

        $stmt = $this->conn->prepare($sql);
        $stmt->bindParam(':vehicle_model', $name);
        $stmt->bindParam(':vehicle_plate_number', $num);
        $stmt->bindParam(':seating_capacity', $number_passengers);
        $stmt->bindParam(':car_id', $car_id);

        return $stmt->execute();
    }

    public function deleteReservation($reservation_id)
    {
        // Add your validation and sanitization here before executing the SQL query

        $sql = "DELETE FROM hotels WHERE ID = :reservation_id";
        $stmt = $this->conn->prepare($sql);
        $stmt->bindParam(':reservation_id', $reservation_id);

        return $stmt->execute();
    }

}

?>