<?php

namespace Modules\Consultation\Controller;

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');
        $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);

        $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['typeMenu'] == '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(
            $this->JWTKEY,
            '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';
    }
}
