<?php
namespace Modules\Parametrage\Controller;

use Skeleton\Controller\Controller;
use Helpers\Ariane\Ariane;
use Helpers\Ariane\ArianeSub;
use Helpers\Trad\Trad;
use Modules\Parametrage\Repository\ParametrageOxalink;
use Helpers\Validator\Validator;
use Entity\Model\ParamDico;
use Helpers\Trad\ITrad;

class ParametrageAccesOxalinkController extends Controller
{
    public string $menuActive = '';
    public string $menuLeft = '';
    public ITrad $trad;

    public function init()
    {
        $this->trad = $this->getTranslationService();
        if (($_SESSION['ariane'][0]['path']??null) == $this->router->generateUri('Parametrage.Parametrage.index')) {
            $this->menuActive = 'Parametrage';
            $this->menuLeft = 'Parametrage';
        } else {
            $this->menuActive = 'Fournisseur';
            $this->menuLeft = 'Parametrage';
        }
    }
    
    public function accesAction() : string
    {
        $post = $this->request->getParsedBody();

        $listeParamOXALINK = [
            [
                'CLE' => 'PU-DEM_REF',
                'ESPACE' => 'PUBLIC',
                'NAME' => 'DEM_REF',
            ],
            [
                'CLE' => 'PU-DOM_COMP',
                'ESPACE' => 'PUBLIC',
                'NAME' => 'DOM_COMP',
            ],
            [
                'CLE' => 'PU-CONSULTATION',
                'ESPACE' => 'PUBLIC',
                'NAME' => 'CONSULTATION',
            ],
            [
                'CLE' => 'PR-CDE',
                'ESPACE' => 'PRIVE',
                'NAME' => 'CDE',
            ],
            [
                'CLE' => 'PR-AO',
                'ESPACE' => 'PRIVE',
                'NAME' => 'AO',
            ],
            [
                'CLE' => 'PR-CONSULTATION',
                'ESPACE' => 'PRIVE',
                'NAME' => 'CONSULTATION',
            ],
            [
                'CLE' => 'PR-DOM_COMP',
                'ESPACE' => 'PRIVE',
                'NAME' => 'DOM_COMP',
            ],
            [
                'CLE' => 'PR-FAC',
                'ESPACE' => 'PRIVE',
                'NAME' => 'FAC',
            ],
            [
                'CLE' => 'PR-FAC_CHARG',
                'ESPACE' => 'PRIVE',
                'NAME' => 'FAC_CHARG',
            ],
            [
                'CLE' => 'PR-FAC_LITIG',
                'ESPACE' => 'PRIVE',
                'NAME' => 'FAC_LITIG',
            ],
            [
                'CLE' => 'PR-FAC_CREAT',
                'ESPACE' => 'PRIVE',
                'NAME' => 'FAC_CREAT',
            ],
        ];
        
        $paramSQL = new ParametrageOxalink($this->pdo);
        $listeParam = $paramSQL->listeAccesOxalink();
                
        $validator = $this->createValidator();
        $paramdico = new ParamDico($this->pdo);
        
        if ($post) {
            $paramDicoTmp = $paramdico->findAll(['TYPEDICO' => 'OXALINK_FONCTIONS_CACHEES']);
            
            $arrayParamDico = [];
            foreach ($paramDicoTmp as $param) {
                $arrayParamDico[$param['ID1']][$param['ID2']] = $param['LIBELLE'];
            }
            
            foreach ($listeParamOXALINK as $val) {
                $validator->isCheck($post[$val['CLE']]);
                
                $value = $post[$val['CLE']];
                $espace = $val['ESPACE'];
                $code = $val['NAME'];
                
                if (isset($arrayParamDico[$espace][$code])) {
                    $paramdico->update(['LIBELLE' => ($value?'N':'O')], ['TYPEDICO' => 'OXALINK_FONCTIONS_CACHEES', 'ID1' => $espace, 'ID2' => $code]);
                } else {
                    $paramdico->insert(['TYPEDICO' => 'OXALINK_FONCTIONS_CACHEES', 'ID1' => $espace, 'ID2' => $code, 'LIBELLE' => ($value?'N':'O')]);
                }
            }
        } else {
            foreach ($listeParam as $value) {
                $espace = $value['ID1']=='PUBLIC'? 'PU':'PR';
                
                $post[$espace.'-'.$value['ID2']] = $value['LIBELLE']=='O'? false:true;
            }
        }
        
        //Ariane::generate(0, $trad->t('Gestion des paramètres'), $post);
        ArianeSub::generate(0, 3, $this->trad->t('Accès'), $this->router->generateUri('Parametrage.ParametrageAcces.acces'), true);
        
        return $this->renderer->render('@Parametrage/oxalink/acces', [
            'menuActive' => 'Fournisseur',
            'menuLeft' => 'Parametrage',
            'post' => $post,
        ]);
    }
}
