<?php

namespace Modules\Consultation\Controller;

use Helpers\Session\Enums\MenuTopEnum;
use Helpers\Session\Session;
use Skeleton\Controller\Controller;
use Helpers\Ariane\Ariane;
use Helpers\Outil\Date;
use Modules\Consultation\Repository\AoEvent;
use TwigExtension\OutilExtension;

class ConsultationEventController extends Controller
{

    

    public function suiviAction() : string
    {
        $idAO       = $this->request->getAttribute('id');
        $codeFrs    = $this->request->getAttribute('idFrs');
        $typeFG     = $this->request->getAttribute('typeFG');
        $idQr       = $this->request->getAttribute('idQst');
        $post = $this->request->getParsedBody();

        if (!$post) {
            $post['tri_DTE'] = 'desc';
            $post['tri_HEURE'] = 'desc';
        }

        $trad = $this->getTranslationService();

        $aoEvent = new AoEvent($this->pdo);
        $listeEvent = $aoEvent->getListeEvent($idAO, $post, $codeFrs, $typeFG, $idQr);

        $paginator = new \Helpers\Pagination\Paginator();
        $infopagination = $paginator->generate($listeEvent, 50, $post, $this->renderer);

        Ariane::generate(2, $trad->t('Liste des évènements'));

        return $this->renderer->render('@Consultation/Consultation/suivi', [
            'idAO' => $idAO,

            'menuActive' => Session::verifySessionTypeMenuTop(MenuTopEnum::P2P) ? 'eproc':'Consultations',
            'menuLeft' => 'gestionAO',

            'listEventType' => array_column(
                $aoEvent->listEventType(),
                'LIBELLE',
                'CODE_SAO'
            ),
            'listeEvent' => $listeEvent,
            'post' => $post,
            'infoPagination' => $infopagination,
        ]);
    }


    public function exportAction(): string
    {
        $id = $this->request->getAttribute('id');
        $post = $this->request->getParsedBody();
        $trad = $this->getTranslationService();

        $this->getEmailCsvService()->sendCsv(
            'export_consultation_event_' . date('Ymd'),
            (new AoEvent($this->pdo))->export($id, $post),
            function (array $row) {
                return [
                    $row['NO_AO'],
                    $row['LIBELLE'],
                    'DTE' => Date::dateConverte($row['DTE']),
                    'HEURE' => Date::heureTrad($row['HEURE']),
                    OutilExtension::entityLabel($row, 'UTIL', 'Auteur_NOM'),
                    OutilExtension::entityLabel($row, 'SIGNAT', 'Signataire_NOM'),
                    $row['COMMENTAIRE'],
                ];
            },
            [
                $trad->t('Code'),
                $trad->t('Évènement'),
                $trad->t('Date'),
                $trad->t('Heure'),
                $trad->t('Auteur'),
                $trad->t('Signataire'),
                $trad->t('Commentaire'),
            ]
        );
        return 'ok';
    }
}
