<?php

namespace Modules\Evaluation\MenuContextuel;

use Helpers\Outil\MenuContextuel;
use Helpers\Trad\ITrad;
use Helpers\Trad\Trad;
use Helpers\User\User;
use Skeleton\Router;
use Helpers\Outil\Sql;

class GestionMenuContextuel extends MenuContextuel
{
    /**
     * @var array<mixed> $droit
     */
    public $droit;

    /**
     * @var bool|string $typePreaffectationMail
     */
    public $typePreaffectationMail;

    /**
     * @param array<mixed> $droit
     */
    public function __construct(\PDO $pdo, Router $router, ITrad $trad, $droit)
    {
        parent::__construct($pdo, $router, $trad);
        $this->droit = $droit;
    }

    /**
     * @param array<mixed> $eval
     * @return array<mixed>
     */
    public function generate($eval)
    {
        $menuContextuel = [];

        if ($this->droit['HA_CEVALCM']) {
            if ($eval['STATUT'] == 'P') {
                $menuContextuel['listePrincipal'][] = [
                    'icone' => 'pictoValidate',
                    'lien' => '',
                    'libelle' => $this->trad->t('Valider'),
                    //todo Utirlisé le $this->popupConfirm
                    'lienJs' => "popupConfirmation(
                                    '" . $this->router->generateUri('Evaluation.Campaign.validation', ['id' => $eval['ID_CAMP']]) . "', 
                                    'reload', 
                                    '" . $this->router->generateUri('Evaluation.Campaign.validation', ['id' => $eval['ID_CAMP']]) . "', 
                                    '" . $this->trad->t('Voulez-vous valider la préparation de cette campagne ?') . "', 
                                    '', '', '', '', '', '', '', '', '', '', 
                                    '" . $this->trad->t('Validation campagne') . "')",
                ];

                $menuContextuel['listeSecondaire'][] = [
                    'lien' => $this->router->generateUri('Evaluation.Campaign.createdit', ['id' => $eval['ID_CAMP']]),
                    'libelle' => $this->trad->t('Modifier'),
                ];
    
                if ($this->droit['HA_CEVASUP']) {
                    $menuContextuel['listeSecondaire'][] = [
                        'lien' => '',
                        'libelle' => $this->trad->t('Supprimer'),
                        'lienJs' => $this->popupConfirm(
                            false,
                            $this->router->generateUri('Evaluation.Campaign.suppression', ['id' => $eval['ID_CAMP']]),
                            Trad::strBuild($this->trad->t('Voulez-vous supprimer la campagne @1@ ?'), $eval['LIBELLE']),
                            true,
                            'tableRefresh',
                            '',
                            false,
                            'filtre_campagne'
                        ),
                    ];
                }
            }

            $menuContextuel['listeSecondaire'][] = [
                'lien' => $this->router->generateUri('Evaluation.Campaign.duplicate', ['id' => $eval['ID_CAMP']]),
                'libelle' => $this->trad->t('Dupliquer'),
            ];

            if ($eval['STATUT'] == 'E') {
                $menuContextuel['listeSecondaire'][] = [
                    'lien' => '',
                    'libelle' => $this->trad->t('Dévalider'),
                    'lienJs' => "popupConfirmationDevalidation(
                                    '" . $this->router->generateUri('Evaluation.Campaign.popupConfirmationDevalidation', ['id' => $eval['ID_CAMP']]) . "', 
                                    'reload', 
                                    '" . $this->router->generateUri('Evaluation.Campaign.devalidation', ['id' => $eval['ID_CAMP']]) . "', 
                                    '" . $this->trad->t('Voulez-vous dévalider cette campagne ?') . "', 
                                    'liste-campagnes', '" . $this->trad->t('Dévalidation campagne') . "')",
                ];
            }

            if ($eval['STATUT'] == 'E') {
                $rang = $eval['total'] ? (float)$eval['encours'] * 100 / (float)$eval['total'] : (float)0;

                if ($rang != 100) {
                    $menuContextuel['listeSecondaire'][] = [
                        'lien' => '',
                        'libelle' => $this->trad->t('Relance évaluateurs'),
                        'lienJs' => $this->ajaxPopupV2($this->router->generateUri('Evaluation.Campaign.resumeRaters', ['id' => $eval['ID_CAMP']])),
                    ];
                }

                if ($eval['REPONSEENCOURS'] != '') {
                    $menuContextuel['listeSecondaire'][] = [
                        'lien' => $this->router->generateUri('Evaluation.Campaign.detail', ['id' => $eval['ID_CAMP']]),
                        'libelle' => $this->trad->t('Saisie des réponses'),
                    ];
                }
            }

            if (($eval['STATUT'] == 'D') && (User::getUser()->getCodeUser() == $eval["CREEEPAR"])) {
                $menuContextuel['listeSecondaire'][] = [
                    'lien' => $this->router->generateUri('Evaluation.Evaluation.notationDepouilleur', ['idCamp' => $eval['ID_CAMP']]),
                    'libelle' => $this->trad->t('Notation dépouilleur'),
                ];
            }

            if (in_array($eval['STATUT'], ['E', 'A', 'N']) || (($eval['STATUT'] == 'D') && (User::getUser()->getCodeUser() == $eval["CREEEPAR"]))) {
                $menuContextuel['listeSecondaire'][] = [
                    'lien' => '',
                    'libelle' => $this->trad->t('Clôturer'),
                    //todo Utirlisé le $this->popupConfirm
                    'lienJs' => "popupConfirmation(
                                    '" . $this->router->generateUri('Evaluation.Campaign.cloture', ['id' => $eval['ID_CAMP']]) . "', 
                                    'reload', 
                                    '" . $this->router->generateUri('Evaluation.Campaign.cloture', ['id' => $eval['ID_CAMP']]) . "', 
                                    '" . $eval['closingMessage'] . "', 
                                    'page', '', '', '', '', '', '', '', '', '', 
                                    '" . $this->trad->t('Clôture campagne') . "')",
                ];
            }
        }

        if ($eval['STATUT'] != 'P') {
            $menuContextuel['listeSecondaire'][] = [
                'lien' => $this->router->generateUri('Evaluation.Campaign.etatAvancement', ['id' => $eval['ID_CAMP'], 'date' =>  $eval['DTECREAT'], 'par' =>  $eval['CREATOR']]),
                'libelle' => $this->trad->t('Etat d\'avancement par utilisateur'),
            ];
            $menuContextuel['listeSecondaire'][] = [
                'lien' => $this->router->generateUri('Evaluation.Campaign.etatAvancementByFourniss', ['id' => $eval['ID_CAMP'], 'date' =>  $eval['DTECREAT'], 'par' =>  $eval['CREATOR']]),
                'libelle' => $this->trad->t('Etat d\'avancement par fournisseur'),
            ];
        }

        if ($eval['STATUT'] == 'T' && (Sql::toBool($eval['PUBLIER_SYNTHFOUR'])) == false) {
            $menuContextuel['listeSecondaire'][] = [
                'libelle' => $this->trad->t("Publier la synthèse de l'évaluation sur le portail fournisseur"),
                //todo Utirlisé le $this->popupConfirm
                'lienJs' => "popupConfirmation(
                    '" . $this->router->generateUri('Evaluation.Campaign.publierSynthese', ['id' => $eval['ID_CAMP']]) . "',
                    'reload',
                    '" . $this->router->generateUri('Evaluation.Campaign.publierSynthese', ['id' => $eval['ID_CAMP']]) . "',
                    '" . $this->trad->t("Publier la synthèse de l\\'évaluation sur le portail fournisseur") . "',
                    '', '', '', '', '', '', '', '', '', '', 
                    "
                    . json_encode(Trad::strBuild(
                        $this->trad->t("Publication de la synthèse d’évaluation @1@"),
                        $eval['ID_CAMP']
                    ))
                    . ")",
            ];
        }

        if ($eval['STATUT'] == 'T') {
            $menuContextuel['listeSecondaire'][] = [
                'lien' => $this->router->generateUri('Evaluation.Resultat.index', ['id' => $eval['ID_CAMP']]),
                'libelle' => $this->trad->t('Résultat'),
            ];
        }

        $menuContextuel['listePrincipal'][] = [
            'icone' => 'pictoList',
            'lien' => $this->router->generateUri(
                'Evaluation.CampaignEvent.monitoring',
                ['id' => $eval['ID_CAMP']]
            ),
            'libelle' => $this->trad->t('Suivi'),
        ];

        return $menuContextuel;
    }
}
