<?php
namespace Modules\EtatStatistiques\Controller;

use Entity\Model\EtatGroupEtat;
use Entity\Model\EtatGrp;
use Entity\Model\EtatUser;
use Entity\Model\Hastutil;
use Helpers\Form\FormBuilder;
use Helpers\Outil\ArrayHelper;
use Helpers\Pagination\PaginatorGestion;
use Modules\EtatStatistiques\MenuContextuel\MenuGestionEtatstat;
use Modules\EtatStatistiques\Repository\EtatStatTypeRepo;
use Skeleton\Controller\Controller;

class GestionEtatStatTypeController extends Controller
{
    public function indexAction() :string
    {
        $post = $this->request->getParsedBody();
        $repo = new EtatStatTypeRepo($this->pdo);
        
        (new PaginatorGestion())->generate(10, 'CODETAT', 'ASC');
        $list = $repo->listEtatStatGestion($post);
        $pagination = PaginatorGestion::getInfoPagination();

        $list = ArrayHelper::joinManyToMany(
            $list,
            $repo->getMapEtatTheme(array_column($list, 'CODETAT')),
            'CODETAT',
            'listTheme'
        );

        $menu = new MenuGestionEtatstat($this->pdo, $this->router, $this->getTranslationService());
        
        return $this->renderer->render('@EtatStatistiques/gestion/gestion', [
            'post' => $post,
            'listRow' => array_map(function ($item) use ($menu) {
                return [
                    // 'Type' => '', Rendered in block cell_Type
                    'col3' => $item['NOMFIC'],
                    'menu' => $menu->generate($item),
                ] + $item;
            }, $list),
            'isAjax' => $this->request->isAjax(),
            'pagination' =>
                ($pagination ?? [])
                + ['path' => $this->router->generateUri(
                    $this->router->getNameRoute(),
                    $this->request->getAttributes()
                )]
        ]);
    }
}
