<?php
namespace Modules\Fournisseur\Controller;

require_once 'application/helpers/downloadHelper.php';
use Skeleton\Controller\Controller;
use Helpers\Outil\Date;
use Entity\Model\Fourniss;
use Helpers\Fichier\Fichier;
use Modules\Fournisseur\Repository\AttestationsVisaGlobal;
use Modules\Fournisseur\Outil\DroitUtilisateur;
use Helpers\Ariane\ArianeSub;
use Helpers\Trad\Trad;

class AttestationsVisaGlobalFournisseurController extends Controller
{
    // phpcs:ignore
    public function AttestationsVisaGlobalAction()
    {
        $id = $this->request->getAttribute('id');
        $post = $this->request->getParsedBody();
        
        $fournis = new Fourniss($this->pdo);
        $fournis = $fournis->find($id);
        
        $listeAttestations = new AttestationsVisaGlobal($this->pdo);
        $listeAttestations = $listeAttestations->ListeAttestationFournisseur($id, $post);

        $paginator = new \Helpers\Pagination\Paginator();
        $infopagination = $paginator->generate($listeAttestations, 0, $post, $this->renderer);
        
        $trad = $this->getTranslationService();
        ArianeSub::generate(1, 0, $trad->t('Attestations visa global'), $_SERVER['REQUEST_URI'], 1, $post);
        
        return $this->renderer->render('@Fournisseur/AttestationsVisaGlobal/attestationsVG', [
            'menuActive' => 'Fournisseur',
            'menuLeft' => 'GestionFournisseur',
            "code_fournisseur" => $id,
            'infoPagination' => $infopagination,
            "libelle_fournisseur" => $fournis["RS"],
            "listeAttestations" => $listeAttestations,
        ]);
    }
    
    public function exportAttestationsAction() : string
    {
        $code = $this->request->getAttribute('id');
        $post = $this->request->getParsedBody();
        
        $Attestations = new AttestationsVisaGlobal($this->pdo);
        $Attestations = $Attestations->ListeAttestationFournisseur($code, $post, true);
        
        foreach ($Attestations as &$value) {
            $value['DATE_DEBUT'] = DATE::dateConverte($value['DATE_DEBUT']);
            $value['DATE_FIN'] = DATE::dateConverte($value['DATE_FIN']);
            $value['DATE_DECISION'] = DATE::dateConverte($value['DATE_DECISION']);
            $value['DATE_ENVOI'] = DATE::dateConverte($value['DATE_ENVOI']);
        }
       
        \Helpers\ExportTo\Outils::gestionEmailAsync2(
            "export_Attestation_Visa_Global_".$code,
            $Attestations,
            $this->JWTKEY,
            $this->router,
            $this->getTranslationService()
        );

        return 'ok';
    }
}
