<?php
namespace Modules\Commande\Controller;

use Skeleton\Controller\Controller;
use Modules\Commande\Repository\Clause;
use GuzzleHttp\Psr7\Response;
use Helpers\Outil\Roles;
use Helpers\Trad\Trad;
use Helpers\Validator\Validator;
use Helpers\Outil\Sql;

class ModificationClauseController extends Controller
{
    public function ajaxAjoutClauseAction() : string
    {
        $idCommande = $this->request->getAttribute('idCommande');
        $post = $this->request->getParsedBody();
        $idClause = $post['index'];
        
        $clause = new Clause($this->pdo);
        $detailClause = $clause->detailClause($idCommande, $idClause);
        
        if ($detailClause) {
            $clause = $detailClause;
        }

        return $this->renderer->render('@Commande/Creation/Clause/ClauseModifiable', [
            'clause' => $clause,
            'idClause' => $idClause,
            'idCommande' => $idCommande,
        ]);
    }
    
    /**
     * @return Response|string
     */
    public function validationClauseAction()
    {
        $idCommande = $this->request->getAttribute('idCommande');
        $post = $this->request->getParsedBody();
        $idClause = $post['clause_NO_ORDRE'];
        
        $validator = $this->createValidator();
        $validator->isStringAvecEspace('Clause', $post['clause_CONTMEMO'], 1, 3000);

        if ($validator->isValid()) {
            $clause = new Clause($this->pdo);
            $detailClause = $clause->detailClause($idCommande, $idClause);
            
            if ($detailClause) {
                Sql::updateMemo($this->pdo, $detailClause['NODEMEMO'], $post['clause_CONTMEMO']);
                $detailClause = $clause->detailClause($idCommande, $idClause);
            } else {
                $array = [
                    'noCde' => $idCommande,
                    'noClause' => $idClause,
                    'contentClause' => $post['clause_CONTMEMO'],
                ];
                $clause->insertClause($array);
                
                $detailClause = $clause->detailClause($idCommande, $idClause);
            }

            $droit = [
                'HA_CA_CLA'     => Roles::droitsUtilisateur($this->pdo, 'HA_CA_CLA', $_SESSION["Session_Login"]),
            ];

            return new Response(443, [], $this->renderer->render('@Commande/Creation/Clause/ClauseNonModifiable', ['clause' => $detailClause, 'idCommande' => $idCommande, 'droit' => $droit]));
        } else {
            $trad = $this->getTranslationService();
            return $this->renderer->render('@popup-erreur/ErreurCreation', [
                'ErreurValid' => $validator->getError(),
                'titrePopup'  => $trad->t('Erreur sur la validation de la clause') . $trad->colon() . $idClause,
            ]);
        }
    }
    
    public function ajaxSuppresionClauseAction() :Response
    {
        $idCommande = $this->request->getAttribute('idCommande');
        $post = $this->request->getParsedBody();
        $idClause = $post['clause_NO_ORDRE'];
        
        $clause = new Clause($this->pdo);
        $clause->deleteClause($idCommande, $idClause);

        return new Response(200);
    }
    
    public function annulationClauseCommandeAction() :Response
    {
        $idCommande = $this->request->getAttribute('idCommande');
        $post       = $this->request->getParsedBody();
        
        $idClause = $post['clause_NO_ORDRE'];
        
        $clause = new Clause($this->pdo);
        $detailClause = $clause->detailClause($idCommande, $idClause);
        
        $droit = [
            'HA_CA_CLA'     => Roles::droitsUtilisateur($this->pdo, 'HA_CA_CLA', $_SESSION["Session_Login"]),
        ];

        if ($detailClause) {
            return new Response(200, [], $this->renderer->render('@Commande/Creation/Clause/ClauseNonModifiable', ['clause' => $detailClause, 'idCommande' => $idCommande, 'droit' => $droit]));
        } else {
            return new Response(200);
        }
    }
    
    public function ajaxModificationOrdreAction() : string
    {
        $idCommande = $this->request->getAttribute('idCommande');
        $post       = $this->request->getParsedBody();
        
        $newPosition = $post['newPosition'];
        $oldPosition = $post['oldPosition'];
        
        $moveUp = false;
        if ($newPosition<$oldPosition) {
            $moveUp = true;
        }
        
        $clause = new Clause($this->pdo);
        $clause->modificationOrdreClause($idCommande, $newPosition, $oldPosition, $moveUp);
        
        return '';
    }
}
