<?php

namespace Modules\Contrat\Controller;

use Entity\Model\Acccadre;
use GuzzleHttp\Psr7\Response;
use Modules\Contrat\Repository\AccordCadreEventRepo;
use Skeleton\Controller\Controller;
use Helpers\Outil\Date;
use TwigExtension\OutilExtension;

class AccordCadreEventController extends Controller
{

    public function indexAction(): string
    {
        $idAcc = $this->request->getAttribute('idacc');
        return $this->renderer->render('@Contrat/accord-cadre/pageTableEvent', [
            'acd' => (new Acccadre($this->pdo))->find($idAcc),
            'listeSuiviEvent' =>
                (new AccordCadreEventRepo($this->pdo))->listEvent($idAcc),
        ]);
    }

    public function exportAction() :string
    {
        $trad = $this->getTranslationService();
        $idAcc = $this->request->getAttribute('idacc');
        
        $this->getEmailCsvService()->sendCsv(
            $this->JWTKEY,
            'export_acd_event_' . date('Ymd'),
            (new AccordCadreEventRepo($this->pdo))->export($idAcc),
            function ($row) {
                return [
                    $row['CODE_EVT'],
                    $row['LIB'],
                    Date::dateConverte($row['DTE']),
                    Date::heureTrad($row['HEURE']),
                    OutilExtension::entityLabel($row, 'UTIL', 'Util_NOM'),
                    $row['COMMENTAIRE'],
                ];
            },
            [
                $trad->t('Code'),
                $trad->t('Libellé'),
                $trad->t('Date'),
                $trad->t('Heure'),
                $trad->t('Auteur'),
                $trad->t('Commentaire'),
            ]
        );
        return 'ok';
    }
}
