<?php
namespace classs;
use PDO;
use PDOException;
class ManagerAgent
{
    private $conn;

    public function __construct($conn)
    {
        $this->conn = $conn;
    }
    public function getAll()
    {
        $sql = "SELECT * FROM agents";
        $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 insertAgentData($agent_name, $phone, $email, $cost_centers_id, $id_level_acc) {
    
        try {
            // اتصل بالدالة لإدخال البيانات في جدول agents
            $stmt = $this->conn->prepare("INSERT INTO agents (id_level_acc, agent_name, phone_number, email, id_cost_center, user_id_insert) VALUES (:id_level_acc, :agent_name, :phone, :email, :cost_centers_id, :user_id_insert)");
    
            // قم بربط القيم باستخدام bindParam
            $stmt->bindParam(':id_level_acc', $id_level_acc);
            $stmt->bindParam(':agent_name', $agent_name);
            $stmt->bindParam(':phone', $phone);
            $stmt->bindParam(':email', $email);
            $stmt->bindParam(':cost_centers_id', $cost_centers_id);
            $stmt->bindParam(':user_id_insert', $_SESSION['admin']);
    
            // قم بتنفيذ الاستعلام
            // $stmt->execute();
            $restmt = $stmt->execute();
            $get_id = $this->conn->lastInsertId();
            logToHistory('insert', 'agents', $get_id, '', '', 'ID');
            return $restmt;
    
            // echo "تم إضافة الوكيل بنجاح!";
        } catch (PDOException $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();
    }

}

?>