<?php
namespace Modules\Ajax\Controller;

require_once 'application/helpers/downloadHelper.php';
use Modules\GlobalApplication\Repository\WorkflowSignature;
use Skeleton\Controller\Controller;
use Helpers\Trad\Trad;
use Helpers\Ariane\Ariane;

class ErreurController extends Controller
{
    public function popupErreurValidAction() : string
    {
        $post = $this->request->getParsedBody();
        
        if (is_array($post['Erreur'])) {
            $erreur = '';
            foreach ($post['Erreur'] as $value) {
                $erreur .= '<br />'.$value;
            }
            $post['Erreur'] = $erreur;
        }
        
        return $this->renderer->render('@popup-erreur/ErreurCreation', [
            'ErreurValid' => $post['Erreur'],
        ]);
    }
    
    
    public function popupErreurXmlAction() : string
    {
        $post = $this->request->getParsedBody();
        $erreur = $post['Erreur'];
        $params = $post['params'];
 
        $path = $post['Path'];

        if (is_array($erreur)) {
            $reponse = $erreur;
        } elseif (strpos($erreur, '<') !== false || strpos($erreur, '&lt;') !== false) {
            $reponse = new \SimpleXMLElement($erreur);
        } else {
            $reponse = json_decode($erreur, true);
        }
        
        $droits['ajoutSignataire'] = \Helpers\Outil\Roles::droitsUtilisateur($this->pdo, 'HA_SE_AJSI');
        $droits['suppressionSignataire'] = \Helpers\Outil\Roles::droitsUtilisateur($this->pdo, 'HA_SE_SUSI');
        
        $signataires = new \Modules\Ajax\Repository\Signataires($this->pdo);
        if (($params['type']??'')=='facture') {
            $signataire = $signataires->signatairesBAP($params['numFact']);
            
            foreach ($signataire as $value) {
                $listeSignataires[$value['CODE']]=$value['NOM'];
            }
        }
        if (isset($reponse['ListeErreur']['Erreur']['Num'])) {
            $reponse['ListeErreur']['Erreur'] = [0 => $reponse['ListeErreur']['Erreur']];
        }

        return $this->renderer->render('@popup-erreur/ErreurXml', [
            'ErreurValid' => $reponse,
            'pathValidation' => $path,
            'params' => $params,
            'type' => $params['type']??'',
            'pathArret' => $post['pathArret'],
            'listeTypeRole' => ['N' => 'Signataire', 'O' => 'Informé'],
            'droits' => $droits,
            'listeSignataires' => $listeSignataires ?? '',
        ]);
    }
    
    public function ajaxAjoutSignataireAction() : string
    {
        $post = $this->request->getParsedBody();

        $trad = $this->getTranslationService();

        $signataire = null;
        if (isset($post['choixType']) && $post['choixType'] == 'S') {
            $signataires = new \Entity\Model\Hastutil($this->pdo);
            $signataire = $signataires->find($post['signataire']);
        }
        if (isset($post['choixType']) && $post['choixType'] == 'GS') {
            $signataires = new \Modules\Ajax\Repository\Signataires($this->pdo);
            $signataire = $signataires->signataireREFGroupeById($post['signataire']);
        }
        return $this->renderer->render('@popup-erreur/ajax-ajout-signataire', [
            'id' => $post['id'],
            'rang' => $post['rang'],
            'listeTypeRole' => [
                'N' => $trad->t('Signataire'),
                'O' => $trad->t('Informé')
            ],
            'signataire' => $signataire,
            'listDeleg' =>
                (new WorkflowSignature($this->pdo))
                    ->delegByCDE($signataire['CODE']??''),
            'choixType' => $post['choixType']
        ]);
    }
    
    public function popupConfirmationSuppresionAction() : string
    {
        $post    = $this->request->getParsedBody();
        $path    = $post['Path'];
        $message = $post['Message'];
        $entity  = $post['Entity'];
        $div     = $post['div'];
        $form    = $post['form']??'';

        $message = urldecode($message);

        return $this->renderer->render('@popup-erreur/confirmationSuppresion', [
            'message' => $message,
            'pathURL' => $path,
            'entity'  => $entity,
            'div'     => $div,
            'form'    => $form,
            'post'    => $post,
        ]);
    }

    public function popupConfirmationMultiSuppressionAction() : string
    {
        $post    = $this->request->getParsedBody();

        return $this->renderer->render('@popup-erreur/confirmationMultiSuppression', [
            'message'       => $post['Message'],
            'pathURL'       => $post['Path'],
            'entity'        => $post['Entity'],
            'div'           => $post['div'],
            'form'          => $post['form'] ?? '',
            'titrePopup'    => $post['titlePopup'] ?? '',
        ]);
    }

    public function popupConfirmationGestionAction() : string
    {
        $post    = $this->request->getParsedBody();

        $path    = $post['Path'];
        $message = $post['Message'];
        $div     = $post['div'];
        $option  = $post['option']??'';
        $loader  = $post['loader']??false;
        $popup   = $post['popup']??false;
        $form    = $post['form']??'';
        $onclick  = $post['onclick']??'';
        $refresh   = $post['refresh']??false;
        $div_refresh  = $post['div_refresh']??'';
        $form_refresh  = $post['form_refresh']??'';
        $iconePopup  = $post['iconePopup']??'';
        $withOutPopup= $post['withOutPopup'] ?? false;
        $reload = $post['reload'] ?? false;

        $message = urldecode($message);
        
        return $this->renderer->render('@popup-erreur/confirmationGestion', [
            'message' => $message,
            'pathURL' => $path,
            'div'     => $div,
            'option'  => $option,
            'loader'  => $loader,
            'popup'   => $popup,
            'reload'  => $reload,
            'onclick' => $onclick,
            'refresh' => $refresh,
            'withOutPopup' => $withOutPopup,
            'div_refresh' => $div_refresh,
            'form_refresh' => $form_refresh,
            'form'    => $form,
            'iconePopup' => $iconePopup,
        ]);
    }
    public function popupConfirmationSuppressionNoteurAction() : string
    {
        $post    = $this->request->getParsedBody();

        $path    = $post['Path'];
        $message = $post['Message'];
        $div     = $post['div'];
        $option  = $post['option']??'';
        $loader  = $post['loader']??false;
        $popup   = $post['popup']??false;

        return $this->renderer->render('@popup-erreur/confirmationGestion', [
            'message' => $message,
            'pathURL' => $path,
            'div'     => $div,
            'option'  => $option,
            'loader'  => $loader,
            'popup'   => $popup,
        ]);
    }
    
    public function popupConfirmationRedirectionAction() : string
    {
        $post = $this->request->getParsedBody();

        $path = $post['Path'];
        $message = $post['Message'];

        return $this->renderer->render('@popup-erreur/confirmationRedirection', [
            'message' => $message,
            'pathURL' => $path,
        ]);
    }
    
    public function popupInformationAction() : string
    {
        $post = $this->request->getParsedBody();

        if (isset($post['pathRetour']) && $post['pathRetour'] != '' && $this->router->generateUri($post['pathRetour']) == Ariane::getBackPath()) {
            $postRetour = json_encode(Ariane::getBackPost());
        } else {
            $postRetour = '';
        }

        return $this->renderer->render('@popup-erreur/information', [
            'message' => $post['message']??$post['Message'],
            'pathRetour' => $post['pathRetour']??'',
            'postRetour' => $postRetour,
            'uriRetour' => $post['uriRetour']??'',
            'paramsRetour' => $post['paramsRetour']??'',
        ]);
    }
    
    public function popupErreurAlerteEtAvertissementAction() : string
    {
        $post = $this->request->getParsedBody();
        
        return $this->renderer->render('@popup-erreur/ErreurAlerteEtAvertissement', [
            'params' => $post['params'],
        ]);
    }
}
