<?php
namespace Modules\Catalogue\Controller;

use Entity\Model\ArtEquiv;
use Entity\Model\CarArt;
use Entity\Model\CataEnt;
use Entity\Model\CCataSrc;
use Entity\Model\EquivArt;
use Entity\Model\VersCata;
use GuzzleHttp\Psr7\Response;
use Helpers\Ariane\Ariane;
use Helpers\Ariane\ArianeSub;
use Helpers\Outil\Sql;
use Helpers\Pagination\PaginatorGestion;
use Helpers\Parametres\Constante;
use Modules\Ajax\Repository\Listes;
use Modules\Catalogue\MenuContextuel\ArticleEquivalentMenuContextuel;
use Modules\Catalogue\Repository\CatalogueExterneRepository;
use Modules\Catalogue\Repository\ControleRepository;
use Modules\Consultation\Repository\Reponse;
use Skeleton\Controller\Controller;

class AffectationTypeArticleController extends Controller
{
    public function parArticleAction():string
    {
        $idVersion = $this->request->getAttribute('idVersion');
        $post = $this->request->getParsedBody();
        $trad = $this->getTranslationService();

        Ariane::generate(1, $trad->t('Affectation des types d\'article'));

        ArianeSub::init();
        ArianeSub::generate(0, 0, $trad->t('Par article'), $this->router->generateUri('Catalogue.AffectationTypeArticle.parArticle', ['idVersion' => $idVersion]), 1);
        ArianeSub::generate(0, 1, $trad->t('Par sous-famille / Gamme'), $this->router->generateUri('Catalogue.AffectationTypeArticle.parSsfamilleGamme', ['idVersion' => $idVersion]), 0);

        if ($idVersion) {
            $post['filter_egal_IDVERCATA'] = $idVersion;

            $versCata = (new VersCata($this->pdo))->find(['IDATACATA' => $idVersion]);
            $infoCata = (new CataEnt($this->pdo))->find(['ID_CATAL' => $versCata['ID_CATAL']]);
        }

        (new PaginatorGestion())->generate(0, 'IDLIGNE', 'asc');
        $liste = (new Listes($this->pdo))->listeArticleCatalogue($post);

        $infoPagination = PaginatorGestion::getInfoPagination();

        return $this->renderer->render('@catalogue/AffectationTypeArticle/parArticle', [
            'idVersion' => $idVersion,
            'listeArticle' => (new ControleRepository($this->pdo))->listeArticles($post, $idVersion),
            'infoPagination' => $infoPagination,
            'listes' => $liste,
            'infoCata' => $infoCata??[],
        ]);
    }

    public function parSsfamilleGammeAction():string
    {
        $idVersion = $this->request->getAttribute('idVersion');
        $post = $this->request->getParsedBody();
        $trad = $this->getTranslationService();

        Ariane::generate(1, $trad->t('Affectation des types d\'article'));

        ArianeSub::generate(0, 1, $trad->t('Par sous-famille / Gamme'), $this->router->generateUri('Catalogue.AffectationTypeArticle.parSsfamilleGamme', ['idVersion' => $idVersion]), 1);

        $listeCaracSfam = (new CarArt($this->pdo))->fetchAll([], $post);
        $listeCaracGamme = (new CarArt($this->pdo))->fetchAll([], $post);

        if ($idVersion) {
            $post['filter_egal_IDVERCATA'] = $idVersion;

            $versCata = (new VersCata($this->pdo))->find(['IDATACATA' => $idVersion]);
            $infoCata = (new CataEnt($this->pdo))->find(['ID_CATAL' => $versCata['ID_CATAL']]);

            $post['filter_egal_IDVERCATA'] = $idVersion;
        }

        $catalogueRepo = new CatalogueExterneRepository($this->pdo);

        (new PaginatorGestion())->generate(10, 'SOUSFAMI', 'asc');
        $listeSsFamille = $catalogueRepo->getListeSousFamilleCatalogue($post);
        $infoPaginationSFA = PaginatorGestion::getInfoPagination();

        (new PaginatorGestion())->generate(10, 'FABRICANT', 'asc');
        $listeGamme = $catalogueRepo->geteListeGammeCatalogue($post);
        $infoPaginationGamme = PaginatorGestion::getInfoPagination();

        return $this->renderer->render('@catalogue/AffectationTypeArticle/parSsfamilleGamme', [
            'idVersion' => $idVersion,
            'infoCata' => $infoCata??[],
            'post' => $post,

            'codeSfam' => $post['codeSfam']??$listeSsFamille['0']['SOUSFAMI'],

            'codeGamme' => $post['codeGamme']??$listeGamme['0']['GAMFABRIC'],
            'codeFabricant' => $post['fabricant']??$listeGamme['0']['FABRICANT'],

            'listeGamme' => $listeGamme,
            'listeSsFamille' => $listeSsFamille,
            'listeCaracSfam' => $listeCaracSfam,
            'listeCaracGamme' => $listeCaracGamme,

            'infoPaginationSFA' => $infoPaginationSFA,
            'infoPaginationGamme' => $infoPaginationGamme
        ]);
    }

    public function ajaxAppliquerChangementAction():Response
    {
        $idVersion = $this->request->getAttribute('idVersion');
        $post = $this->request->getParsedBody();

        $listeUpdate = [];
        foreach ($post['selected_carac_forSfam'] as $sfam) {
            list ($codeSfa, $caracArt) = explode(Constante::spliter, $sfam);
            
            $listeUpdate[$caracArt]['SFA'][] = $codeSfa;
        }
        foreach ($post['selected_carac_forGamme'] as $gamme) {
            list ($gamme, $fabricant, $caracArt) = explode(Constante::spliter, $gamme);

            $listeUpdate[$caracArt]['GAMME'][] = [
                'GAMME' => $gamme,
                'FABRICANT' => $fabricant
            ];
        }

        $catalogueRepo = new CatalogueExterneRepository($this->pdo);
        foreach ($listeUpdate as $key => $update) {
            $catalogueRepo->updateTypeArticle($key, $update, false, $idVersion);
            $catalogueRepo->updateTypeArticle($key, $update, true, $idVersion);
        }

        return new Response(200, [], $this->renderer->render('@popup-erreur/information', [
            'message' => $this->getTranslationService()->t('Les changements ont été appliquer.'),
        ]));
    }
}
