<?php

namespace Modules\Stock\MenuContextuel;

use Entity\Model\Parametr;
use Helpers\ChampsSupplementaire\ChampSupp;
use Helpers\Outil\MenuContextuel;
use Helpers\Outil\Roles;
use Helpers\Outil\Sql;
use Helpers\Trad\ITrad;
use Helpers\Trad\Trad;
use Modules\Stock\Interface\MenuContextuelInterface;
use Modules\Stock\Trait\MenuContextuelTrait;
use PDO;
use Skeleton\Router;

class BonEntreeContextuel extends MenuContextuel implements MenuContextuelInterface
{
    use MenuContextuelTrait;

    /**
     * @var array<string,mixed>
     */
    public array $droit = [];

    public function __construct(PDO $pdo, Router $router, ITrad $trad)
    {
        parent::__construct($pdo, $router, $trad);
    }

    /**
     * @param array<string,string|int> $entity
     * @return array<string, array<int,array<string, mixed>>>
     */
    public function generate(array $entity): array
    {
        $menuContextuel = [
            'listeSecondaire' => [
                [
                    'icone' => 'visualiser_v2',
                    'libelle' => $this->trad->t('Visualiser'),
                    'lien' => $this->router->generateUri('Stock.BonEntree.visualiser', ['noBe' => $entity['NO_BE']]),
                ],
                [
                    'libelle' => $this->trad->t('Documents rattachés'),
                    'lien' => $this->router->generateUri('GlobalApplication.DocumentRattaches.document', [
                        'MODULE' => 'BonEntree',
                        'id' => ChampSupp::autoCompletCodeEnr((string) $entity['NO_BE'])
                    ]),
                ]
            ]
        ];

        if (Roles::droitsUtilisateur($this->pdo, 'ST_BE_VAL')) {
            match ($entity['DTE_VALID']) {
                '00000000' => $menuContextuel['listePrincipal'][] = [
                    'icone' => 'pictoValidationManagement',
                    'libelle' => $this->trad->t('Valider'),
                    'lien' => false,
                    'lienJs' => 'popupConfirmValiderEntete('
                        . join(',', [
                            json_encode($this->trad->t('Validation du bon d\'entrée N° ' . $entity['NO_BE'])),
                            json_encode(Trad::strBuild($this->trad->t('Etes-vous sûr de vouloir valider le bon d\'entrée N°@1@ ?'), $entity['NO_BE'])),
                            json_encode($this->router->generateUri('Stock.BonEntree.valider', ['noBe' => $entity['NO_BE']])),
                        ]) . ');',
                ],
                default => null
            };
        }

        if ($entity['DTE_VALID'] === '00000000') {
            $menuContextuel['listePrincipal'][] = [
                'icone' => 'modifier_v2',
                'libelle' => $this->trad->t('Modifier'),
                'lien' => $this->router->generateUri('Stock.BonEntree.modifierForm', ['noBe' => $entity['NO_BE']]),
            ];

            if (Roles::droitsUtilisateur($this->pdo, 'ST_BE_SUP')) {
                $menuContextuel['listeSecondaire'][] = [
                    'libelle' => $this->trad->t('Supprimer'),
                    'lienJs' => 'popupConfirmSimple('
                        . join(',', [
                            json_encode($this->trad->t('Suppression du bon d\'entrée N° ' . $entity['NO_BE'])),
                            json_encode(Trad::strBuild($this->trad->t('Etes-vous sûr de vouloir supprimer le bon d\'entrée N°@1@ ?'), $entity['NO_BE'])),
                            json_encode($this->router->generateUri('Stock.BonEntree.supprimer', ['noBe' => $entity['NO_BE']])),
                            '{}',
                            '() => {window.action.arianeReload()}'
                        ]) . ');',
                ];
            }
        }

        if ($entity['DTE_VALID'] !== '00000000') {
            if (Sql::toBool((new Parametr($this->pdo))->get()->IMP_ETIQ)) {
                $menuContextuel['listeSecondaire'][] = [
                    'target' => '_blank',
                    'libelle' => $this->trad->t('Imprimer étiquettes'),
                    'lien' => $this->router->generateUri('Stock.BonEntree.etiquettes', ['noBe' => $entity['NO_BE']]),
                ];
            }

            if (Roles::droitsUtilisateur($this->pdo, 'ST_BE_IMP')) {
                $menuContextuel['listeSecondaire'][] = [
                    'target' => '_blank',
                    'libelle' => $this->trad->t('Imprimer'),
                    'lien' => $this->router->generateUri('Stock.BonEntree.imprimer', ['noBe' => $entity['NO_BE']]),
                ];
            }
        }

        return $menuContextuel;
    }
}
