<?php

namespace Modules\Fournisseur\Controller;

use Entity\Model\ParamDico;
use Modules\Evaluation\Service\ResultatService;
use Skeleton\Controller\Controller;
use Entity\Model\Fourniss;
use Modules\Fournisseur\Repository\Evaluation;
use Helpers\Fichier\Fichier;
use Helpers\Ariane\ArianeSub;
use Helpers\ChargeDocument\ChargeDocument;
use Helpers\ExportTo\Outils;
use Helpers\Outil\Sql;
use Modules\Fournisseur\MenuContextuel\EvaluationMenuContextuel;
use PDO;

class EvaluationController extends Controller
{
    public function evaluationAction(): string
    {
        $codeFrs          = $this->request->getAttribute('id');
        $fournis          = (new Fourniss($this->pdo))->find($codeFrs);
        $eval             = (new evaluation($this->pdo))->AllEvaluation($codeFrs);
        $trad             = $this->getTranslationService();
        $menuContextuel   = new EvaluationMenuContextuel($this->pdo, $this->router, $trad);

        foreach ($eval as &$e) {
            $e['menuContextuel'] = $menuContextuel->generate($e['CODE_FOU'], $e['ID_CAMP']);
        }

        $param = (new ResultatService($this->pdo))->getEvalParamdico();

        ArianeSub::generate(0, 4, $trad->t('Évaluation'), $_SERVER['REQUEST_URI'], 1);

        return $this->renderer->render('@Fournisseur/Evaluations/evaluations', [
            'menuActive'          => 'Fournisseur',
            'menuLeft'            => 'GestionFournisseur',
            "code_fournisseur"    => $fournis["CODE"],
            "libelle_fournisseur" => $fournis["RS"],
            "liste_eval"          => $eval,
            "param"          => $param,
            "eval"                => true,
        ]);
    }

    public function listeNoteurAction(): string
    {
        $codeFrs    = $this->request->getAttribute('id');
        $campagne   = $this->request->getAttribute('campagne');

        $eval = (new Evaluation($this->pdo))->ListeNoteurs($codeFrs, $campagne);

        return $this->renderer->render('@Fournisseur/Popup-Evaluation/listeNoteur', [
            'liste_eval'          => $eval,
            'campagne'            => $campagne,
            'code_fournisseur'    => $codeFrs,
        ]);
    }

    public function radarPerfAction(): string
    {
        error_reporting(0);

        $codeFrs    = $this->request->getAttribute('id');
        $campagne   = $this->request->getAttribute('campagne');
        $fournis    = (new Fourniss($this->pdo))->find($codeFrs);

        $array = [
            'CodeFour' => $codeFrs,
            'idCamp' => $campagne,
        ];

        $wsdl_eval    = (new \Modules\Fournisseur\WebService\Evaluation())->radarEvaluation($array);
        $reponse      = new \SimpleXMLElement($wsdl_eval->responseData);
        $pathInfo     = pathinfo($reponse->URL);

        $test = (new ChargeDocument())->chargeDoc($this->pdo, $pathInfo['basename'], 'tmp', true);
        $trad = $this->getTranslationService();
        ArianeSub::generate(1, 1, $trad->t('Radar de perf.'), $_SERVER['REQUEST_URI'], 1);

        $url      = $pathInfo['basename'];

        $url = $this->router->generateUri('GlobalApplication.ChargeDocument.chargeDocument', [
            'emplacement' => 'tmp',
            'urlFichier' => $url,
            'modeAffichage' => 0
        ]);

        return $this->renderer->render('@Fournisseur/Evaluations/radarPerf', [
            'menuActive'          => 'Fournisseur',
            'menuLeft'            => 'GestionFournisseur',
            'code_fournisseur'    => $fournis['CODE'],
            'libelle_fournisseur' => $fournis['RS'],
            'fileName'            => $url,
        ]);
    }

    public function imprimerAction(): string
    {
        $codeFrs    = $this->request->getAttribute('id');
        $campagne   = $this->request->getAttribute('campagne');

        $array = [
            'idCamp'    => $campagne,
            'type'      => '',
            'CodeFour'  => $codeFrs,
        ];

        $wsdl_eval    = (new \Modules\Fournisseur\WebService\Evaluation())->imprimerEvaluation($array);
        $reponse      = new \SimpleXMLElement($wsdl_eval->responseData);
        $extention    = pathinfo($reponse->FichierPDF);
        $url          = Fichier::URLDownload($extention['basename']);
        $message      = Fichier::UploadClient($url, $extention['basename']);
        return $message;
    }

    public function resultatAction(): string
    {
        $codeFrs    = $this->request->getAttribute('id');
        $campagne   = $this->request->getAttribute('campagne');
        $fournis    = (new Fourniss($this->pdo))->find($codeFrs);
        $xmlResult  = (new \Modules\Fournisseur\WebService\Evaluation())
            ->evaluation(['CodeFournisseur' => $codeFrs, 'IdCampagne' => $campagne]);
        $listePere  = new \SimpleXMLElement($xmlResult->responseData);
        $trad       = $this->getTranslationService();
        ArianeSub::generate(1, 1, $trad->t('Résultat'), $_SERVER['REQUEST_URI'], 1);

        return $this->renderer->render('@Fournisseur/Evaluations/resultat', [
            'menuActive'          => 'Fournisseur',
            'menuLeft'            => 'GestionFournisseur',
            "code_fournisseur"    => $fournis["CODE"],
            "libelle_fournisseur" => $fournis["RS"],
            "liste_pere"          => $listePere->Resultat,
            "resultat"            => true,
            'campagne'            => $campagne,
        ]);
    }

    public function exportNoteurAction(): string
    {
        $codeFrs    = $this->request->getAttribute('id');
        $campagne   = $this->request->getAttribute('campagne');
        $eval       = (new Evaluation($this->pdo))->ListeNoteurs($codeFrs, $campagne);

        Outils::gestionEmailAsync2(
            'export_liste_noteur',
            $eval,
            $this->JWTKEY,
            $this->router,
            $this->getTranslationService()
        );

        return 'ok';
    }
}
