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

use GuzzleHttp\Psr7\Response;
use Modules\API\APIInterne\OxaMobility\Repository\CdeComptoirRepo;
use Modules\GlobalApplication\Outil\InitAxesImpCdeComptoirMnt;
use TwigExtension\ConvertLibelleExtension;

// phpcs:ignore
class PAGE_CdeComptoirController extends AbsApiController
{
    public function INITAction() :Response
    {
        $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 $this->createApiResponse($return);
    }
}
