<?php
namespace Modules\Commande\Controller;

use Entity\Model\CaPlCt;
use Entity\Model\HCaPlCt;
use Modules\GlobalApplication\Repository\LicenceOxalys;
use Skeleton\Controller\Controller;
use Helpers\Ariane\Ariane;
use Helpers\Trad\Trad;
use Helpers\Ariane\ArianeSub;
use Modules\Commande\Repository\LigneCommande;
use Modules\Commande\Repository\Commande;
use Helpers\ChampsSupplementaire\ChampSupp;
use Helpers\Outil\Roles;
use Modules\Commande\MenuContextuel\GestionMenuContextuel;
use Modules\Commande\Repository\Avenant;

class ArticleCommandeController extends Controller
{
    public function articleAction(
        LicenceOxalys $licenceOxalys
    ): string {
        $post = $this->request->getParsedBody();
        $numCde = $this->request->getAttribute('idCommande');
        $avenant = $this->request->getAttribute('idAvenant');
        
        $modeHisto = false;
        $posAriane = 1;
        if ($avenant || $avenant === '0') {
            $modeHisto = true;
            $posAriane = 2;
        }

        if ($modeHisto) {
            $avenantRepo = new Avenant($this->pdo);
            $listeLigne = $avenantRepo->getLigneAvenant($numCde, $avenant);
            $commande = $avenantRepo->getEnteteAvenant($numCde, $avenant);
        } else {
            $ligneCommande = new LigneCommande($this->pdo);
            $listeLigne = $ligneCommande->getLigneCommande($numCde, $post);
    
            $commande = new Commande($this->pdo);
            $commande = $commande->commandeEntete($numCde);

            $menuContextuel = new GestionMenuContextuel($this->pdo, $this->router, $this->getTranslationService(), []);
            $commande['menuContextuel'] = $menuContextuel->lightGenerate($commande);
        }

        $droit = [
            'HA_CA_VIPX' => Roles::droitsUtilisateur($this->pdo, 'HA_CA_VIPX', $_SESSION["Session_Login"]),
        ];
        
        $trad = $this->getTranslationService();
        Ariane::generate($posAriane, $trad->t('Détail commande'), $post);
        ariane::setFocus($numCde);
        ArianeSub::init();
        ArianeSub::generate(0, 0, $trad->t('Articles commande'), $this->router->generateUri('Commande.Commande.article', ['idCommande' => $numCde, 'idAvenant' => $avenant]), 1);
        ArianeSub::generate(0, 1, $trad->t('Informations commande'), $this->router->generateUri('Commande.Commande.commande', ['idCommande' => $numCde, 'idAvenant' => $avenant]), 0);
        if (!$modeHisto) {
            ArianeSub::generate(0, 2, $trad->t('Documents rattachés'), $this->router->generateUri('GlobalApplication.DocumentRattaches.document', ['MODULE' => 'Commande', 'id' => $numCde]), 0, $post);
        }
        ArianeSub::generate(0, 3, $trad->t('Pièces-jointes'), $this->router->generateUri('Commande.PieceJointe.index', ['idCommande' => $numCde, 'idAvenant' => $avenant]), 0);
        ArianeSub::generate(0, 4, $trad->t('Clauses'), $this->router->generateUri('Commande.Clause.index', ['idCommande' => $numCde, 'idAvenant' => $avenant]));

        if (!$modeHisto) {
            ArianeSub::generate(0, 5, $trad->t('Interactions'), $this->router->generateUri('Commande.Interaction.interactions', ['idCommande' => $numCde]));
        }
        if ($licenceOxalys->termPaieActive()) {
            ArianeSub::generate(0, 6, $trad->t('Termes de paiement'), $this->router->generateUri('Commande.TermeDePaiementCommande.index', ['idCommande' => $numCde, 'MODULE' => 'Commande']));
        }
        $codEnregCS = ChampSupp::autoCompletCodeEnr($numCde, '', 'CA_ENTET');



        return $this->renderer->render('@Commande/Commande/article', [
            'menuActive' => 'CollecteBesoins',
            'menuLeft' => 'Commande.GestionCommande.index',
            'numCde' => $numCde,
            'listeLigne' => $listeLigne,
            'commande' => $commande,
            'droit' => $droit,
            'modeHisto' => $modeHisto,
            'idAvenant' => $avenant,
            'post' => $post
            
        ]);
    }
    
    public function detailArticleAction() : string
    {
        $ogirine = $this->request->getAttribute('origine');
        $numCde = $this->request->getAttribute('idCommande');
        $idLigne = $this->request->getAttribute('idLigne');
        $avenant = $this->request->getAttribute('idAvenant');

        $modeHisto = false;
        if ($avenant || $avenant === '0') {
            $modeHisto = true;
        }

        if ($modeHisto) {
            $avenantRepo = new Avenant($this->pdo);
            $detailLigne = $avenantRepo->getDetailLigneAvenant($numCde, $idLigne, $avenant);

            $codEnregCS = ChampSupp::autoCompletCodeEnr($numCde);
            $codEnregCS .= $avenant>10? $avenant:' '.$avenant;
            $idCS = $codEnregCS.str_pad($idLigne, 3, '0', STR_PAD_LEFT);

            $hcaPlCt = new HCaPlCt($this->pdo);
            $planning = $hcaPlCt->findAll(['NUM_CDE' => $numCde, 'NUM_POSTE' => $idLigne, 'AVENANT' => $avenant==='0'? '':$avenant]);
        } else {
            $ligneCommande = new LigneCommande($this->pdo);
            $detailLigne = $ligneCommande->getDetailLigne($numCde, $idLigne);

            $idCS = ChampSupp::autoCompletCodeEnr($numCde, $idLigne);

            $caPlCt = new CaPlCt($this->pdo);
            $planning =  $caPlCt->findAll(['NO_CA' => $numCde, 'NO_PO_CA' => $idLigne]);
        }

        if (ChampSupp::listeIdCS('CA_LIGNE', $this->pdo)) {
            $CSPresent = true;
        }

        $trad = $this->getTranslationService();
        $tabPeriodicite = [
            'M' => $trad->t('Mensuelle'),
            'T' => $trad->t('Trimestrielle'),
            'S' => $trad->t('Semestrielle'),
            'A' => $trad->t('Annuelle'),
        ];
        
        $tabPeriode = [
            'D' => $trad->t('Début de période'),
            'F' => $trad->t('Fin de période'),
        ];
        
        $tabTypeDate = [
            'D' => $trad->t('Date'),
            'S' => $trad->t('Semaine'),
            'M' => $trad->t('Mois'),
        ];

        $idcsAvenant = '';
        if ($avenant) {
            $idcsAvenant = ChampSupp::autoCompletCodeEnr($numCde, '', 'CA_ENTET');
            ;
            $noAvt = (int)$avenant;

            $idcsAvenant .= $noAvt>=10? $noAvt:' '.$noAvt;

            $idcsAvenant .=  ($codEnregCS ?? '').str_pad($idLigne, 3, '0', STR_PAD_LEFT);
        } elseif ($avenant==='0') {
            $idcsAvenant = ChampSupp::autoCompletCodeEnr($numCde, '', 'CA_ENTET');
        }
        
        return $this->renderer->render('@Commande/Commande/detailArticle', [
            'ligne' => $detailLigne,
            'tabPeriodicite' => $tabPeriodicite,
            'tabPeriode' => $tabPeriode,
            'tabTypeDate' => $tabTypeDate,
            'idForCS' => $idCS,
            'csPresent' => $CSPresent??false,
            'idCommande' => $numCde,
            'idLigne' => $idLigne,
            'planning' => $planning,
            'modeHisto' => $modeHisto,
            'idAvenant' => $avenant,
            'idcsAvenant' => $idcsAvenant,
        ]);
    }
}
