<?php

class Rolle {

    private $Id_rolle;
    private $libelle;


    public function __construct($Id_rolle, $libelle) {
        $this->Id_rolle = $Id_rolle;
        $this->libelle = $libelle;
    }

    public function getId_rolle() {
        return $this->Id_rolle;
    }

    public function setId_rolle($Id_rolle) {
        $this->Id_rolle = $Id_rolle;
    }

    public function getLibelle() {
        return $this->libelle;
    }

    public function setLibelle($libelle) {
        $this->libelle = $libelle;
    }

    public static function getRolle($id_rolle) {
        $db = Bdd::getInstance();
        $pdo = $db->getConnection();   
        try {
            $stmt = $pdo->prepare("SELECT * FROM rolle WHERE Id_rolle = :id_rolle");
            $stmt->bindParam(':id_rolle', $id_rolle);
            $stmt->execute();

            if ($stmt->rowCount() == 1) {
                $row = $stmt->fetch(PDO::FETCH_ASSOC);
                $r = new Rolle( $row['Id_rolle'],$row['libelle']);

                return $r;
            } else {
                return null; // Rôle non trouvé
            }
        } catch (PDOException $e) {
            echo "Erreur: " . $e->getMessage();
        }
    }
        public static function getRolles() {
        $db = Bdd::getInstance();
        $pdo = $db->getConnection();   
        try {
            $stmt = $pdo->query("SELECT * FROM rolle ");
            $rolles = array();
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                $r = new Rolle( $row['Id_rolle'],$row['libelle']);
                $rolles[] = $r;
            }
            return $rolles;
         } catch (PDOException $e) {
            echo "Erreur: " . $e->getMessage();
        }
    }
    public static function getRollebyname($name) {
        $db = Bdd::getInstance();
        $pdo = $db->getConnection();   
        try {
            $stmt = $pdo->prepare("SELECT * FROM rolle WHERE libelle = :libelle");
            $stmt->bindParam(':libelle', $name);
            $stmt->execute();

            if ($stmt->rowCount() == 1) {
                $row = $stmt->fetch(PDO::FETCH_ASSOC);
                $r = new Rolle( $row['id_rolle'],$row['libelle']);

                return $r;
            } else {
                return null; // Rôle non trouvé
            }
        } catch (PDOException $e) {
            echo "Erreur: " . $e->getMessage();
        }
    }

    public function __toString()
    {
        return $this->getLibelle();
    }
}
