<?php

namespace Modules\Additional\Specifique\Client77\Html;

use Entity\Model\Dilispec;
use Entity\Model\OxaUserPanier;
use Entity\Model\OxaUserPanierFour;
use Helpers\Trad\ITrad;
use Psr\Container\ContainerInterface;
use Skeleton\Router;

// use Psr\Container\ContainerInterface;


class HtmlExtension extends \Twig\Extension\AbstractExtension
{
    private Router $router;
    private \PDO $pdo;

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

    public function __construct(Router $router, ContainerInterface $container, \PDO $pdo)
    {
        $this->router       = $router;
        $this->container    = $container;
        $this->pdo          = $pdo;
    }

    public function getFunctions()
    {
        return [
            new \Twig\TwigFunction('demandeDelais', [
                $this,
                'demandeDelais'
            ], [
                'is_safe' => [
                    'html'
                ]
            ]),
            new \Twig\TwigFunction('btnDemandeDelais', [
                $this,
                'btnDemandeDelais'
            ], [
                'is_safe' => [
                    'html'
                ]
            ])
        ];
    }

    /**
     * @param array<mixed> $array
     * @return string
     */
    public function btnDemandeDelais($array)
    {
        $infoPanier = (new OxaUserPanier($this->pdo))->find([
            'id_user' => $_SESSION['id_user'],
            'id_panier' => $_SESSION['id_panier'],
        ]);

        $infoFrs = (new OxaUserPanierFour($this->pdo))->find([
            'id_user' => $_SESSION['id_user'],
            'id_panier' => $_SESSION['id_panier'],
        ], ['COUNT(DISTINCT COD_FOURN) COUNT_FRS'])['COUNT_FRS'];

        if ($infoFrs == 1 && $infoPanier['CHIF_EXEC']  == 'C') {
            return '<button class="principal" onClick="ajaxPopupV2(\''.$this->router->generateUri('Specifique77.Panier.demandeDelais').'\');">'.$array['Demande de délais'].'</button>';
        }
        
        return '';
    }

    /**
     * @param array<mixed> $array
     * @return string
     */
    public function demandeDelais($array)
    {
        if ($array['panier']['flg_panier_modele'] == '0') {
            $idPanier = $array['panier']['id_panier'];
            $specifique = (new Dilispec($this->pdo))->find([
                'TYPEDICO'  =>  'SPACOXAPDL',
                'CODEDICO'  =>  'DEM_DEL|'.$idPanier,
            ]);

            if ($specifique) {
                return '<a target="_blank" href="'.$this->router->generateUri('Specifique77.Panier.loadDemandeDelais', ['idPanier' => $idPanier]).'">
                            <span class="pdf"></span>
                        </a>';
            }
        }

        return '';
    }
}
