<?php
namespace Modules\Additional\Specifique\Client128\Modules\EtatStatistique\Repository;

use Helpers\QueryBuilder\AbsRepository;
use Modules\Contrat\Repository\Uo;
use Helpers\Outil\Sql;

class ReportingUO extends AbsRepository
{

    /**
     * A REVOIR ET A GENERALISER
     */
    public function listUOUserTreeview($returnChoice = 'uo_list')
    {

        $uo                                     = new Uo($this->pdo);

        $UOUtilisateur                          = $this->rqGetInfoUser($_SESSION['Session_Login'])['CODE_UO'];

        $UORespStr = $uo->getUOResponsableStr();
        $uoListReqTmp                           = [];
        if (array_key_exists('AuthAccesToutesUO', $_SESSION) && $_SESSION['AuthAccesToutesUO']) {
            $uoListReqTmp                       = $uo->getAllOfUO('');
        } else {
            $UOUtilisateurTmp[]['CODE_UO']      = $UOUtilisateur;

            if (strpos($UORespStr, ',')) {
                foreach (explode(",", $UORespStr) as $tmpUO) {
                    if ($UOUtilisateur != str_replace("'", '', $tmpUO)) {
                        $UOUtilisateurTmp[]['CODE_UO']      = $tmpUO;
                    }
                }
            } else {
                if ($UOUtilisateur != str_replace("'", '', $UORespStr)) {
                    $UOUtilisateurTmp[]['CODE_UO']      = $UORespStr;
                }
            }

            $ste = $_SESSION['Session_SocUsr'];
            $isSecto = Sql::boolSql(Sql::paravrac($this->pdo, 'PARAMSTE', 'UOSECTO'));
            $tmpUOPourFonction =[];
            foreach ($UOUtilisateurTmp as $arrayTmp) {
                /** @phpstan-ignore-next-line */
                if ($isSecto) {
                    $req =  'SELECT CODE_UO, 
                                LIB_UO,
                                UO_MERE,
                                NIVEAU,
                                CODE_STE  
                            FROM UO with(nolock)
                            WHERE CODE_UO =\''. str_replace("'", "", $arrayTmp['CODE_UO']) .'\' AND CODE_STE = \''.$ste.'\'  order by CODE_UO';
                } else {
                    $req =  'SELECT CODE_UO, 
                                LIB_UO,
                                UO_MERE,
                                NIVEAU,
                                CODE_STE  
                            FROM UO with(nolock)
                            WHERE CODE_UO =\''. str_replace("'", "", $arrayTmp['CODE_UO']) .'\'  order by CODE_UO';
                }
                
                $listeUoDb = $this->fetchAssocAll($req, []);
                
                foreach ($listeUoDb as $listeUoDbTmp) {
                    $tmpUOPourFonction[] = [
                        'CODE_UO' => $listeUoDbTmp['CODE_UO'],
                        'LIB_UO' => $listeUoDbTmp['LIB_UO'],
                        'UO_MERE' => '',
                        'NIVEAU' => $listeUoDbTmp['NIVEAU']

                    ];
                }
            }

            if ($isSecto == 1) {
                $req =  'SELECT CODE_UO, 
                            LIB_UO,
                            UO_MERE,
                            NIVEAU,
                            CODE_STE  
                        FROM UO with(nolock)
                        WHERE RESP_HIER =\''. $_SESSION['Session_Login'] .'\' AND CODE_STE = \''.$ste.'\'  order by CODE_UO';
            } else {
                $req = 'SELECT CODE_UO, 
                            LIB_UO,
                            UO_MERE,
                            NIVEAU,
                            CODE_STE  
                        FROM UO with(nolock)
                        WHERE RESP_HIER =\''. $_SESSION['Session_Login'] .'\'  order by CODE_UO';
            }

            $listeUoDb = $this->fetchAssocAll($req, []);
            
            foreach ($listeUoDb as $listeUoDbTmp) {
                $tmpUOPourFonction[] = [
                    'CODE_UO' => $listeUoDbTmp['CODE_UO'],
                    'LIB_UO' => $listeUoDbTmp['LIB_UO'],
                    'UO_MERE' => '',
                    'NIVEAU' => $listeUoDbTmp['NIVEAU']
                ];
            }
            
            $uo =new \Modules\Contrat\Repository\Uo($this->pdo);
            $uo_list_req                        = $uo->getFilleUo($uoListReqTmp, $tmpUOPourFonction);
        }

        $uo_list_req = $uoListReqTmp;
        

        return $returnChoice == 'uo_list' ? $uo_list_req : $UORespStr;
    }

    public function listUOUser($returnChoice = 'uo_list')
    {
        $uo = new Uo($this->pdo);

        $UOUtilisateur = $this->rqGetInfoUser($_SESSION['Session_Login'])['CODE_UO'] ?? '';

        $UORespStr = $uo->getUOResponsableStr();
        $uoListReqTmp                           = [];
        if (array_key_exists('AuthAccesToutesUO', $_SESSION) && $_SESSION['AuthAccesToutesUO']) {
            $uoListReqTmp                       = $uo->getAllOfUO('');
        } else {
            $UOUtilisateurTmp[]['CODE_UO']      = $UORespStr;
            $UOUtilisateurTmp[]['CODE_UO']      = $UOUtilisateur;
          
            $uo_list_req                        = $uo->getFilleUo($uoListReqTmp, $UOUtilisateurTmp);
        }
        
        $uo_list_req = $uoListReqTmp;
        if (is_array($uo_list_req)) {
            $arrayUOList = array();
            foreach ($uo_list_req as &$values) {
                $CODE_UO                        = $values['CODE_UO'];
                if (! in_array($CODE_UO, $arrayUOList)) {
                    $arrayUOList[]              = $CODE_UO;
                }
            }
        }
        $uo_list                                = $uo->array1DToSqlStr($arrayUOList);

        if (!is_array($UOUtilisateur)) {
            $concat_uo_list                     = $uo_list;
        } else {
            $concat_uo_list                     = $uo_list;
        }
     
        if (!is_array($UORespStr)) {
            $concat_resp_uo_list                = $uo_list;
        } else {
            $concat_resp_uo_list                = $UORespStr . ',' .$uo_list;
        }

        return $returnChoice == 'uo_list' ? $concat_uo_list : $concat_resp_uo_list;
    }

    public function rqGetInfoUser($code)
    {
        $req                = " SELECT HASTUTIL.*, DILISPEC.LIB01DICO as SIGNAT_DA_FAV from HASTUTIL with (nolock)  left outer join DILISPEC with (nolock) on CODEDICO = HASTUTIL.CODE and DILISPEC.TYPEDICO = :TYPE_DICO where HASTUTIL.CODE = :CODE";
         
        $arrayReq           =
        [
            ':CODE'         => $code,
            ':TYPE_DICO'    => 'ETDESDAFAV',
        ];
        $requete            = $this->pdo->prepare($req);
        
        $requete->execute($arrayReq);
        return $requete->fetch(\PDO::FETCH_ASSOC);
    }
}
