<?php
namespace Modules\GlobalApplication\Controller;

use ErrorException;
use Helpers\Outil\Sql;
use Helpers\PhpOffice\PhpOfficeAdapter;
use Skeleton\Controller\Controller;
use Entity\Model\Docatt;
use Helpers\Fichier\Fichier;
use Modules\GlobalApplication\Repository\DocumentsRattaches;
use Helpers\Trad\Trad;
use GuzzleHttp\Psr7\Response;
use Modules\ProjetAchat\Repository\ProjetAchat;
use Helpers\Session\ParseBase1;

class AjaxDocRattachesController extends Controller
{

    
    /** Suppresion d'un document **/
    public function supprimerAction()
    {
        
        $id = $this->request->getAttribute('id');
        $NO_DA = $this->request->getAttribute('NO_DA');
        $NoOrdre = $this->request->getAttribute('NoOrdre');
       
        $trad = $this->getTranslationService();
        
        $value=[
            "Langue" => $_SESSION['Session_Langue'],
            "refObjet" => $id,
            "numOrdre" => $NoOrdre,
            "id" => $NO_DA,
        ];
        
        $xmlResult = new \Modules\GlobalApplication\WebService\DocRattaches();
        $xmlResult= $xmlResult->SuppresionDocAtt($value);
        
        $reponseDocRat = new \SimpleXMLElement($xmlResult->responseData);
        
        if ($reponseDocRat->Statut=="O") {
            $message =  $trad->t('Votre document a été supprimé.');
            
            if ($NO_DA=='DOA') {
                $projetAchatRepo = new ProjetAchat($this->pdo);
                $infoDoc = $projetAchatRepo->docExistInData((int) $id, $NoOrdre);
                
                if ($infoDoc) {
                    $projetAchatRepo->deleteFileData((int) $id, $infoDoc['ID_DONNEE'], $infoDoc['ID_DATA'], $infoDoc['ID_RECORD']);
                }
                
                $doaEntity = new \Entity\Model\Doa($this->pdo);
                $doa = $doaEntity->find((int) $id);
                
                $path = $this->router->generateUri('ProjetAchat.ModeleProjet.details', [
                    'typeObjet' => (Sql::toBool($doa['MODELE']) ? 'M':'D'),
                    'idObjet' => $doa['ID']
                ]);
                $post =  ['onglet' => 'document'];
                
                return new Response(200, [
                    
                ], json_encode(['path' => $path, 'statut' => 'O', 'post' => $post]));
            }
        } else {
            $message =  $reponseDocRat->MsgErreur;
        }
            
        return $this->renderer->render('@popup-erreur/confirmationGestion', [
            'message' => $message,
            'iconePopup' => 'information',
        ]);
    }
    
    
    /** retourne le fichier **/
    public function downloadFichierAction() : string
    {
        
        $id = $this->request->getAttribute('id');
        $ordre = $this->request->getAttribute('ordre');
        $NO_DA = $this->request->getAttribute('NO_DA');
        
        $docAtt = new Docatt($this->pdo);
        $docAtt = $docAtt->find(["COD_ENR" => $id, "NO_ORDRE" => $ordre, "NO_DA" => $NO_DA]);
        
        if ($docAtt['NOM_ORI'] == '') {
            $tblNomFic = explode('\\', $docAtt['NOM_FIC']);
            $nomfichier = end($tblNomFic);
        } else {
            $nomfichier = $docAtt['NOM_ORI'];
        }
        
        return Fichier::UploadClient((new ParseBase1())->getRep()->pj.$docAtt['NOM_FIC'], $nomfichier);
    }


    /** retourne le fichier **/
    public function getSrcDocIframeAction() : Response|string
    {
        $id = $this->request->getAttribute('id');
        $ordre = $this->request->getAttribute('ordre');
        $NO_DA = $this->request->getAttribute('NO_DA');
        
        $docAtt = new Docatt($this->pdo);
        $docAtt = $docAtt->find(["COD_ENR" => $id, "NO_ORDRE" => $ordre, "NO_DA" => $NO_DA]);

        try {
            return PhpOfficeAdapter::fileToHtmlForIFrameSrcDoc((new ParseBase1())->getRep()->pj . $docAtt['NOM_FIC']);
        } catch (ErrorException) {
            return new Response(500);
        }
    }
}
