<?php

namespace Modules\Facture\Controller;

use Helpers\Ariane\Ariane;
use Helpers\Fichier\Fichier;
use Skeleton\Controller\Controller;
use Helpers\Session\ParseBase1;

class FacturesPortailController extends Controller
{
    public function suiviFacturesPortailAction() : string
    {
        $post = $this->request->getParsedBody();
        
        $FacturesPortail= new \Modules\Facture\Repository\FacturesPortail($this->pdo);
        $factures = $FacturesPortail->listeFacturesPortail($post);

        $infoPagination = \Helpers\Pagination\PaginatorGestion::getInfoPagination();
        
        Ariane::generate(0, 'Factures issues du Portail');

        return $this->renderer->render('@factures-portail/suivi-factures-portail', [
            'menuActive'       => 'Facture',
            'post'             => $post,
            'factures'         => $factures,
            'infoPagination'   => $infoPagination,
            
        ]);
    }

    public function detailFactureAction() : string
    {
        $post = $this->request->getParsedBody();
        
        $params = array_merge($post, ['NO_DA' => 'MAIL_SUIVI', 'TITRE' => 'Fichier message']);
        
        $Docatt = new \Entity\Model\Docatt($this->pdo);
        $ligne = $Docatt->find($params, ['NOM_FIC']);
        
        $filename = (new ParseBase1())->getRep()->pj.$ligne['NOM_FIC'];
        
        if (is_file($filename)) {
            $ligne['contenuMail'] = \Helpers\Outil\StringHelper::utf8Encode(Fichier::fileGetContents($filename));
        }

        return $this->renderer->render('@factures-portail/detail-facture', [
            'post'  => $post,
            'ligne' => $ligne,
            
        ]);
    }
}
