<?php

namespace Modules\Facture\Controller;

use Helpers\Ariane\Ariane;
use Skeleton\Controller\Controller;

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 = $_SESSION['chemin_pj'].$ligne['NOM_FIC'];
        
        if (is_file($filename)) {
            $ligne['contenuMail'] = utf8_encode(file_get_contents($filename));
        }

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