<?php

namespace Modules\Consultation\Controller;

use Skeleton\Controller\Controller;
use Helpers\Trad\Trad;
use Helpers\Ariane\ArianeSub;
use Modules\Consultation\Repository\GestionConsultation;
use Modules\Consultation\Repository\AoEvent;
use GuzzleHttp\Psr7\Response;
use Helpers\Validator\Validator;
use Entity\Model\AoClaus;
use Helpers\Outil\Sql;

class ClauseController extends Controller
{
    public function creatEditAction() : string
    {
        $idAO = $this->request->getAttribute('idAO');
        $noOrdre = $this->request->getAttribute('noOrdre');
        $post = $this->request->getParsedBody();
        
        $gestionConsu = new GestionConsultation($this->pdo);
        $consultation = $gestionConsu->getInfoAo($idAO);
        
        $aoEvent = new AoEvent($this->pdo);
        $AOEventCreat = $aoEvent->getNameCreate($idAO);
        
        $trad = $this->getTranslationService();
        
        if ($noOrdre || $noOrdre==='0') {
            ArianeSub::generate(1, 0, $trad->t('Modification clause'), $this->router->generateUri('Consultation.Clause.creatEdit', ['idAO' => $idAO, 'noOrdre' => $noOrdre]), 1);
            
            $AoClausEntity = new AoClaus($this->pdo);
            $Clause = $AoClausEntity->find([
                'NO_AO' => $idAO,
                'NO_ORDRE' => $noOrdre,
            ]);
            
            $post['CLAUSE'] = sql::getMemo($this->pdo, $Clause['NM_PARAG']);
        } else {
            ArianeSub::generate(1, 0, $trad->t('Création clause'), $this->router->generateUri('Consultation.Clause.creatEdit', ['idAO' => $idAO]), 1);
        }
        
        
        return $this->renderer->render('@Consultation/Clause/creatEdit', [
            'menuActive' => $_SESSION['typeMenu'] == 'P2P'? 'eproc':'Consultations',
            'menuLeft'          => 'gestionAO',
            'idAO'              => $idAO,
            'noOrdre'           => $noOrdre,
            'post'              => $post,
            'consultation'      => $consultation,
            'AOEventCreat'      => $AOEventCreat,
        ]);
    }
    
    public function validateAction()
    {
        $idAO = $this->request->getAttribute('idAO');
        $noOrdre = $this->request->getAttribute('noOrdre');
        $post = $this->request->getParsedBody();
        
        $trad = $this->getTranslationService();
        
        $validator = $this->createValidator();
        
        $validator->isStringAvecEspace($trad->t('Clause'), $post['CLAUSE'], 1, 3000);
        
        if ($validator->isValid()) {
            $path = $this->router->generateUri('Consultation.Consultation.details', ['idAO' => $idAO, 'onglet' => 'Clause']);
            
            $AoClausEntity = new AoClaus($this->pdo);

            if ($noOrdre) {
                $Clause = $AoClausEntity->find([
                    'NO_AO' => $idAO,
                    'NO_ORDRE' => $noOrdre,
                ]);
                Sql::updateMemo($this->pdo, $Clause['NM_PARAG'], $post['CLAUSE']);
            } else {
                $idMemos = Sql::updateMemo($this->pdo, '', $post['CLAUSE']);
                $gestionConsu = new GestionConsultation($this->pdo);
                $noOrdre = (int) $gestionConsu->getMaxOrderClause($idAO)['MAX_ORDER'];
               
                $AoClausEntity->insert([
                    'NO_AO' => $idAO,
                    'NO_ORDRE' => $noOrdre+1,
                    'NM_PARAG' => $idMemos,
                ]);
            }
            
            return new Response(443, [
                
            ], (string) \json_encode([
                'statut' => 'O',
                'path' => $path,
            ]));
        } else {
            return $this->renderer->render('@popup-erreur/ErreurCreation', [
                'ErreurValid' => $validator->getError(),
                'desactiveRow' => true,
            ]);
        }
    }
    
    public function deleteAction()
    {
        $idAO = $this->request->getAttribute('idAO');
        $noOrdre = $this->request->getAttribute('noOrdre');
        
        $AoClausEntity = new AoClaus($this->pdo);
        
        $Clause = $AoClausEntity->find([
            'NO_AO' => $idAO,
            'NO_ORDRE' => $noOrdre,
        ]);
        sql::deleteMemos($this->pdo, $Clause['NM_PARAG']);
        
        
        $AoClausEntity->delete([
            'NO_AO' => $idAO,
            'NO_ORDRE' => $noOrdre,
        ]);

        return new Response(200, [], (string) \json_encode([]));
    }
}
