<?PHP 
namespace Classe;

use MysqlDB;
use views\DepartementHtml;
class Departement{
    public $idDep = 1;
    public $nomDep = "Reoleweb - Immobilier";
    public $commemtairedep = "";
    public $typeAffDep = 0;
    public $rapportAicDep = 0;

    private $conn;
    private $nom_departement;
    private $id_departement;
    private $code_departement;
    public function __construct() {
        $args = func_get_args();
        $numArgs = func_num_args();
       
        if($numArgs>0){
            if (method_exists($this, $f = '__construct' . $numArgs)) {
                call_user_func_array([$this, $f], $args);
            }
        }else{
            $this->idDep=$this->id_departement;
            $this->nomDep=$this->nom_departement;
            $this->commemtairedep=$this->commemtairedep;
        }

        
        
    }
    public function __construct1($conn){
        $this->conn = $conn;
        $this->nomDep="";
        $this->idDep=0;
        $this->commemtairedep="";
    }
    public function __construct2($conn, $idDep){
        $this->conn = $conn;
        $this->idDep = $idDep;
        // Department functionality removed - using default values
        // No database operations needed since departments are not used
    }
    
    public function __construct7($nom_departement,$id_departement){        
        $this->idDep=$id_departement;
        $this->nomDep=$nom_departement; 
        $this->nomDep=$nom_departement;        
    }
    
    function getNomDep(): String{
        return $this->nomDep;
    }
    
    function getListeDep($charSep="-/",$ordre ="asc"):String{
        $listeDepartement="";
        $sqlDepartement = "SELECT nom_departement FROM departements Order by nom_departement ".$ordre;
        $this->conn->exec($sqlDepartement);
        $resultDepartement = $this->conn->data;
        foreach($resultDepartement as $departement){
            $listeDepartement.= $departement['nom_departement'].$charSep;
        }
        if($listeDepartement!=""){
            $listeDepartement=substr($listeDepartement,0,strlen($listeDepartement)-strlen($charSep));
        }
        return $listeDepartement;
    }
    
    function getListeDepObj($ordre="asc"){
        $listeDepartement="";
        $sqlDepartement = "SELECT nom_departement FROM departements Order by nom_departement ".$ordre;
        $this->conn->execObject($sqlDepartement,"Classe\Departement");
        $resultDepartement = $this->conn->data;
        return $resultDepartement;
    }

    function getListeDepUser($idUser):string{
        $sqlDep="SELECT nom_departement FROM departements, departement_user 
        WHERE departements.id_departement=departement_user.id_departement and id_user=".$idUser."
        ORDER BY nom_departement ASC";
        $this->conn->exec($sqlDep);
        
        $resultDep=$this->conn->data;        
        $dep = [];

        foreach ($resultDep as $rowDep) {
            $dep[] = $rowDep['nom_departement'];
        }

        if (count($dep) > 1) {
            $lastDep = array_pop($dep); 
            return implode(", ", $dep) . " et " . $lastDep;
        }
        return implode(", ", $dep);
    }
    
    public function afficherListeDep(){
        $conn=$this->conn;
        extract($_REQUEST, EXTR_PREFIX_ALL | EXTR_REFS, 'v');
        $_POST[] = "";
        $_GET[] = "";
        $get[]="";
        $get["page"]="dep";

        $sql = "SELECT * FROM departements;";
        
        $conn->exec($sql);
        $result = $conn->data;
       
        DepartementHtml::AfficherListeDepartement($result);
    }
}
?>
