<?php
namespace Modules\Fournisseur\Controller;

use Skeleton\Controller\Controller;
use Entity\Model\Fourniss;
use Modules\Fournisseur\Repository\Attestations;
use Helpers\Fichier\Fichier;
use Helpers\Outil\Date;
use Modules\Fournisseur\Outil\DroitUtilisateur;
use Helpers\Ariane\ArianeSub;
use Helpers\Trad\Trad;
use Modules\Fournisseur\MenuContextuel\AttestationsFournisseurMenuContextuel;

class AttestationsFournisseurController extends Controller
{
    // phpcs:ignore
    public function AttestationsAction()
    {
        $id = $this->request->getAttribute('id');
        $post = $this->request->getParsedBody();
        
        /* trad */
        
        $trad = $this->getTranslationService();
        
        $fournis = new Fourniss($this->pdo);
        $fournis = $fournis->find($id);
        
        $listeAttestations = new Attestations($this->pdo);
        $listeAttestations = $listeAttestations->ListeAttestationFournisseur($id, $post);

        $menuContextuel = new AttestationsFournisseurMenuContextuel($this->pdo, $this->router, $this->getTranslationService());
        foreach ($listeAttestations as &$attestation) {
            $attestation['menuContextuel'] = $menuContextuel->generate($id, $attestation['NOATTEST']);
        }

        $listeTypeOperation= [
            'E' => $trad->t('Exportation'),
            'I' => $trad->t('Intracommunautaire'),
            'C' => $trad->t('Comptoir de vente'),
            'D' => $trad->t('Distance'),
            'M' => $trad->t('Montage'),
        ];

        $paginator = new \Helpers\Pagination\Paginator();
        $infopagination = $paginator->generate($listeAttestations, 0, $post, $this->renderer);
        
        ArianeSub::generate(1, 0, $trad->t('Attestations'), $_SERVER['REQUEST_URI'], 1, $post);
        
        return $this->renderer->render('@Fournisseur/Attestations/attestations', [
            'menuActive' => 'Fournisseur',
            'menuLeft' => 'GestionFournisseur',
            "code_fournisseur" => $id,
            'infoPagination' => $infopagination,
            "libelle_fournisseur" => $fournis["RS"],
            "listeAttestations" => $listeAttestations,
            'listeTypeOpe' => $listeTypeOperation,
            
        ]);
    }
    
    public function ajaxSuppresionAttestationAction() : string
    {
        $code = $this->request->getAttribute('id');
        $attestation = $this->request->getAttribute('attestation');
        
        $attestations = new Attestations($this->pdo);
        $attestations->SuppressionAttestations($code, $attestation);
        $trad = $this->getTranslationService();
            
        return $this->renderer->render('@popup-erreur/confirmationGestion', [
            'message' => $trad->t("Votre attestation a été supprimer"),
            'iconePopup' => 'information',
        ]);
    }
    
    public function popupConfirmationSuppressionAction() : string
    {
        $code = $this->request->getAttribute('id');
        $post = $this->request->getParsedBody();
        
        $fournis = new Fourniss($this->pdo);
        $fournis = $fournis->find($code);
        
        return $this->renderer->render('@Fournisseur/Popup-Attestations/confirmationSuppresionAttesttion', [
            "code_fournisseur" => $code,
            "idAttestation" => $post['divCode'],
            "fournisseurLibelle" => $fournis["RS"],
        ]);
    }
    
    public function exportAttestationsAction() : string
    {
        $code = $this->request->getAttribute('id');
        $post = $this->request->getParsedBody();
        
        $Attestations = new Attestations($this->pdo);
        $Attestations = $Attestations->ListeAttestationFournisseur($code, $post, true);
        
        foreach ($Attestations as &$value) {
            switch ($value['TYPE_OPERATION']) {
                case 'E':
                    $value['TYPE_OPERATION'] = 'Exportation';
                    break;
                case 'I':
                    $value['TYPE_OPERATION'] = 'Intracommunautaire';
                    break;
                case 'C':
                    $value['TYPE_OPERATION'] = 'Comptoir de vente';
                    break;
                case 'D':
                    $value['TYPE_OPERATION'] = 'Distance';
                    break;
                case 'M':
                    $value['TYPE_OPERATION'] = 'Montage';
                    break;
            }
            $value['DATE_CREATION'] = DATE::dateConverte($value['DATE_CREATION']);
        }

        \Helpers\ExportTo\Outils::gestionEmailAsync2(
            "export_Attestation_".$code,
            $Attestations,
            $this->JWTKEY,
            $this->router,
            $this->getTranslationService()
        );

        return 'ok';
    }
}
