<?php

namespace Modules\Additional\Specifique\Client132\HTML;

use Entity\Model\Dilispec;
use Helpers\Trad\Trad;
use Entity\Model\Doa;
use Entity\Model\Hastutil;
use Helpers\ChargeDocument\ChargeDocument;
use Psr\Container\ContainerInterface;

use Modules\Additional\Specifique\Client132\Modules\Repository\ProjetAchat as RepositoryProjetAchat;
use Skeleton\Router;

class ProjetAchat extends \Twig\Extension\AbstractExtension
{
    /** @var Router $router */
    // @phpstan-ignore-next-line
    private Router $router;

    /** @var ContainerInterface $container */
    // @phpstan-ignore-next-line
    private ContainerInterface $container;

    /** @var \PDO $pdo */
    private \PDO $pdo;
    
    /** @phpstan-ignore-next-line */
    public function __construct(Router $router, ContainerInterface $container, \PDO $pdo)
    {
        $this->router       = $router;
        $this->container    = $container;
        $this->pdo          = $pdo;
    }

    /**
     * @return \Twig\TwigFunction[]
     */
    public function getFunctions()
    {
        return [
            new \Twig\TwigFunction('transmettrePopup', [
                $this,
                'transmettrePopup'
            ], [
                'is_safe' => [
                    'html'
                ]
            ]),
            new \Twig\TwigFunction('gedifPopup', [
                $this,
                'gedifPopup'
            ], [
                'is_safe' => [
                    'html'
                ]
            ]),
            new \Twig\TwigFunction(
                'gedifIcon',
                [ $this, 'geditIcon' ],
                [ 'is_safe' => [ 'html' ] ]
            ),
        ];
    }

    /**
     * @param array<mixed> $array
     * @return string|null
     */
    public function transmettrePopup($array)
    {
        
        $doaEntity = new Doa($this->pdo);
        $doa = $doaEntity->find($array['idProjetAchat']);
        $trad = $this->container->get('translationService');
        
        if ($doa['STATUT'] != 'I') {
            return null;
        }
        
        $hastutilEntity = new Hastutil($this->pdo);
        $hastutil = $hastutilEntity->find($array['DESTINATAIRE']);
        
        $message = Trad::strBuild($trad->t('Êtes-vous sur de vouloir transmettre le projet @1@ à @2@ ?', $this->pdo), $doa['REF'], $hastutil['NOM']);
        
        $html = "<script>
                    $('#formPopupProjetAchat').html('<input type=\"hidden\" name=\"DESTINATAIRE\" value=\"".$array['DESTINATAIRE']."\"><div class=\"display_cell\">$message</div>');
                 </script>";
        
        return $html;
    }

    /**
     * @param array<mixed> $array
     * @return string|null
     */
    public function gedifPopup($array)
    {
        $diliEntity = new RepositoryProjetAchat($this->pdo);
        $chargeDocument = new ChargeDocument();

        $dili = $diliEntity->getUrlGed();

        if (count($dili) >= 1) {
            if ($dili[0]['LIBELLE'] == '' && $dili[0]['ALPHA1']  == '') {
                return null;
            }
        } else {
            return null;
        }
        
        $html = '<li><a class="menuSpecifique" target="_blank" href="'.$dili[0]['ALPHA1'].'" >'.$dili[0]['LIBELLE'].'</a></li>';
        
        return $html;
    }

    /**
     * @return string|null
     */
    public function geditIcon()
    {
        $diliEntity = new RepositoryProjetAchat($this->pdo);

        $dili = $diliEntity->getUrlGed();

        if (count($dili) >= 1) {
            if ($dili[0]['LIBELLE'] == '' && $dili[0]['ALPHA1']  == '') {
                return null;
            }
        } else {
            return null;
        }
        $html = "
        <a id ='gedifIcon' class='white-tooltip' target='_blank' style='opacity: 0.7;top:16px;left:-13px;' href = '".$dili[0]['ALPHA1']. "'title =''>
            <span id='addChamps' class='infos_tooltip top-left'>
            <span class='tooltip-arrow'></span>".$dili[0]['LIBELLE']."</span>
        </a>";
        
        // Style
        $html .= '<style>#btnPlanning{position:relative;right:-86px;}</style>';
        
        return $html;
    }
}
