<?php

namespace Modules\DemandeAchat\Controller;

use Modules\DemandeAchat\Outil\FormArray;
use Skeleton\Controller\Controller;
use Helpers\Ariane\Ariane;
use Helpers\Ariane\ArianeSub;
use Helpers\Trad\Trad;
use Entity\Model\DaEntet;

class ModificationTypeDocDemandeAchatController extends Controller
{

    public function modificationTypeDocAction(): string
    {
        $idDa = $this->request->getAttribute('idDA');

        $DaEntet = new DaEntet($this->pdo);
        $DaEntet = $DaEntet->find($idDa);

        $trad = $this->getTranslationService();

        $TypeDoc = new \Entity\Model\TypeDoc($this->pdo);
        $typeDoc = $TypeDoc->fetchAll([], ['filter_NVIBOXAP' => 'F']);

        foreach ($typeDoc as $value) {
            $tabTypeDoc[$value['TYPE']] = $value['LIBELLE'];
        }

        $formArray = FormArray::getFormArrays($trad);

        if ($_SESSION['arianeSub'] ?? '') {
            ArianeSub::generate(2, 0, $trad->t('Modication Type de Document'), $_SERVER['REQUEST_URI'], 1);
        } else {
            Ariane::generate(1, $trad->t('Modication Type de Document'));
        }

        return $this->renderer->render('@DemandeAchat/ModificationTypeDoc/modificationTypeDoc', [
            'menuActive' => 'CollecteBesoins',
            'menuLeft' => 'GestionDemandeAchat',
            'idDA' => $idDa,
            'demandeAchat' => $DaEntet,
            'typeChiffExec' => $formArray['typeChiffExec'],
            'tabTypeMarchePublic' => $formArray['tabTypeMarchePublic'],
            'tabPeriodicite' => $formArray['tabPeriodicite'],
            'tabPeriode' => $formArray['tabPeriode'],
            'tabJoursAppli' => $formArray['tabJoursAppli'],
            'tabTypeDate' => $formArray['tabTypeDate'],
            'tabTypeDoc' => $tabTypeDoc,
        ]);
    }

    public function confirmationChgtTypeDocAction(): string
    {
        $idDA = $this->request->getAttribute('idDA');
        $trad = $this->getTranslationService();

        return $this->renderer->render('@popup-erreur/confirmationGestion', [
            // 'titre' => ' ',
            'message' => Trad::strBuild($trad->t("Etes-vous sûr de vouloir supprimer la demande d'achat n° @1@"), $idDA),
            'option'  =>  $this->router->generateUri('DemandeAchat.ModificationTypeDocDemandeAchat.validationTypeDoc', ['idDA' => $idDA]),
            'onclick' => 'validation',
        ]);
    }

    public function validationTypeDocAction(): string
    {
        $post = $this->request->getParsedBody();
        $idDA = $this->request->getAttribute('idDA');
        $trad = $this->getTranslationService();

        $TypeDoc = new \Entity\Model\TypeDoc($this->pdo);
        $typeDoc = $TypeDoc->fetchAll([], ['filter_NVIBOXAP' => 'F']);
        foreach ($typeDoc as $value) {
            $tabTypeDoc[$value['TYPE']] = $value['LIBELLE'];
        }

        $validator = $this->createValidator();
        $validator->contentInArray($trad->t("Type de DA"), $post['TYP_DOC'], $tabTypeDoc);

        if ($validator->isValid()) {
            // TODO METTRE EN PLACE L'ACTION D'ENREGISTEMENT (WSDL ?)


            // TODO PROVISOIRE
            return $this->renderer->render('@DemandeAchat/Popup/popupConfirmation', [
                'message' => Trad::strBuild($trad->t("Le type de la demande d'achat @1@ est modifié."), $idDA),
                'redirect' => 1,
                'iconePopup' => 'information',
                'path' => $this->router->generateUri('DemandeAchat.GestionDemandeAchat.index'),
            ]);
        } else {
            $params = [
                'alerte' => $validator->getError(),
                'titrePopup'  => $trad->t("Erreur"),
            ];
            return $this->renderer->render('@popup-erreur/erreurAlerteEtAvertissement', [
                'params' => $params,
            ]);
        }
    }
}
