<?php

namespace Modules\Reception\Controller;

use Helpers\Outil\Roles;
use Helpers\Outil\Sql;
use Helpers\Trad\Trad;
use Modules\Reception\Repository\CreationBR;
use Modules\Reception\Repository\GestionBR;
use Skeleton\Controller\Controller;

class AjaxController extends Controller
{
    /** @var GestionBR $repository */
    protected GestionBR $gestionBRRepository;

    /** @var CreationBR $creationBRRepository */
    protected CreationBR $creationBRRepository;

    /** @var array<mixed> $droits */
    protected array $droits;

    /**
     * @return void
     */
    public function init(): void
    {
        $this->gestionBRRepository  = new GestionBR($this->pdo);
        $this->creationBRRepository = new CreationBR($this->pdo);

        $this->droits = [
            'BR_AFF_CODE_ART'       => Roles::droit($this->pdo, 'BR_AFF_CODE_ART') == 'O',
            'CTR_EMET_VISIBLE_BR'   => sql::paravrac($this->pdo, 'OXAPROC', 'CTR_EMET_VISIBLE_BR') == 'O',
            'DESIGN_VISIBLE_BR'     => sql::paravrac($this->pdo, 'OXAPROC', 'DESIGN_VISIBLE_BR') == 'O',
            'BR_AFF_REF_ART_FOUR'   => sql::paravrac($this->pdo, 'OXAPROC', 'BR_AFF_REF_ART_FOUR') == 'O',
            'HA_BR_PX'              => Roles::droitsUtilisateur($this->pdo, 'HA_BR_PX'),
        ];
    }

    /**
     * @return string
     */
    public function getCommandeDetailEntetAction(): string
    {
        $commandeId = $this->request->getAttribute('order');

        $lignesCommandes    = $this->gestionBRRepository->getLignesCommandeForReception($commandeId);
        $infoLigne          = $this->gestionBRRepository->getInfoOnLigneCDECS($commandeId);
        $commande           = $this->creationBRRepository->orderDetails($commandeId);

        return $this->renderer->render('@Reception/Commande/ligne-commande', [
            'id'        => $commandeId,
            'lignes'    => $lignesCommandes,
            'infoLigne' => $infoLigne,
            'commande'  => $commande,
            'droits'    => $this->droits,
        ]);
    }

    public function getLigneCommandeDetailAction(): string
    {
        $commandeId = $this->request->getAttribute('order');
        $noPo       = $this->request->getAttribute('po');

        $order      = $this->creationBRRepository->orderDetails($commandeId);
        $lineDetail = $this->gestionBRRepository->getLigneCommandeDetails($commandeId, $noPo);

        return $this->renderer->render('@Reception/Commande/ligne-article', [
            'id'        => $commandeId,
            'order'     => $order,
            'details'   => $lineDetail,
            'droits'    => $this->droits,
        ]);
    }

    public function getBRDetailAction(): string
    {
        $numBR  = $this->request->getAttribute('br');

        $lignes     = $this->gestionBRRepository->listeLigneBR($numBR);
        $details    = $this->gestionBRRepository->getDetailBR($numBR);

        return $this->renderer->render('@Reception/BR/ligne', [
            'lignes'    => $lignes,
            'numBR'     => $numBR,
            'details'   => $details,
            'droits'    => $this->droits,
        ]);
    }
}
