<?php
namespace Modules\DemandeAchat\Controller;

use Entity\Model\Hastutil;
use Entity\Model\Paravrac;
use Modules\DemandeAchat\Outil\FormArray;
use Skeleton\Controller\Controller;
use Helpers\Ariane\Ariane;
use Helpers\Ariane\ArianeSub;
use Modules\DemandeAchat\MenuContextuel\GestionMenuContextuel;
use Modules\DemandeAchat\Repository\DemandeAchat;

class DemandeAchatController extends Controller
{

    public function demandeAchatAction(): string
    {
        $post = $this->request->getParsedBody();
        $numDA = $this->request->getAttribute('idDA');
        $MODULE = $this->request->getAttribute('MODULE') ?? 'DemandeAchat';
        $trad = $this->getTranslationService();

        $paravrac = new Paravrac($this->pdo);
        $param['DA_AFF_CODE_ART'] = $paravrac->getGeneric('DA_AFF_CODE_ART') == 'O';
        $param['DA_AFF_CODE_REF_FAB'] = $paravrac->getGeneric('DA_AFF_CODE_REF_FAB') == 'O';
        $param['DA_AFF_REF_ART_FOUR'] = $paravrac->getGeneric('DA_AFF_REF_ART_FOUR') == 'O';
        $param['ACTIVATION_BPA'] = $paravrac->getGeneric('ACTIVATION_BPA') == 'O';

        $purchaseRequest = (new DemandeAchat($this->pdo))
            ->demandeAchatEntete($numDA);

        $purchaseRequest['menuContextuel'] = (new GestionMenuContextuel(
            $this->pdo,
            $this->router,
            $trad
        ))->lightGenerate($purchaseRequest);

        $formArray = FormArray::getFormArrays(
            $this->getTranslationService(),
        );

        Ariane::generate(2, $trad->t('Détails'), $post);
        ArianeSub::initPurchaseRequestSub(
            $trad,
            $this->router,
            $numDA,
            $MODULE ?? 'DemandeAchat',
            $post,
            1
        );

        return $this->renderer->render('@DemandeAchat/DemandeAchat/index', [
            'numDA' => $numDA,
            'da' => $purchaseRequest,
            'typeChiffExec' => $formArray['typeChiffExec'],
            'tabTypeMarchePublic' => $formArray['tabTypeMarchePublic'],
            'tabPeriodicite' => $formArray['tabPeriodicite'],
            'tabPeriode' => $formArray['tabPeriode'],
            'tabJoursAppli' => $formArray['tabJoursAppli'],
            'tabTypeDate' => $formArray['tabTypeDate'],

            'isExpedActive' => $_SESSION['IS_EXPE_ACTIVE'],
            'module' => $MODULE,
            // TODO : does the user really need to be reminded of its name ?
            'userNom' => (new Hastutil($this->pdo))->find($_SESSION['Session_Login'])['NOM'],
            'param' => $param
        ]);
    }
}
