<?php
namespace Modules\ProjetAchat\Controller;

use Helpers\Outil\Date;
use Helpers\Pagination\PaginatorGestion;
use Modules\ProjetAchat\Repository\ProjetAchatEventRepo;
use Skeleton\Controller\Controller;
use Helpers\Ariane\Ariane;
use Modules\ProjetAchat\Repository\ProjetAchat;
use Entity\Model\Doa;
use TwigExtension\OutilExtension;

class ProjetAchatEventController extends Controller
{
    public function suiviAllAction() : string
    {
        $post = $this->request->getParsedBody();
        if (!$post) {
            $post['tri_ID_DOA'] = 'desc';
            $post['tri_NUM_ORDRE'] = 'desc';
        }

        return $this->renderer->render('@projetAchat/ProjetAchat/Suivi', [
            'listeEvent' =>
                (new ProjetAchatEventRepo($this->pdo))->listEvent($post),
            'post' => $post,
            'infoPagination' => PaginatorGestion::getInfoPagination()
        ]);
    }

    public function suiviAction() : string
    {
        $post = $this->request->getParsedBody();
        $id = $this->request->getAttribute('id');
        
        if (!$post) {
            $post['tri_ID_DOA'] = 'desc';
            $post['tri_NUM_ORDRE'] = 'desc';
        }

        return $this->renderer->render('@projetAchat/ProjetAchat/Suivi', [
            'listeEvent' =>
                (new ProjetAchatEventRepo($this->pdo))
                    ->listEvent(['ID' => $id] + $post),
            'doa' => (new Doa($this->pdo))->find($id),
            'post' => $post,
            'infoPagination' => PaginatorGestion::getInfoPagination(),
        ]);
    }
    
    public function exportAction() :string
    {
        $post = $this->request->getParsedBody();
        $trad = $this->getTranslationService();
        $this->getEmailCsvService()->sendCsv(
            $this->JWTKEY,
            'export_proj_event_'.date('Ymd'),
            (new ProjetAchatEventRepo($this->pdo))->export($post),
            function (array $row) {
                return [
                    $row['Proj_REF'],
                    $row['Proj_LIB'],
                    //________________
                    $row['CODE_EVT'],
                    $row['LIB_EVENT'],
                    Date::dateConverte($row['DATE_EVT']),
                    Date::heureTrad($row['HEURE_EVT']),
                    OutilExtension::entityLabel($row, 'CODE_USER', 'Auteur_NOM'),
                    $row['COMMENTAIRE'],
                ];
            },
            [
                $trad->t('Référence'),
                $trad->t('Libellé projet'),
                //_____
                $trad->t('Code'),
                $trad->t('Évènement'),
                $trad->t('Date'),
                $trad->t('Heure'),
                $trad->t('Auteur'),
                $trad->t('Commentaire'),
            ]
        );
        return 'ok';
    }
}
