<?php

namespace Modules\Additional\Specifique\Client131\Html;

use Entity\Model\CsChps;
use Entity\Model\Dilispec;
use Helpers\Outil\Sql;
use Helpers\Trad\ITrad;
use Helpers\User\User;
use Modules\Additional\Specifique\Client131\Modules\Suivi\Repository\SuiviSpecifiqueRepository;
use Modules\Additional\Specifique\Client37\Modules\Signature\Service\WorkflowSpecifique;
use Modules\Panier\Repository\PanierEntete;
use Psr\Container\ContainerInterface;
use TwigExtension\FormExtension;
use Skeleton\Router;

class HtmlExtension extends \Twig\Extension\AbstractExtension
{
    /** @phpstan-ignore-next-line */
    private Router $router;
    /** @phpstan-ignore-next-line */
    private ContainerInterface $contenaire;
    /** @phpstan-ignore-next-line */
    private \PDO $pdo;
    private ITrad $trad;
    
    public function __construct(Router $router, ContainerInterface $contenaire, \PDO $pdo)
    {
        $this->router = $router;
        $this->pdo = $pdo;
        $this->contenaire = $contenaire;
        $this->trad = $contenaire->get(ITrad::class);
    }

    public function getFunctions()
    {
        return [
            /** @phpstan-ignore-next-line */
            new \Twig\TwigFunction('specifiqueSignatureBap', [
                $this,
                'specifiqueSignatureBap'
            ], [
                'is_safe' => [
                    'html'
                ]
            ])
        ];
    }
    
    /**
     * @param array<mixed> $array
     * @return string
     */
    public function specifiqueSignature($array):string
    {
        $user = new User();
        $user->initObjectSource($_SESSION);
        $typeDoc    = $array['typeDoc'];
        $idDoc      = $array['idDoc'];
        $signataire = $array['signataire']??$user->getCodeUser();
        $context    = $array['context'];

        if ($typeDoc == 'CDE') {
            $idTable = 'commande';
            $cssTop = '185px';
            $listeLignePM = (new SuiviSpecifiqueRepository($this->pdo))->getListeLigneCde($idDoc, $signataire, 'projetManager');
            $listeLigneHO = (new SuiviSpecifiqueRepository($this->pdo))->getListeLigneCde($idDoc, $signataire, 'HeadOperation');
            $listeLigneRF = (new SuiviSpecifiqueRepository($this->pdo))->getListeLigneCde($idDoc, $signataire, 'responsableFamille');
        } elseif ($typeDoc == 'DA') {
            $idTable = 'Demande';
            $listeLignePM = (new SuiviSpecifiqueRepository($this->pdo))->getListeLigneDA($idDoc, $signataire, 'projetManager');
            $listeLigneHO = (new SuiviSpecifiqueRepository($this->pdo))->getListeLigneDA($idDoc, $signataire, 'HeadOperation');
            $listeLigneRF = (new SuiviSpecifiqueRepository($this->pdo))->getListeLigneDA($idDoc, $signataire, 'responsableFamille');
            $cssTop = '185px';
        } elseif ($typeDoc == 'BAP') {
            $idTable = 'Fac';
            $listeLignePM = (new SuiviSpecifiqueRepository($this->pdo))->getListeLigneFac($idDoc, $signataire, 'projetManager');
            $listeLigneHO = (new SuiviSpecifiqueRepository($this->pdo))->getListeLigneFac($idDoc, $signataire, 'HeadOperation');
            $listeLigneRF = (new SuiviSpecifiqueRepository($this->pdo))->getListeLigneFac($idDoc, $signataire, 'responsableFamille');
            $cssTop = '2px';
        } else {
            return '';
        }

        if ($context != 'signature') {
            return '';
        }

        if (!$listeLignePM && !$listeLigneHO && !$listeLigneRF) {
            return '';
        }
        
        if ($listeLignePM) {
            $projetManager_totalHT  = number_format((float)$listeLignePM[0]['totalHT'], 2, '.', ' ');
            $projetManager_totalTTC = number_format((float)$listeLignePM[0]['totalTTC'], 2, '.', ' ');
        }

        if ($listeLigneHO) {
            $HeadOperation_totalHT  = number_format((float)$listeLigneHO[0]['totalHT'], 2, '.', ' ');
            $HeadOperation_totalTTC = number_format((float)$listeLigneHO[0]['totalTTC'], 2, '.', ' ');
        }

        if ($listeLigneRF) {
            $responsableFamille_totalHT  = number_format((float)$listeLigneRF[0]['totalHT'], 2, '.', ' ');
            $responsableFamille_totalTTC = number_format((float)$listeLigneRF[0]['totalTTC'], 2, '.', ' ');
        }

        $CSprojetManager = trim(Sql::dilispec($this->pdo, 'PARNCS0131', 'NCS_SECTIMPU_PROJET_MANAGER'));
        $CSHeadOperation = trim(Sql::dilispec($this->pdo, 'PARNCS0131', 'NCS_SECTIMPU_HEAD_OPERATION'));
        $CSresponsableFamille = trim(Sql::dilispec($this->pdo, 'PARNCS0131', 'NCS_DIRECTIO_LOB_MANAGER'));

        $projetManager = (new CsChps($this->pdo))->find([
            'COD_TAB' => 'SECTIMPU',
            'NUM_FIX' => $CSprojetManager? $CSprojetManager:'1'
        ]);

        $headOperation = (new CsChps($this->pdo))->find([
            'COD_TAB' => 'SECTIMPU',
            'NUM_FIX' => $CSHeadOperation? $CSHeadOperation:'4'
        ]);

        $lobFamilly = (new CsChps($this->pdo))->find([
            'COD_TAB' => 'DIRECTIO',
            'NUM_FIX' => $CSresponsableFamille? $CSresponsableFamille:'2'
        ]);

        $devise     = $listeLignePM[0]['DEVISE']??'';

        $html = <<<HTML
            <div class="sigPanierSpecifique">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>{$this->trad->t('Rôles')}</th>
                            <th>{$this->trad->t('HT')}</th>
                            <th>{$this->trad->t('TTC')}</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody> 
            HTML;

        if ($listeLignePM) {
            $html .= <<<HTML
                        <tr>
                            <td>{$projetManager['NOM']}</td>
                            <td class="montant">$projetManager_totalHT $devise</td>
                            <td class="montant">$projetManager_totalTTC $devise</td>
                            <td>
                                <span class="visualiser_v2 showPM"></span>
                            </td>
                        </tr>
                HTML;
        }

        if ($listeLigneHO) {
            $html .= <<<HTML
                        <tr>
                            <td>{$headOperation['NOM']}</td>
                            <td class="montant">$HeadOperation_totalHT $devise</td>
                            <td class="montant">$HeadOperation_totalTTC $devise</td>
                            <td>
                                <span class="visualiser_v2 showHO"></span>
                            </td>
                        </tr>
                HTML;
        }

        if ($listeLigneRF) {
            $html .= <<<HTML
                        <tr>
                            <td>{$lobFamilly['NOM']}</td>
                            <td class="montant">$responsableFamille_totalHT $devise</td>
                            <td class="montant">$responsableFamille_totalTTC $devise</td>
                            <td>
                                <span class="visualiser_v2 showRF"></span>
                            </td>
                        </tr>
                HTML;
        }
                
        $html .= <<<HTML
                    </tbody>
                </table>
            </div>
        HTML;

        $html .= '<style>';

        $html .= <<<CSS
                .sigPanierSpecifique {
                    position: absolute;
                    left: 52%;
                    top: {$cssTop};
                }
                .sigPanierSpecifique > div {
                    display: flex;
                    flex-direction: row;
                    gap: 25px;
                }
                .tr_glow_SPEC {
                    background-color: #45a3f742 !important;
                }
        CSS;
        $html .= '</style>';

        $html .= '<script>';
        foreach ($listeLignePM as $ligne) {
            $idLigne = $ligne['NO_PO'];
            $html .= <<<JS
                $("#ligne_{$idTable}{$idLigne}{$idDoc}").addClass('tr_glow_PM');
            JS;
        }

        foreach ($listeLigneHO as $ligne) {
            $idLigne = $ligne['NO_PO'];
            $html .= <<<JS
                $("#ligne_{$idTable}{$idLigne}{$idDoc}").addClass('tr_glow_HO');
            JS;
        }

        foreach ($listeLigneRF as $ligne) {
            $idLigne = $ligne['NO_PO'];
            $html .= <<<JS
                $("#ligne_{$idTable}{$idLigne}{$idDoc}").addClass('tr_glow_RF');
            JS;
        }

        $html .= <<<JS
                var tr_glow_RF = false;
                var tr_glow_HO = false
                var tr_glow_PM = false;

                $('.showPM').click(function(){
                    $('.tr_glow_SPEC').removeClass('tr_glow_SPEC');
                    $('.tr_glow_PM').addClass('tr_glow_SPEC');

                    if (tr_glow_RF) {
                        $('.tr_glow_SPEC').removeClass('tr_glow_SPEC');
                        tr_glow_RF = false;
                    } else {
                        tr_glow_RF = true;
                    }

                    tr_glow_HO = false;
                    tr_glow_PM = false;
                });

                $('.showHO').click(function(){
                    $('.tr_glow_SPEC').removeClass('tr_glow_SPEC');
                    $('.tr_glow_HO').addClass('tr_glow_SPEC');

                    if (tr_glow_HO) {
                        $('.tr_glow_SPEC').removeClass('tr_glow_SPEC');
                        tr_glow_HO = false;
                    } else {
                        tr_glow_HO = true;
                    }

                    tr_glow_RF = false;
                    tr_glow_PM = false;
                });
                
                $('.showRF').click(function(){
                    $('.tr_glow_SPEC').removeClass('tr_glow_SPEC');
                    $('.tr_glow_RF').addClass('tr_glow_SPEC');

                    if (tr_glow_PM) {
                        $('.tr_glow_SPEC').removeClass('tr_glow_SPEC');
                        tr_glow_PM = false;
                    } else {
                        tr_glow_PM = true;
                    }

                    tr_glow_RF = false;
                    tr_glow_HO = false;
                    
                });
            JS;

        $html .= '</script>';

        return $html;
    }
}
