<?php
namespace Modules\API\APIInterne\OxaMobility\Controller;

use Entity\Model\Articles;
use Entity\Model\OxaUserPanier;
use Entity\Model\SousFamille;
use Modules\API\APIInterne\OxaMobility\Repository\CdeComptoirRepo;
use Modules\GlobalApplication\Outil\InitAxesImpCdeComptoirMnt;
use Skeleton\Controller\Controller;
use Helpers\Outil\Sql;
use Helpers\Trad\Trad;
use Modules\API\APIInterne\OxaMobility\Outil\OutilExtension;
use TwigExtension\ConvertLibelleExtension;

// phpcs:ignore
class PAGE_CdeComptoirController extends Controller
{
    public function init() : void
    {
        /** A deplacer dans absAPIcontroller plus tard car pas top */
        $trad = $this->getTranslationService();
        if ($trad instanceof Trad) {
            $trad->setLang($_SESSION['Session_Langue']);
        }
    }


    public function INITAction($body = [], $jwtPayload = [], $json = true)
    {
        $contents = (string)file_get_contents(session_save_path() . '\\sess_' . $jwtPayload['sessionId']);
        @session_name('__OxaProc-PHPSESSID');
        @session_start();
        session_decode($contents);
        
        $trad = $this->getTranslationService();
        
        $return['Trads']['MLS_TitreCdeComptoir'] = $trad->t('Choix de la commande');
        $return['Trads']['MLS_ParFournisseur'  ] = $trad->t('Par articles');
        $return['Trads']['MLS_ParMontant'      ] = $trad->t('Par montant');
        $return['Trads']['MLS_ParCodebarre'    ] = $trad->t('Par code barre');
        $return['Trads']['MLS_Fournisseur'     ] = $trad->t('Fournisseur');
        $return['Trads']['MLS_MontantTotal'    ] = $trad->t('Montant total');
        $return['Trads']['MLS_PieceJointe'     ] = $trad->t('Pièce(s) jointe(s)');
        $return['Trads']['MLS_Btn_Valider'     ] = $trad->t('Valider la commande');
        $return['Trads']['MLS_Fourn_Oblig'     ] = $trad->t('Le fournisseur est obligatoire');
        $return['Trads']['MLS_Montant_Oblig'   ] = $trad->t('Le montant est obligatoire');
        $return['Trads']['MLS_Btn_Commander'   ] = $trad->t('Commander');
        $return['Trads']['MLS_Txt_SI'          ] = $trad->t('Section d\'imputation');
        $return['Trads']['MLS_Txt_CodeGestion' ] = $trad->t('Code gestion');
        $return['Trads']['MLS_Txt_CDC'         ] = $trad->t('Code complémentaire');
        $return['Trads']['MLS_Txt_SCA'         ] = $trad->t('Sous-compte d\'analyse');

        $entete = [
            'SECTIMP'   => $_SESSION["Session_SectionImputation"],
            'CODECOMP'  => $_SESSION["Session_CDC"],
            'SSCPTEANA' => $_SESSION["Session_SCA"],
            'CODE_STE'  => $_SESSION["Session_SocUsr"]
        ];
    
        $ligne = (new CdeComptoirRepo($this->pdo))
            ->getCommandeLigne();
        
        $imptation =
            (new InitAxesImpCdeComptoirMnt($this->pdo, $entete))
                ->getInitImputation($ligne);

        $convertLibelleExtension = new ConvertLibelleExtension($this->pdo);
        $return['Article'] = [
            'SI' => $imptation['SI'],
            'LibSI' => \htmlspecialchars($convertLibelleExtension->convertLibelleGlobal('Sectimpu', '', $imptation['SI'], false)),
            'CDC' => $imptation['CDC'],
            'LibCDC' => \htmlspecialchars($convertLibelleExtension->convertLibelleGlobal('codeComp', '', $imptation['CDC'], false)),
            'SCA' => $imptation['SCA'],
            'LibSCA' => \htmlspecialchars($convertLibelleExtension->convertLibelleGlobal('Sscptana', '', $imptation['SCA'], false)),
        ];

        return OutilExtension::checkExtension($body, $return, $json);
    }
}
