<?php

namespace Modules\Additional\Specifique\Client59\Modules\Liste\Service;

use Helpers\Outil\Sql;
use Modules\Ajax\Repository\UO;

class SpecifiqueSI
{
    /**
     * @param \PDO $pdo
     * @param array<mixed> $param
     * @return string
     */
    public static function clauseSI(\PDO $pdo, $param) : string
    {

        $filtreUO = $param['filtreUO']??'';
        $codeSte = $param['codeSte']??'';

        if (!$filtreUO) {
            $filtreUO = $_SESSION['Session_UO'];
        }
        
        $num_cs_uo_direction = sql::dilispec($pdo, 'PARAMERI', 'NCS_DIRECTIO_UO_FILTRE');
        $num_cs_uo_direction_fille = sql::dilispec($pdo, 'PARAMERI', 'NCS_DIRECTIO_INCLURE_UO_FILLES');
        $isSISecto  = Sql::paravrac($pdo, 'PARAMSTE', 'SISECTOR') == 'O' ? true : false;
        if ($isSISecto) {
            $prefix_code_dir = str_pad($codeSte, 7, ' ', STR_PAD_RIGHT);
        } else {
            $prefix_code_dir = '       ';
        }
        
        $uo = new UO($pdo);

        $liste_uo = [];
        $isUOSecto  = Sql::paravrac($pdo, 'PARAMSTE', 'UOSECTO') == 'O' ? true : false;
        $uo->getListeParentUOByUO($isUOSecto, $filtreUO, $liste_uo, $codeSte);
        $listeFille = [];
        $uo->trouveFilleUO($isUOSecto, $codeSte, $filtreUO, $listeFille);

        if ($liste_uo) {
            $liste_uo = "'".implode("','", $liste_uo)."'";
        } else {
            $liste_uo = "''";
        }

        if ($listeFille) {
            $listeFille = "'".implode("','", $listeFille)."'";
        }

        $req = "
            ( 
                exists (
                    SELECT 
                        null 
                    from CS_DON with(nolock) 
                    where CS_DON.COD_TAB='DIRECTIO' and COD_ENR= '$prefix_code_dir'+SECTIMPU.CODE_DIR 
                    and CS_DON.NUM_FIX = {$num_cs_uo_direction} and CS_DON.VALEUR IN ({$liste_uo})
                )
                and exists (
                    SELECT 
                        null 
                    from CS_DON with(nolock) 
                    where CS_DON.COD_TAB='DIRECTIO' and COD_ENR= '$prefix_code_dir'+SECTIMPU.CODE_DIR 
                    and CS_DON.NUM_FIX = {$num_cs_uo_direction_fille} and CS_DON.VALEUR = 'O'
                ) 
            ) 

            or exists (
                SELECT 
                    null 
                from CS_DON with(nolock) 
                where CS_DON.COD_TAB='DIRECTIO' and COD_ENR= '$prefix_code_dir'+SECTIMPU.CODE_DIR 
                and CS_DON.NUM_FIX = {$num_cs_uo_direction} and (CS_DON.VALEUR = '" . $filtreUO . "')
            )
        
        ";

        if ($listeFille) {
            $req .= "
                or exists (
                    SELECT 
                        * 
                    from CS_DON with(nolock) 
                    where CS_DON.COD_TAB='DIRECTIO' and COD_ENR= '$prefix_code_dir'+SECTIMPU.CODE_DIR 
                    and CS_DON.NUM_FIX = {$num_cs_uo_direction} and CS_DON.VALEUR IN ({$listeFille})
                ) 
            ";
        }

        return " AND ( $req ) ";
    }
}
