<?php
namespace Modules\Catalogue\Controller;

use Entity\Model\CrossRefFmt;
use GuzzleHttp\Psr7\Response;
use Helpers\Ariane\Ariane;
use Helpers\Pagination\PaginatorGestion;
use Modules\Catalogue\Repository\FormatRepository;
use Skeleton\Controller\Controller;

class NomParametrageController extends Controller
{

    public function listeAction():string
    {
        $post = $this->request->getParsedBody();
        $trad = $this->getTranslationService();

        if (!$post) {
            $post['tri_IDCLE'] =  'asc';
            $post['tri_NOM_PARAMETRE'] =  'asc';
        }

        $paginator = new \Helpers\Pagination\PaginatorGestion();
        $paginator->generate(0, 'IDCLE');
        $liste = (new FormatRepository($this->pdo))->getListeNomParam($post);

        $nombreArticle  = PaginatorGestion::getNbrArticle();
        $infopagination = PaginatorGestion::getInfoPagination();

        Ariane::generate(0, $trad->t('Liste des paramètres de formats'), $post, $nombreArticle);

        return $this->renderer->render('@catalogue/NomParametrage/listeNomParametrage', [
            'menuActive'        => 'Catalogue',
            'liste'             => $liste,
            'post'              => $post,
            'infoPagination'    => $infopagination,
        ]);
    }

    public function createEditAction():string|Response
    {
        $idCle = $this->request->getAttribute('idCle');
        $nomParam = $this->request->getAttribute('nomParam');

        $post = $this->request->getParsedBody();
        $trad = $this->getTranslationService();

        if ($post) {
            $validator = $this->createValidator();
            $validator->isStringAvecEspace($trad->t('ID CLE'), $post['IDCLE'], 1, 10);
            $validator->isStringAvecEspace($trad->t('NOM PARAMETRE'), $post['NOM_PARAMETRE'], 1, 60);

            if ($validator->isValid()) {
                if ($idCle) {
                    (new CrossRefFmt($this->pdo))->update([
                        'IDCLE'         => $post['IDCLE'],
                        'NOM_PARAMETRE' => $post['NOM_PARAMETRE'],
                    ], [
                        'IDCLE'         => $idCle,
                        'NOM_PARAMETRE' => $nomParam,
                    ]);
                } else {
                    (new CrossRefFmt($this->pdo))->insert([
                        'IDCLE'         => $post['IDCLE'],
                        'NOM_PARAMETRE' => $post['NOM_PARAMETRE'],
                    ]);
                }

                $backPath = Ariane::getBackPath();
                $backPost = Ariane::getBackPost();

                return new Response(443, [], (string) json_encode(['path' => $backPath, 'post' => $backPost]));
            } else {
                return new Response(200, [], $this->renderer->render('@popup-erreur/ErreurCreation', [
                    'ErreurValid' => $validator->getError(),
                    'desactiveRow' => true,
                ]));
            }
        }

        $param = [];
        if ($idCle) {
            Ariane::generate(1, $trad->t('Modification d\'un paramètre'));
            $param = (new CrossRefFmt($this->pdo))->find([
                'IDCLE' => $idCle,
                'NOM_PARAMETRE' => $nomParam
            ]);
        } else {
            Ariane::generate(1, $trad->t('Création d\'un paramètre'));
        }


        return $this->renderer->render('@catalogue/NomParametrage/createEdit', [
            'menuActive'        => 'Catalogue',
            'param'             => $param,
            'idCle'             => $idCle,
            'nomParam'          => $nomParam,
        ]);
    }

    public function deleteAction():Response
    {
        $idCle = $this->request->getAttribute('idCle');
        $nomParam = $this->request->getAttribute('nomParam');
        
        (new CrossRefFmt($this->pdo))->delete([
            'IDCLE' => $idCle,
            'NOM_PARAMETRE' => $nomParam,
        ]);

        return new Response(200);
    }
}
