<?php
namespace Modules\DemandeAchat\Controller;

use Entity\Model\Hastutil;
use Entity\Model\Paravrac;
use GuzzleHttp\Psr7\Response;
use Modules\DemandeAchat\Outil\FormArray;
use Modules\GlobalApplication\Repository\LicenceOxalys;
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(
        LicenceOxalys $licenceOxalys
    ): 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,
            $post,
            1,
            $licenceOxalys
        );

        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' => (new LicenceOxalys($this->pdo))->expeditionActive(),
            '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
        ]);
    }

    public function popupPlanningActionAction() :string
    {
        return $this->renderer->render('@DemandeAchat/DemandeAchat/popupPlanning', [
            'listCaLigne' =>
                (new DemandeAchat($this->pdo))->listLignePlanning(
                    $this->request->getAttribute('idDA')
                ),
        ]);
    }
}
