<?php

namespace Modules\ProjetAchat\Controller;

use Skeleton\Controller\Controller;
use GuzzleHttp\Psr7\Response;
use Helpers\Ariane\Ariane;
use Helpers\Validator\Validator;

class CreationTableOperationController extends Controller
{
    public function creationAction(): string
    {
        $post = $this->request->getParsedBody();
        $mode = $this->request->getAttribute('mode');
        $idOperation = $this->request->getAttribute('idOperation');

        $DoarOp = new \Entity\Model\DoarOpe($this->pdo);
        $operation = $DoarOp->find($idOperation);

        if ($mode == 'creation') {
            $titleOnglet = $this->getTranslationService()->t('Nouvelle opération');
            $h2 = '';
        } else {
            $titleOnglet = $this->getTranslationService()->t('Modification');
            $h2 = $this->getTranslationService()->t("Modification de l'opération")
                . ' : '
                . $operation['LIB']
                . ' (' . $idOperation . ')';
        }


        Ariane::generate(1, $titleOnglet, $post);

        return $this->renderer->render('@tableOperation/creation', [
            'menuActive'             => 'ProjetAchat',
            'operation'              => $operation ?? null,
            'mode'                   => $mode,
            'idOperation'            => $idOperation,
            'H2'                     => $h2,
        ]);
    }

    public function validationAction()
    {
        $post = $this->request->getParsedBody();
        $mode = $this->request->getAttribute('mode');
        $idOperation = $this->request->getAttribute('idOperation');
        $validator = $this->createValidator();
        $DoarOp = new \Entity\Model\DoarOpe($this->pdo);
        
        
        $trad = $this->getTranslationService();

        /***************/
        /** Validator **/
        if ($validator->isStringSansEspace($trad->t('Code'), $post['CODE_OPE'], 1, 10) && $mode=='creation') {
            $validator->isTrue($DoarOp->find($post['CODE_OPE']), '<br />'.$trad->t('Ce code existe déjà.'));
        }
        
        $validator->isStringAvecEspace($trad->t('Libellé'), $post['LIB'], 1, 120);
        $validator->isStringAvecEspace($trad->t('Description'), $post['DESCR'], 0, 3000);
        $validDatedeb = $validator->isDate($post['DATE_DEB'], '', '', false, $trad->t('Date de début'));
        $validDateFin = $validator->isDate($post['DATE_FIN'], '', '', false, $trad->t('Date de fin'));
        if ($validDatedeb && $validDateFin) {
            $validator->isTrue($post['DATE_DEB']>$post['DATE_FIN'], '<br />'.$trad->t("La date de début ne peut pas être ultérieure à la date de fin."));
        }

        if ($validator->isValid()) {
            if ($mode == 'creation') {
                $DoarOp->insert($post);
            } else {
                $DoarOp->update($post, $idOperation);
            }

            return new Response(200, [], (string) \json_encode([
                'statut' => 'O',
                'path' => $this->router->generateUri('ProjetAchat.TableOperation.tableOperation'),
            ]));
        } else {
            $params = [
                'alerte' => $validator->getError(),
            ];
        }
        return new Response(443, [], $this->renderer->render('@popup-erreur/erreurAlerteEtAvertissement', ['params' => $params]));
    }
}
