<?php

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

use Entity\Model\OxaUserPanier;
use Entity\Model\OxaUserPanierLigne;
use Entity\Model\Sectimpu;
use Helpers\ChampsSupplementaire\ChampSupp;
use Helpers\Outil\Sql;
use Modules\Panier\Repository\PanierEntete;

class SpecifiqueCentreLivraison
{

    /**
     * @param array<mixed> $array
     */
    public static function clauseCentre(\PDO $pdo, $array = []):string
    {
        $sisecto = \Helpers\Outil\Roles::droit($pdo, 'SISECTOR', 'PARAMSTE');

        if ($array['id_lig']??'') {
            $panierLigne = (new OxaUserPanierLigne($pdo))->find([
                'id_user' => $_SESSION['id_user'],
                'id_panier' => $_SESSION['id_panier'],
                'id_lig' => $array['id_lig'],
            ], ['SI_CODE']);
            $si = $panierLigne['SI_CODE'];
        } else {
            $panierEntete = (new OxaUserPanier($pdo))->find([
                'id_user' => $_SESSION['id_user'],
                'id_panier' => $_SESSION['id_panier']
            ], ['SI_CODE']);
            
            $si = $panierEntete['SI_CODE'];
        }

        $infoSI = (new Sectimpu($pdo))->find([
            'CODE_SI' => $si,
            'CODE_STE' => $sisecto? $_SESSION["Session_SocUsr"]:'',
        ], ['CODE_DIR']);

        $listeCentre = self::getValueCSByDirection($pdo, $infoSI['CODE_DIR']??'', $_SESSION["Session_SocUsr"]);

        if ($listeCentre) {
            return " AND CODE_CTR in ('".implode("','", $listeCentre)."') ";
        } else {
            return '';
        }
    }

    /**
     * Summary of initCentreBySi
     * @param \pdo $pdo
     * @param mixed $array
     * @return string
     */
    public static function initCentreBySi(\pdo $pdo, $array = []):string
    {
        $numFixe = Sql::dilispec($pdo, 'PARNCS0177', 'NCS_SECTIMPU_ADRESSE_PRINCIPALE');
        $si = ChampSupp::autoCompletCodeEnrImputations($pdo, 'SECTIMPU', $array['SI']);
        return ChampSupp::getValueCS('SECTIMPU', $si, $numFixe, $pdo);
    }

    /**
     * @return array<mixed>
     */
    public static function getValueCSByDirection(\PDO $pdo, ?string $direction = null, ?string $societe = null)
    {
        $idDIrection = ChampSupp::autoCompletCodeEnrImputations($pdo, 'DIRECTIO', $direction, $societe);

        $numFixe1 = Sql::dilispec($pdo, 'PARNCS0177', 'NCS_DIRECTIO_ADRESSE_PRINCIPALE');
        $numFixe2 = Sql::dilispec($pdo, 'PARNCS0177', 'NCS_DIRECTIO_ADRESSE_2');
        $numFixe3 = Sql::dilispec($pdo, 'PARNCS0177', 'NCS_DIRECTIO_ADRESSE_3');
        $numFixe4 = Sql::dilispec($pdo, 'PARNCS0177', 'NCS_DIRECTIO_ADRESSE_4');
        $numFixe5 = Sql::dilispec($pdo, 'PARNCS0177', 'NCS_DIRECTIO_ADRESSE_5');
        $numFixe6 = Sql::dilispec($pdo, 'PARNCS0177', 'NCS_DIRECTIO_ADRESSE_6');
        $numFixe7 = Sql::dilispec($pdo, 'PARNCS0177', 'NCS_DIRECTIO_ADRESSE_7');
        $numFixe8 = Sql::dilispec($pdo, 'PARNCS0177', 'NCS_DIRECTIO_ADRESSE_8');
        $numFixe9 = Sql::dilispec($pdo, 'PARNCS0177', 'NCS_DIRECTIO_ADRESSE_9');


        $req = "select VALEUR from CS_DON 
                where COD_TAB = 'DIRECTIO' and NUM_FIX in (:numFix1, :numFix2, :numFix3,:numFix4, :numFix5, :numFix6, :numFix7,:numFix8,:numFix9) 
                and COD_ENR = :idDirection";

        $arrayReq = [
            ':idDirection' => $idDIrection,

            ':numFix1' => $numFixe1,
            ':numFix2' => $numFixe2,
            ':numFix3' => $numFixe3,
            ':numFix4' => $numFixe4,
            ':numFix5' => $numFixe5,
            ':numFix6' => $numFixe6,
            ':numFix7' => $numFixe7,
            ':numFix8' => $numFixe8,
            ':numFix9' => $numFixe9,
        ];

        $requete = $pdo->prepare($req);
        $requete->execute($arrayReq);

        $return = $requete->fetchAll();

        $listeCentre = [];
        foreach ($return as $r) {
            $listeCentre[] = $r['VALEUR'];
        }

        return $listeCentre;
    }
}
