<?php

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

use Helpers\Outil\Sql;
use Helpers\Parametres\Constante;
use Modules\Additional\Specifique\Client71\Modules\Administration\Repository\ChantierRepository;

class SpecifiqueSI
{
    /**
     * @param \PDO $pdo
     * @param array<mixed> $array
     * @return string
     */
    public static function clauseSI(\PDO $pdo, $array) : string
    {
        
        $isSISecto  = Sql::paravrac($pdo, 'PARAMSTE', 'SISECTOR') == 'O' ? true : false;

        $req =  " and (
                    SECTIMPU.CODE_DIR = '{$array['CenterEmetteur']}' 
                    or (
                        SECTIMPU.CODE_SI in (
                                SELECT 
                                PARAMDICO.LIBELLE 
                                from PARAMDICO with (nolock) 
                                WHERE  PARAMDICO.TYPEDICO = 'LEONG_DIRSI'
                                AND    PARAMDICO.ID1= '" . ($isSISecto ? ($array['codeSte']) : '') . "' 
                                AND PARAMDICO.ID2= '{$array['CenterEmetteur']}' 
                        )
                    )
                )
        ";

        return $req;
    }

    /**
     * @param \PDO $pdo
     * @param array<mixed> $array
     * @return array<mixed>
     */
    public static function multiSelectSI($pdo, $array)
    {
        if (!$array['codeSte'] || !$array['direction']) {
            return [];
        }

        if ($array['filter']) {
            $array['filter_LIBELLE'.Constante::spliter.'CODE_SI'] = $array['filter'];
        }

        return (new ChantierRepository($pdo))->getSIByDir($array, $array['codeSte'], $array['direction']);
    }
}
