<?php

namespace Modules\ProjetAchat\Controller;

use Entity\Model\Hastutil;
use Modules\ProjetAchat\Service\ModeleService;
use Psr\Container\ContainerInterface;
use Skeleton\Controller\Controller;
use Helpers\Ariane\Ariane;
use Helpers\Trad\Trad;
use Modules\ProjetAchat\Repository\Modele;
use Entity\Model\DoarType;
use Entity\Model\DoarOpe;
use Skeleton\Renderer\RendererInterface;
use Skeleton\Router;
use Tamtamchik\SimpleFlash\Flash;
use Helpers\Outil\Roles;
use Modules\ProjetAchat\Service\ModeleProjetService;
use Modules\ProjetAchat\Repository\ElementConcerned;
use Helpers\ChampsSupplementaire\ChampSupp;
use Modules\ProjetAchat\MenuContextuel\GestionModeleMenuContextuel;

class GestionModeleController extends Controller
{
    /**
     * @var ModeleService
     */
    private $modelService;

    /**
     * @var Flash $flash
     */
    private $flash;

    /**
     * GestionModeleProjetAchatController constructor.
     * @param RendererInterface $renderer
     * @param \PDO $pdo
     * @param Router $router
     * @param ContainerInterface $contenaire
     */
    public function __construct(RendererInterface $renderer, \PDO $pdo, Router $router, ContainerInterface $contenaire)
    {
        parent::__construct($renderer, $pdo, $router, $contenaire);
        $this->modelService = new ModeleService($this->pdo);
        $this->flash = new Flash();
    }

    public function indexAction() : string
    {
        ModeleProjetService::resetDoaSession();
        
        $post = $this->request->getParsedBody();
        
        if (!$post) {
            $post['tri_REF'] = 'desc';
        }
        
        
        $droit = [
            'DOA_MOD_CM' => Roles::droitsUtilisateur($this->pdo, 'DOA_MOD_CM', $_SESSION["Session_Login"]),
            'DOA_MOD_TT' => Roles::droitsUtilisateur($this->pdo, 'DOA_MOD_TT', $_SESSION["Session_Login"]),
            'DOA_IMP_MO' => Roles::droitsUtilisateur($this->pdo, 'DOA_IMP_MO', $_SESSION["Session_Login"]),
            'DOA_DOA_CM' => roles::droitsUtilisateur($this->pdo, 'DOA_DOA_CM'),
        ];

        $ModeleProjetAchat = new Modele($this->pdo);
        $liste = $ModeleProjetAchat->listeModelesProjetAchat($post, $droit);
        $trad = $this->getTranslationService();

        $menuContextuel = new GestionModeleMenuContextuel($this->pdo, $this->router, $trad);
        foreach ($liste as &$projet) {
            $projet['menuContextuel'] = $menuContextuel->generate($projet, true);
        }

        

        $DoarType = new DoarType($this->pdo);
        $listeTMP = $DoarType->fetchAll();
 
        // YANIS H ERE
        foreach ($listeTMP as $item) {
            $listeType[$item['ID']]     = $item['LIB'];
        }

        $DoarOpe = new DoarOpe($this->pdo);
        $listeTMP = $DoarOpe->fetchAll();
        
        foreach ($listeTMP as $item) {
            $ListeOpe[$item['CODE_OPE']] = $item['LIB'];
        }
        
        Ariane::generate(0, $trad->t('Gestion'), $post, count($liste));
        
        $paginator = new \Helpers\Pagination\Paginator();
        $infopagination = $paginator->generate($liste, 0, $post, $this->renderer);

        $filtreAvance = new \Helpers\Filter\FiltreAvance\Generator($post);

        $elementConcernedRepo = new ElementConcerned($this->pdo);
        $elementConcerned = $elementConcernedRepo->getListElementsConcerned();
        
        if ($elementConcerned) {
            $listStd =  ChampSupp::getListeTableStd();
            foreach ($elementConcerned as $element) {
                if ($listStd[$element['TYPE_ELEM']]??[]) {
                    $valueSTD = $listStd[$element['TYPE_ELEM']];
                    $filtreAvance->setFiltre('DOAELEM_'.$element['TYPE_ELEM'], 'liste', $element['LIBELLE'], '', 'Ajax.liste.'.$valueSTD[1], $valueSTD[0], null, true);
                } else {
                    $champSupp = new ChampSupp($this->pdo);
                    $csType = $champSupp->tsDonExist($element['TYPE_ELEM']);
                    if ($csType) {
                        $filtreAvance->setFiltre('DOAELEM_'.$element['TYPE_ELEM'], 'liste', $element['LIBELLE'], '', ['path' => 'Ajax.liste.tsDon', 'params' => ['typeDico' => $csType['TYPE']]], 'tsDon', null, true);
                    }
                }
            }
        }
        
        return $this->renderer->render('@projetAchat/Modeles/index', [
            'menuActive'                => 'ProjetAchat',
            'infoPagination'            => $infopagination,
            'login'                     => $_SESSION['Session_Login'],
            'Utilisateur'               => (new Hastutil($this->pdo))
                                                ->find($_SESSION['Session_Login']),
            'filtreAvance'              => $filtreAvance->getFiltres(),
            'initFiltreAvance'          => $filtreAvance->getInit(),
            'liste'                     => $liste,
            'listeType'                 => $listeType,
            'ListeOpe'                  => $ListeOpe,
            'droit'                     => $droit,
            'post'                      => $post
        ]);
    }

    public function favoriAction() : string
    {
        $id = $this->request->getAttribute('idProjet');
        $post = $this->request->getParsedBody();

        $fav = new Modele($this->pdo);

        if ($post['favEta']!='-1') {
            $fav->rmvFavori($id, $_SESSION['Session_Login']);
        } else {
            $fav->addFavori($id, $_SESSION['Session_Login']);
        }

        return $id;
    }

    public function getModelService()
    {
        return $this->modelService;
    }
    public function getFlash()
    {
        return $this->flash;
    }
}
