<?php
namespace Modules\DemandeAchat\Controller;

use Entity\Model\Centre;
use Modules\DemandeAchat\Outil\FormArray;
use Skeleton\Controller\Controller;
use Helpers\Ariane\Ariane;
use Helpers\Ariane\ArianeSub;
use Helpers\Trad\Trad;
use Modules\DemandeAchat\Repository\ListeDemandeAchat;
use Modules\DemandeAchat\Repository\DemandeAchat;

class ListeDemandeAchatController extends Controller
{
    
    public function listeEnteteAction() : string
    {
        $post = $this->request->getParsedBody();
        $typeListe = $this->request->getAttribute('typeListe');
        $trad = $this->getTranslationService();

        if (!$post) {
            $post['tri_NO_DA']='asc';
        }
        
        switch ($typeListe) {
            case 'acommander':
                $titre = $trad->t('Liste des DA à commander / Entête');
                $post['filter_TRAITEE'] = 'F';
                break;
            default:
                $titre = $trad->t('Liste des DA / Entête');
                break;
        }

        $listeDA = new ListeDemandeAchat($this->pdo);
        $liste= $listeDA->listeDemandeAchat($post);

        Ariane::generate(0, $titre, $post, count($liste));
        ArianeSub::init();
        
        $paginator = new \Helpers\Pagination\Paginator();
        $infopagination = $paginator->generate($liste, 0, $post, $this->renderer);

        $trad = $this->getTranslationService();
       
        return $this->renderer->render('@DemandeAchat/Liste/listeEntete', [
            'menuActive' => 'CollecteBesoins',
            'menuLeft' => 'GestionDemandeAchat',
            'infoPagination' => $infopagination,
            
            'listeDA' => $liste,
            'typeListe' => $typeListe,
        ]);
    }
    
    public function detailListeEnteteAction() : string
    {
        $numDA = $this->request->getAttribute('idDA');

        $demandeAchat = new DemandeAchat($this->pdo);
        
        $listeLigneDemandeAchat = $demandeAchat->listeLigneDemandeAchat($numDA);
        $detailDA = $demandeAchat->demandeAchat($numDA);
        
        $trad = $this->getTranslationService();

        $formArray = FormArray::getFormArrays($trad);

        $detailDA['CentreEmet'] = (new Centre($this->pdo))
            ->find($detailDA['DAENT_CENTRE_EMETTEUR']);
        
        return $this->renderer->render('@DemandeAchat/Liste/detailListeEntete', [
            'detailDA' => $detailDA,
            'listeLigneDemandeAchat' => $listeLigneDemandeAchat,
            'id' => $numDA,
            'typeChiffExec' => $formArray['typeChiffExec'],
            'tabTypeMarchePublic' => $formArray['tabTypeMarchePublic'],
            'tabPeriodicite' => $formArray['tabPeriodicite'],
            'tabPeriode' => $formArray['tabPeriode'],
            'tabJoursAppli' => $formArray['tabJoursAppli'],
            'tabTypeDate' => $formArray['tabTypeDate'],
        ]);
    }
    
    public function listeLigneAction() : string
    {
        $post = $this->request->getParsedBody();
        $typeListe = $this->request->getAttribute('typeListe');
        $trad = $this->getTranslationService();
        
        if (!$post) {
            $post['tri_NO_DA']='asc';
        }
        
        switch ($typeListe) {
            case 'acommander':
                $titre = $trad->t('Liste des DA à commander / Poste');
                $post['filter_TRAITEE'] = 'F';
                break;
            default:
                $titre = $trad->t('Liste des DA / Poste');
                break;
        }
        
        $listeDA = new ListeDemandeAchat($this->pdo);
        $liste= $listeDA->listeLigneDemandeAchat($post);

        Ariane::generate(0, $titre, $post, count($liste));
        ArianeSub::init();

        $paginator = new \Helpers\Pagination\Paginator();
        $infopagination = $paginator->generate($liste, 0, $post, $this->renderer);

        $trad = $this->getTranslationService();
        /* **** */
        
        $listeTRAITEE= [
            ''  => $trad->t('Toutes'),
            'T' => $trad->t('Oui'),
            'F' => $trad->t('Non'),
        ];

        return $this->renderer->render('@DemandeAchat/Liste/listeLigne', [
            'menuActive' => 'CollecteBesoins',
            'menuLeft' => 'GestionDemandeAchat',
            'infoPagination' => $infopagination,
            
            'listeTRAITEE' => $listeTRAITEE,
            'listeLigneDA' => $liste,
            'typeListe' => $typeListe,
        ]);
    }
    
    public function detailListeLigneAction() : string
    {
        $numDA = $this->request->getAttribute('idDA');
        $noPO = $this->request->getAttribute('noPO');
        
        $listeDA = new ListeDemandeAchat($this->pdo);
        $demandeAchat = new DemandeAchat($this->pdo);
        
        $detailLigne = $listeDA->detailListeLigneDemandeAchat($numDA, $noPO);
        $detailDA = $demandeAchat->demandeAchat($numDA);
        
        $trad = $this->getTranslationService();

        $formArray = FormArray::getFormArrays($trad);

        return $this->renderer->render('@DemandeAchat/Liste/detailListeLigne', [
            'detailDA' => $detailDA,
            'detailLigne' => $detailLigne,
            'id' => $numDA,
            'noPO' => $noPO,
            'typeChiffExec' => $formArray['typeChiffExec'],
            'tabTypeMarchePublic' => $formArray['tabTypeMarchePublic'],
            'tabPeriodicite' => $formArray['tabPeriodicite'],
            'tabPeriode' => $formArray['tabPeriode'],
            'tabJoursAppli' => $formArray['tabJoursAppli'],
            'tabTypeDate' => $formArray['tabTypeDate'],
        ]);
    }
    
    public function listeLigneCommandeAction() : string
    {
        $post = $this->request->getParsedBody();
        $trad = $this->getTranslationService();

        if (!$post) {
            $post['tri_NO_CDE']='desc';
        }
        
        $listeDA = new ListeDemandeAchat($this->pdo);
        $liste= $listeDA->listeLigneCommande($post);

        Ariane::generate(0, $trad->t('Liste des Commandes / DA / Postes'), $post, count($liste));
        ArianeSub::init();
        
        $paginator = new \Helpers\Pagination\Paginator();
        $infopagination = $paginator->generate($liste, 0, $post, $this->renderer);

        $trad = $this->getTranslationService();
        /* **** */
        
        $listeEtatCommande= [
            ''   => $trad->t('Toutes'),
            'E'  => $trad->t('En cours'),
            'A'  => $trad->t('En Attente'),
            'V1' => $trad->t('A envoyer'),
            'V2' => $trad->t('En signature'),
            'V3' => $trad->t('Refusées'),
            'L'  => $trad->t('Livraison partielle'),
            'S'  => $trad->t('Soldées physiquement'),
            'Z'  => $trad->t('Annulées'),
        ];
        
        $listeEtatTab = [
            'E' => $trad->t('En cours'),
            'A' => $trad->t('En Attente'),
            'V' => $trad->t('Validée'),
            'S' => $trad->t('Soldées physiquement'),
            'Z' => $trad->t('Annulées'),
        ];
        
        $listeEtatFinancier= [
            ''  => $trad->t('Toutes'),
            'T' => $trad->t('Non soldées financièrement'),
            'F' => $trad->t('Soldées financièrement'),
        ];

        return $this->renderer->render('@DemandeAchat/Liste/listeLigneCommande', [
            'menuActive' => 'CollecteBesoins',
            'menuLeft' => 'GestionDemandeAchat',
            'infoPagination' => $infopagination,
            
            'listeEtatPhysique' => $listeEtatCommande,
            'listeEtatFinancier' => $listeEtatFinancier,
            'listeEtatTab' => $listeEtatTab,
            'listeCde' => $liste,
        ]);
    }
    
    public function detailListeLigneCommandeAction() : string
    {
        $numCde = $this->request->getAttribute('idCde');
        $noPO = $this->request->getAttribute('noPO');
        
        $listeDA = new ListeDemandeAchat($this->pdo);

        $detailLigne = $listeDA->detailListeLigneCommande($numCde, $noPO);
        $detailCommande = $listeDA->detailListeCommande($numCde);
        
        $trad = $this->getTranslationService();
        /* **** */
        
        $listeEtatCommande= [
            ''   => $trad->t('Toutes'),
            'E'  => $trad->t('En cours'),
            'A'  => $trad->t('En Attente'),
            'V1' => $trad->t('A envoyer'),
            'V2' => $trad->t('En signature'),
            'V3' => $trad->t('Refusées'),
            'L'  => $trad->t('Livraison partielle'),
            'S'  => $trad->t('Soldées physiquement'),
            'Z'  => $trad->t('Annulées'),
        ];
        
        $listeEtatTab = [
            'E' => $trad->t('En cours'),
            'A' => $trad->t('En Attente'),
            'V' => $trad->t('Validée'),
            'S' => $trad->t('Soldées physiquement'),
            'Z' => $trad->t('Annulées'),
        ];
        
        $listeEtatFinancier= [
            ''  => $trad->t('Toutes'),
            'T' => $trad->t('Non soldées financièrement'),
            'F' => $trad->t('Soldées financièrement'),
        ];
        
        return $this->renderer->render('@DemandeAchat/Liste/detailListeLigneCommande', [
            'detailCommande' => $detailCommande,
            'detailLigne' => $detailLigne,
            'id' => $numCde,
            'noPO' => $noPO,
            'listeEtatPhysique' => $listeEtatCommande,
            'listeEtatFinancier' => $listeEtatFinancier,
            'listeEtatTab' => $listeEtatTab,
        ]);
    }
    
    public function listeEvenementDAAction() : string
    {
        $post = $this->request->getParsedBody();
        $trad = $this->getTranslationService();

        if (!$post) {
            $post['tri_NO_DA']  = 'desc';
        }

        $listeDA = new ListeDemandeAchat($this->pdo);
        $liste= $listeDA->listeEventDA($post);
        
        Ariane::generate(0, $trad->t('Liste d\'évènements de DA'), $post, count($liste));
        ArianeSub::init();
        
        $paginator = new \Helpers\Pagination\Paginator();
        $infopagination = $paginator->generate($liste, 0, $post, $this->renderer);

        return $this->renderer->render('@DemandeAchat/Liste/listeEventDA', [
            'menuActive' => 'CollecteBesoins',
            'menuLeft' => 'GestionDemandeAchat',
            'post'             => $post,
            'listeDA'         => $liste,
            'infoPagination'   => $infopagination,
        ]);
    }
    
    public function detailListeEvenementDAAction() : string
    {
        $post = $this->request->getParsedBody();
        $numDA = $this->request->getAttribute('idDA');
        
        $listesFacture = new ListeDemandeAchat($this->pdo);
        $lignes = $listesFacture->detailListeEventDA($numDA);
        
        $nombreEnregistrement = count($lignes);
        
        if ($nombreEnregistrement>5 && !isset($post['affichage'])) {
            $post['affichage'] = 'notAll';
        }
        
        /* **** */
        
        return $this->renderer->render('@DemandeAchat/Liste/detailListeEventDA', [
            'lignes' => $lignes,
            'nombreEnregistrement' => $nombreEnregistrement,
            'idDA' => $numDA,
            'affichage' => $post['affichage']??'',
            
        ]);
    }
    
    public function listeEvenementAction() : string
    {
        $post = $this->request->getParsedBody();
        
        if (!$post) {
            $post['tri_NO_DA']  = 'asc';
            $post['tri_NUM_ORD'] = 'asc';
        }

        $listeDA = new ListeDemandeAchat($this->pdo);
        $liste= $listeDA->listeEvent($post);
        $trad = $this->getTranslationService();
        
        Ariane::generate(0, $trad->t('Liste globale d\'évènements des DA'), $post, count($liste));
        ArianeSub::init();
        
        $paginator = new \Helpers\Pagination\Paginator();
        $infopagination = $paginator->generate($liste, 0, $post, $this->renderer);

        /* **** */
        
        return $this->renderer->render('@DemandeAchat/Liste/listeEvent', [
            'menuActive' => 'CollecteBesoins',
            'menuLeft' => 'GestionDemandeAchat',
            'infoPagination' => $infopagination,
            'listeEvent' => $liste,
            
        ]);
    }
}
