<?php
namespace Modules\GlobalApplication\Controller;

use Skeleton\Controller\Controller;
use Entity\Model\Fourniss;
use Modules\GlobalApplication\Repository\ArticlesAssocies;
use Helpers\Fichier\Fichier;
use Helpers\Outil\Sql;
use Helpers\Ariane\Ariane;
use Entity\Model\Grpfou;
use Helpers\Trad\Trad;
use Modules\GlobalApplication\MenuContextuel\ArticlesAssociesMenuContextuel;

class ArticlesAssociesController extends Controller
{

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

        $listeArticles = new ArticlesAssocies($this->pdo);
        $listeArticles = $listeArticles->listeArticlesAssocies($id, $type, $post);

        $paginator = new \Helpers\Pagination\Paginator();
        $infopagination = $paginator->generate($listeArticles, 10, $post, $this->renderer, 'Gestion');
        
        if ($type=='F') {
            $fournis = new Fourniss($this->pdo);
            $fournis = $fournis->find($id);
            $libelle = $fournis["RS"];
            $layout='@layoutFournisseur/fournisseur.twig';
            
            $menuActive= 'Fournisseur';
            $menuLeft='GestionFournisseur';
        }
        if ($type=='G') {
            $layout='@layoutFournisseur/groupement.twig';
            $menuActive= 'Fournisseur';
            $menuLeft='GestionGroupement';
            
            $Grp = new Grpfou($this->pdo);
            $Grp = $Grp->find($id);
            $libelle = $Grp["RS"];
        }
        

        $menuContextuel = new ArticlesAssociesMenuContextuel($this->pdo, $this->router, $this->getTranslationService());
        foreach ($listeArticles as &$article) {
            $article['menuContextuel'] = $menuContextuel->generate($article, $id, $type);
        }

        $trad = $this->getTranslationService();
        Ariane::generate(1, $trad->t('Articles associes'));

        return $this->renderer->render('@GlobalApplication/ArticlesAssocies/articlesAssocies', [
            'menuActive' => $menuActive,
            'menuLeft' => $menuLeft,
            "articleAssocies" => true,
            'infoPagination' => $infopagination,
            'listeArticles' => $listeArticles,
            'type' => $type,
            'layout' => $layout??'layout.twig',
            'id' => $id,
            'libelle' => $libelle??'',
            
        ]);
    }
    
    public function suppressionArticlesAssociesAction()
    {
        $id = $this->request->getAttribute('id');
        $type = $this->request->getAttribute('type');
        $idArticle = $this->request->getAttribute('idArticle');
        
        $listeArticles = new ArticlesAssocies($this->pdo);
        $listeArticles->suppresionArticles($id, $type, $idArticle);
    }
    
    public function creationArticlesAssociesAction()
    {
        $id = $this->request->getAttribute('id');
        $type = $this->request->getAttribute('type');
        $post = $this->request->getParsedBody();

        
        $trad = $this->getTranslationService();

        if ($this->request->getMethod() == 'POST') {
            $validator = $this->createValidator();
            $articlesAssocies = new ArticlesAssocies($this->pdo);
            
            $validator->zoom('Articles', $post['ID_OBJ_ASS'], $trad->t('Codes article'), $this->pdo);
            $validator->isInteger('Coefficient', $post['COEFF'], 1, 2);
            $validator->isCheck($post['OBLIG']);
            $validator->isTrue($articlesAssocies->articles($id, $type, $post['ID_OBJ_ASS']), $trad->t('Il ne peut y avoir deux articles pour un même fournisseur.'));

            if ($validator->isValid()) {
                $articlesAssocies->nouveauArtcles($id, $type, $post);
                
                return $this->redirect('GlobalApplication.ArticlesAssocies.articlesAssocies', ['type'=> $type, 'id' => $id]);
            } else {
                $erreur= $validator->getError();
            }
        }
        
        if ($type=='F') {
            $fournis = new Fourniss($this->pdo);
            $fournis = $fournis->find($id);
            $libelle = $fournis["RS"];
            $layout='@layoutFournisseur/fournisseur.twig';
            
            $menuActive= 'Fournisseur';
            $menuLeft='GestionFournisseur';
        }
        if ($type=='G') {
            $layout='@layoutFournisseur/groupement.twig';
            $menuActive= 'Fournisseur';
            $menuLeft='GestionGroupement';
            
            $Grp = new Grpfou($this->pdo);
            $Grp = $Grp->find($id);
            $libelle = $Grp["RS"];
        }
        
        Ariane::generate(2, $trad->t('Ajouter un Article associé'));
        
        return $this->renderer->render('@GlobalApplication/ArticlesAssocies/creationArticlesAssocies', [
            'menuActive' => $menuActive??'',
            'menuLeft' => $menuLeft??'',
            "libelle" => $libelle??'',
            "creationArticlesAssoc" => true,
            'articles' => $post,
            'ErreurValid' => $erreur ?? false,
            'type' => $type,
            'layout' => $layout??'layout.twig',
            "id" => $id??'',
            
        ]);
    }
    
    public function modificationArticlesAssociesAction()
    {
        $id = $this->request->getAttribute('id');
        $idArticle = $this->request->getAttribute('article');
        $type = $this->request->getAttribute('type');
        $post = $this->request->getParsedBody();
        
        $articlesAssocies = new ArticlesAssocies($this->pdo);
        $articles = $articlesAssocies->articles($id, $type, $idArticle);
        $articles['COEFF'] = intval($articles['COEFF']);
        
        $fournis = new Fourniss($this->pdo);
        $fournis = $fournis->find($id);

        
        $trad = $this->getTranslationService();

        if ($this->request->getMethod() == 'POST') {
            $validator = $this->createValidator();

            $validator->isInteger($trad->t('Coefficient'), $post['COEFF'], 0, 2);
            $validator->isCheck($post['OBLIG']);
            
            if ($validator->isValid()) {
                $articlesAssocies->updateArticles($id, $type, $idArticle, $post);
                
                return $this->redirect('GlobalApplication.ArticlesAssocies.articlesAssocies', ['type'=> $type, 'id' => $id]);
            } else {
                $articles=$post;
                $erreur= $validator->getError();
            }
        }
        
        if ($type=='F') {
            $fournis = new Fourniss($this->pdo);
            $fournis = $fournis->find($id);
            $libelle = $fournis["RS"];
            $layout='@layoutFournisseur/fournisseur.twig';
            
            $menuActive= 'Fournisseur';
            $menuLeft='GestionFournisseur';
        }
        if ($type=='G') {
            $layout='@layoutFournisseur/groupement.twig';
            $menuActive= 'Fournisseur';
            $menuLeft='GestionGroupement';
            
            $Grp = new Grpfou($this->pdo);
            $Grp = $Grp->find($id);
            $libelle = $Grp["RS"];
        }
        
        Ariane::generate(2, $trad->t('Modifier un article associé'));
        
        return $this->renderer->render('@GlobalApplication/ArticlesAssocies/creationArticlesAssocies', [
            'menuActive' => $menuActive??'',
            'menuLeft' => $menuLeft??'',
            "id" => $id??'',
            "libelle" => $libelle??'',
            "modificationArticlesAssoc" => true,
            'articles' => $articles,
            'ErreurValid' => $erreur ?? false,
            'type' => $type,
            'layout' => $layout??'layout.twig',
        ]);
    }
    
    public function exportArticlesAction() : string
    {
        $id = $this->request->getAttribute('id');
        $type = $this->request->getAttribute('type');
        $post = $this->request->getParsedBody();
        
        $listeArticles = new ArticlesAssocies($this->pdo);
        $listeArticles = $listeArticles->listeArticlesAssocies($id, $type, $post, true);
        
        foreach ($listeArticles as &$article) {
            $article['OBLIGATOIRE']=Sql::boolSql($article['OBLIGATOIRE'])? 'Oui':'Non';
        }
        
        \Helpers\ExportTo\Outils::gestionEmailAsync2(
            "export_articles_associes_".$id,
            $listeArticles,
            $this->JWTKEY,
            $this->router,
            $this->getTranslationService()
        );

        return 'ok';
    }
}
