<?php
namespace Modules\Facture\Controller;

use Helpers\Ariane\Ariane;
use Helpers\Outil\Roles;
use Modules\Facture\Repository\GestionFacture;
use Skeleton\Controller\Controller;
use Modules\GlobalApplication\MenuContextuel\DocumentMenuContextuel;

class DocumentsRattachesController extends Controller
{

  
    /** Page document rattachés **/
    /**
     * @return \GuzzleHttp\Psr7\Response|string
     */
    public function docRattacheAction()
    {
        $post = $this->request->getParsedBody();
        $post['NO_DA'] = 'FAC_ENT';
        // ID à mettre sur 9 digits
        $idTmp = $this->request->getAttribute('numFact');
        $gestionFacture = new GestionFacture($this->pdo);
        if (!$gestionFacture->isExist($idTmp)) {
            return $this->redirect('Index.Index.error');
        }
        $id = str_pad($idTmp, 9, '0', STR_PAD_LEFT);
        
        /* Liste des documents attaché */
        $docAtt = new \Modules\GlobalApplication\Repository\DocumentsRattaches($this->pdo);
        $docAtt = $docAtt->detail($id, $post);
        
        $paginator = new \Helpers\Pagination\Paginator();
        $infopagination = $paginator->generate($docAtt, 10, $post, $this->renderer);
        
        $trad = $this->getTranslationService();

        \Helpers\Ariane\Ariane::generate(2, $trad->t('Documents rattachés'));
        Ariane::setFocus($id);
        
        $menuContextuel = new DocumentMenuContextuel($this->pdo, $this->router, $this->getTranslationService());
        foreach ($docAtt as &$doc) {
            $doc['menuContextuel'] = $menuContextuel->generate($doc, $post['NO_DA']);
        }
        
        return $this->renderer->render('@GlobalApplication/documents-rattaches/documents-rattaches', [
            'id' => $id,
            'NO_DA' => $post['NO_DA'],
            'docAtts' => $docAtt,
            'date' => date("Ymd"),
            'post' => $post,
            'droitModif' => Roles::droitsUtilisateur($this->pdo, 'HA_DOC_RAT'),
            'doc' => true,
            'infoPagination' => $infopagination,
            'pathPage' => 'Facture.DocumentsRattaches.docRattache',
            'pathRetour' => $this->router->generateUri('Facture.GestionFacture.index'),
            'titre' => $trad->t('Documents rattachés de la facture').' '.$idTmp,
            'droitCreation' => Roles::droitsUtilisateur($this->pdo, 'HA_DOC_RAT'),
            'NUM_FACT_DEB' => $idTmp
        ]);
    }
}
