<?php
namespace Modules\Fournisseur\Controller;

use Helpers\Outil\Date;
use Helpers\Outil\MenuContextuel;
use Helpers\Outil\Outils;
use Modules\Fournisseur\MenuContextuel\ArticlesMenuContextuel;
use Skeleton\Controller\Controller;
use Entity\Model\Fourniss;
use Modules\Fournisseur\Repository\Articles;
use Helpers\Fichier\Fichier;
use Modules\Fournisseur\Outil\DroitUtilisateur;
use Entity\Model\Grpfou;
use Helpers\Ariane\ArianeSub;
use Helpers\Outil\Roles;
use Helpers\Outil\Sql;
use Helpers\Trad\Trad;
use TwigExtension\OutilExtension;

class ArticlesController extends Controller
{
    public function articlesAction()
    {
        $id = $this->request->getAttribute('id');
        $post = $this->request->getParsedBody();
        $module = $this->request->getAttribute('MODULE');
        $trad = $this->getTranslationService();

        $articles = new Articles($this->pdo);

        if ($module=='Fournisseur') {
            $fournis = new Fourniss($this->pdo);
            $fournis = $fournis->find($id);
            $libelle = $fournis['RS'];
            $titre= $trad->t('Fournisseurs');
            
            $listeArticles= $articles->listeArticlesFournisseur($id, $post);
            $menuActive = 'Fournisseur';
            $menuLeft = 'GestionFournisseur';
        } else {
            $titre=$trad->t('Groupements de fournisseur');
            
            $listeArticles= $articles->listeArticlesGroupement($id, $post);
            $menuLeft = 'GestionGroupement';
            $menuActive = 'Fournisseur';
            
            $grp = new Grpfou($this->pdo);
            $grp = $grp->find($id);
            $libelle = $grp['RS'];
        }

        foreach ($listeArticles as &$article) {
            $article['menuContextuel'] = (new ArticlesMenuContextuel($this->pdo, $this->router, $this->getTranslationService()))
                ->generate($module, $id, $article['CODE_ART'], Sql::boolSql($article['STOCKE']));
        }

        $paginator = new \Helpers\Pagination\Paginator();
        $infopagination = $paginator->generate($listeArticles, 0, $post, $this->renderer);
        
        /* trad */
        
        $trad = $this->getTranslationService();
        
        ArianeSub::generate(1, 0, $trad->t('Article'), $_SERVER['REQUEST_URI'], 1, $post);

        /* droits */

        $droits = [
            'HA_A/F_CRE',
            'HA_A/F_MOD'
        ];
        
        return $this->renderer->render('@Fournisseur/Articles/articles', [
            'menuActive' => $menuActive,
            'menuLeft' => $menuLeft,
            "id" => $id,
            "libelle" => $libelle??'',
            'infoPagination' => $infopagination,
            'listeArticles' => $listeArticles,
            'MODULE' => $module,
            'TITRE' => $titre,
            'Droits' => $droits
        ]);
    }
    
    public function accordCadreAction() : string
    {
        $id = $this->request->getAttribute('id');
        $article = $this->request->getAttribute('article');
        $post = $this->request->getParsedBody();
        $module = $this->request->getAttribute('MODULE');
        
        $fournis = new Fourniss($this->pdo);
        $fournis = $fournis->find($id);
        
        $liste = new Articles($this->pdo);
        $liste = $liste->listeAccCadreArticles($id, $article, $module, $post);
        
        $paginator = new \Helpers\Pagination\Paginator();
        $infopagination = $paginator->generate($liste, 0, $post, $this->renderer);
        
        return $this->renderer->render('@Fournisseur/Articles/AccordCadre', [
            'menuActive' => 'Fournisseur',
            'menuLeft' => 'GestionFournisseur',
            "id" => $id,
            "libelle" => $fournis["RS"],
            'infoPagination' => $infopagination,
            'listeAcc' => $liste,
            'layout' => '@layoutFournisseur/fournisseur.twig',
            'MODULE' => $module,
            
        ]);
    }
    
    public function exportArticleFGAction() : string
    {
        $post = $this->request->getParsedBody();
        $id = $this->request->getAttribute('id');
        $module = $this->request->getAttribute('MODULE');

        $articles = new Articles($this->pdo);
        
        if ($module=='Fournisseur') {
            $listeArticles = $articles->listeArticlesFournisseur($id, $post, true);
        } else {
            $listeArticles = $articles->listeArticlesGroupement($id, $post, true);
        }
        $trad = $this->getTranslationService();

        // TODO : factorize OutilExtension::prixUnitaire into an Helper
        $listeArticles = array_map(function ($article) {
            return [
                'CODE_ARTICLE' => $article['CODE_ARTICLE'],
                'DESIGNATION' => $article['DESIGNATION'],
                'REF_FOURNISSEUR' => $article['REF_FOURNISSEUR'],
                'PU' => Outils::formatPrixUnitaire($article['PU']),
                'DATE_DEBUT' => Date::dateConverte($article['DATE_DEBUT']),
                'DATE_FIN' => Date::dateConverte($article['DATE_FIN']),
            ];
        }, $listeArticles);
    
        \Helpers\ExportTo\Outils::gestionEmailAsync2(
            "export_liste_Article_".$module.'_'.$id,
            $listeArticles,
            $this->JWTKEY,
            $this->router,
            $trad,
            [
                $trad->t('Code article'),
                $trad->t('Designation'),
                $trad->t('Réf. Article Fournisseur'),
                $trad->t('Prix'),
                $trad->t('Date de validité début'),
                $trad->t('Date de validité fin'),
            ]
        );

        return 'ok';
    }
    
    public function deleteArticleGFAction() : string
    {
        $id = $this->request->getAttribute('id');
        $module = $this->request->getAttribute('MODULE');
        $article = $this->request->getAttribute('article');
        
        $ART = new Articles($this->pdo);
        $ART->deleteArticleFG($id, $article, $module);
        $ART->deleteListePrixFG($id, $article, $module);
        
        return '';
    }
}
