<?php
namespace Modules\Consultation\Controller;

use Skeleton\Controller\Controller;
use Modules\Consultation\Repository\ReferencementAO;
use Helpers\Ariane\Ariane;
use Helpers\Trad\Trad;
use Modules\Consultation\WebService\Consultation;
use Modules\Consultation\Service\ConsultationService;
use Modules\Consultation\Repository\AoEvent;

class ReferencementAOController extends Controller
{
    public function indexAction() : string
    {
        $post = $this->request->getParsedBody();
        
        if (!$post) {
            $post['filter_ACCEPT_REFU'] = 'ALL';
            $post['filter_ACH'] = $_SESSION["Session_Login"];
        }
        
        $refAo = new ReferencementAO($this->pdo);
        $listeREF = $refAo->listeREFAO($post);
        
        $trad = $this->getTranslationService();
        $tabStatut = ['ALL' => $trad->t('À traiter') ,'R' => $trad->t('Refuser'), 'A' => $trad->t('Accepter')];

        $paginator      = new \Helpers\Pagination\Paginator();
        $infopagination = $paginator->generate($listeREF, 0, $post, $this->renderer);
        
        Ariane::generate(0, $trad->t('Référencement RFx'), $post);
        
        $trad = $this->getTranslationService();
        
        return $this->renderer->render('@Consultation/ReferencementAO/listeReferencementAO', [
            'menuActive' => $_SESSION['typeMenu'] == 'P2P'? 'eproc':'Consultations',
            'menuLeft' => 'gestionAO',
            'listeReferencement' => $listeREF,
            'countRef' => count($listeREF),
            'tabStatut' => $tabStatut,
            'infoPagination'    => $infopagination,
            'post' => $post,
            'login' => $_SESSION["Session_Login"],
        ]);
    }

    public function accepterReferencementAOAction() : string
    {
        $codeTrait = $this->request->getAttribute('codeTrait');
        
        $refAo = new ReferencementAO($this->pdo);
        $aoWSDL = new Consultation();
        
        $detailRefAO = $refAo->detailRefAo($codeTrait);
        $trad = $this->getTranslationService();
        
        //$refAo->insertRefAo($array);
        
        $consultationService = new ConsultationService($this->pdo, $this->getTranslationService());
        $consultationService->addFourniss($detailRefAO['NO_AO'], 'F', $detailRefAO['CODE_FOU']);
        
        $refAo->updateDemRefAO($codeTrait, 'A');
        
        $array=[
            'Utilisateur' => $_SESSION['Session_Login'],
            'NumAO' => $detailRefAO['NO_AO'],
            'CODE_FOUR' => $detailRefAO['CODE_FOU'],
            'Mode' => 'A',
            'Commentaire' => '',
        ];
        
        $trad = $this->getTranslationService();

        $xmlResult = $aoWSDL->refAo_ACC_REF($array);
        
        $reponse = new \SimpleXMLElement($xmlResult->responseData);
        
        if ($reponse->Statut=="O") {
            $aoEvent = new AoEvent($this->pdo);
            $aoEvent->rqInsertEventAO($detailRefAO['NO_AO'], 'ENV', 'Ajout du fournisseur sur une consultation en cours', $detailRefAO['CODE_FOU'], 'F');
            
            return $this->renderer->render('@popup-erreur/confirmationNoteur', [
                'message' => $trad->t('La demande a été acceptée.'),
                'div_refresh' => 'refAO',
                'form_refresh' => 'filtre_refAO',
                'refresh' => true,
            ]);
        } else {
            return $this->renderer->render('@popup-erreur/ErreurCreation', [
                'ErreurValid' => $reponse->MsgErreur,
            ]);
        }
    }
    
    public function popupRefusAOAction() : string
    {
        $codeTrait = $this->request->getAttribute('codeTrait');
        
        return $this->renderer->render('@Consultation/ReferencementAO/popupRefus', [
            'codeTrait' => $codeTrait,
        ]);
    }
    
    public function refuserReferencementAOAction() : string
    {
        $post = $this->request->getParsedBody();
        $codeTrait = $this->request->getAttribute('codeTrait');
        
        $refAo = new ReferencementAO($this->pdo);
        $detailRefAO = $refAo->detailRefAo($codeTrait);
        
        $refAo->updateDemRefAO($codeTrait, 'R');
        
        $array=[
            'Utilisateur' => $_SESSION['Session_Login'],
            'NumAO' => $detailRefAO['NO_AO'],
            'CODE_FOUR' => $detailRefAO['CODE_FOU'],
            'Mode' => 'R',
            'Commentaire' => $post['comment_refus'],
        ];
        $trad = $this->getTranslationService();

        $aoWSDL = new Consultation();
        $xmlResult = $aoWSDL->refAo_ACC_REF($array);
        
        $reponse = new \SimpleXMLElement($xmlResult->responseData);
        
        if ($reponse->Statut=="O") {
            return $this->renderer->render('@popup-erreur/confirmationNoteur', [
                'message' => $trad->t('La demande a été refusée.'),
                'div_refresh' => 'refAO',
                'form_refresh' => 'filtre_refAO',
                'refresh' => true,
            ]);
        } else {
            return $this->renderer->render('@popup-erreur/ErreurCreation', [
                'ErreurValid' => $reponse->MsgErreur,
            ]);
        }
    }
}
