<?php
namespace Modules\Contrat\TwigExtension;

use Psr\Container\ContainerInterface;
use Skeleton\Router;
use Helpers\Trad\Trad;
use Entity\Model\Grpfou;
use Entity\Model\Marches;
use Entity\Model\Marchsoc;
use Entity\Model\Fourniss;
use Helpers\Trad\ITrad;

class OutilContratExtension extends \Twig\Extension\AbstractExtension
{
    protected $pdo;
    protected $contenaire;
    
    public function __construct(\PDO $pdo, ContainerInterface $contenaire)
    {
        $this->pdo = $pdo;
        $this->contenaire = $contenaire;
    }
    
    
    public function getFilters()
    {
        return array(
            'testAllParam' => new \Twig\TwigFilter('testAllParam', array($this, 'testAllParam')),
            'libelleByCodeGroupementFournisseur' => new \Twig\TwigFilter('libelleByCodeGroupementFournisseur', array($this, 'libelleByCodeGroupementFournisseur')),
            'commentaireMarche' => new \Twig\TwigFilter('commentaireMarche', array($this, 'commentaireMarche')),
            'libelleByCodeFournisseur' => new \Twig\TwigFilter('libelleByCodeFournisseur', array($this, 'libelleByCodeFournisseur')),
        );
    }
    
    public function getFunctions()
    {
        return [
            new \Twig\TwigFunction('preavisAccordCadre', [
                $this,
                'preavisAccordCadre'
            ], [
                'is_safe' => [
                    'html'
                ]
            ]),
            new \Twig\TwigFunction('findSocieteAutorise', [
                $this,
                'findSocieteAutorise'
            ], [
                'is_safe' => [
                    'html'
                ]
            ]),
            
            new \Twig\TwigFunction('afficheOrNotDetailArticle', [
                $this,
                'afficheOrNotDetailArticle'
            ], [
                'is_safe' => [
                    'html'
                ]
            ]),
            
        ];
    }
    
    
    public function preavisAccordCadre($acc, $trad) : string
    {
        /** @var ITrad $trad */
        $trad = $this->contenaire->get(\Helpers\Trad\ITrad::class);
        // if($acc['DEL_PRE'] == '')
        // {
        //     $acc['DEL_PRE'] = 0;
        // }
        if ($acc['DEL_PRE'] == 0) {
            return '';
        }
            
            
        if ($acc['JOU_MOI']=='M') {
            $acc['DEL_PRE'] = 30 * $acc['DEL_PRE'];
        }
        
        $dateTmp = strtotime($acc["DATE_FIN"]);
        $datePreavis = $dateTmp + ( (int) $acc['DEL_PRE'] * -3600 * 24);
        
        $datePreavis = date('Ymd', $datePreavis);
        if (date('Ymd')>=$datePreavis && date('Ymd')>=$acc["DATE_FIN"] && $acc["DATE_FIN"] != '00000000') {
            return '
          		<span class="statut alert_appreavis white-tooltip">
                      <span class="infos_tooltip top-left">
			   			<span class="tooltip-arrow"></span>
                    	'.$trad->t('Délais de préavis dépassé').'
               		 </span>
                </span> 
            ';
        } elseif (date('Ymd')>=$datePreavis && $acc["DATE_FIN"] != '00000000') {
            // OLD => alert_avpreavis
            return '
             	<span class="statut depassementPreavis white-tooltip">
                	<span class="infos_tooltip top-left">
			   			<span class="tooltip-arrow"></span>
                    	'.$trad->t('Approche de dépassement du délai de préavis').'
               		</span>
                </span> 
            ';
        }

        return '';
    }
    
    // Cette fonction va nous permettre de savoir si une societ eest bien présente dans la table marchsoc
    public function findSocieteAutorise($codeMarche, $codeSociete)
    {
        $marchSoc = new Marchsoc($this->pdo);
        
        $arraySearch = ['CODEMARCH' => $codeMarche,
            'CODESOC' => $codeSociete];
        
        $marchSoc = $marchSoc->find($arraySearch);
        if (isset($marchSoc["CODEMARCH"]) && $marchSoc['CODESOC']) {
            return 1;
        }
        
        return 0;
    }
    
    public function afficheOrNotDetailArticle($codeAcd, $codeArt)
    {
        $req ="select * FROM ACARTDET with (nolock) WHERE ACARTDET.CODE_ACD = :value1 AND CODE_ART = :value2";
        $arrayReq = [
            ':value1' => $codeAcd,
            ':value2'=> $codeArt
        ];
        
        $detailArticle = $this->pdo->prepare($req);
        $detailArticle->execute($arrayReq);
        
        return $detailArticle->fetch();
    }

    public function libelleByCodeGroupementFournisseur($value)
    {
        $fourniss = new Grpfou($this->pdo);
        $fourniss = $fourniss->find($value);
        if ($fourniss["CODE"]) {
            return $fourniss["RS"];
        }
        
        return " ";
    }
    
    // Cette fonction va nous permettre de tester si le type c'est ALL (ACD) dans le cas ou c'est ALL on affiche rien dans la creation d'un ACD
    public function testAllParam($value)
    {
        if ($value == 'ALL') {
            return '';
        } else {
            return $value;
        }
    }
    
    // SPECIFIQUE POUR L'AFFICHAGE DES COMMENTAIRE SUR LES MARCHES
    public function commentaireMarche($code)
    {
        $req="    select * from MARCHES ma with (nolock)
                    left join MEMOS m with (nolock) on m.NODEMEMO=ma.NMCOMMEN
                    WHERE ma.CODE = :value";
        
        $arrayReq = [':value' => $code];
        $typeMarche = $this->pdo->prepare($req);
        
        $typeMarche->execute($arrayReq);
        return $typeMarche->fetch()['CONTMEMO'];
    }
    
    public function libelleByCodeFournisseur($value)
    {
        $fourniss = new Fourniss($this->pdo);
        $fourniss = $fourniss->find($value);
        if ($fourniss["CODE"]) {
            return $fourniss["RS"];
        }
        
        return " ";
    }
}
