<?php

namespace Modules\Ajax\Controller;

use Skeleton\Controller\Controller;

class FiltreAvanceController extends Controller
{
    
    public function nouveauCritereAction() : string
    {
        
        $post = $this->request->getParsedBody();
        $arrayPath = [];
        $namePath = '';
        
        $key = explode(';', substr($post['key'], 0, -1));
  
        $keySelect = [];
        for ($i=0; $i < count($key); $i++) {
            //echo $key[$i];
            $resultExplode = explode('#', $key[$i]);
            
            if ($resultExplode[0] == $post['nom']) {
                if (isset($resultExplode[4]) && strpos($resultExplode[4], 'separatorPath') !== false) {
                    $tmpExplode = explode('separatorPath', $resultExplode[4]);
                 
                    $namePath = $tmpExplode[0];
                    
                   
                    foreach (explode('separatorParams', $tmpExplode[1]) as $valueSeparator) {
                        $valueTmpInsert = explode('separatorValue', $valueSeparator);

                        $arrayPath[$valueTmpInsert[0]] = $valueTmpInsert[1];
                    }
                } else {
                    $namePath = $resultExplode[4] ?? '';
                }
                $keySelect = $resultExplode;
            }
        }
        $repository = false;
        $nameRepository = '';
        if (strpos($keySelect[0], 'repository') ===0) {
            $repository = true;
            $nameRepository = str_replace('repository', '', $keySelect[0]);
        }


        return $this->renderer->render('@ajax-filtre-avance/nouveau-critere', [
            'id' => $post['id'],
            'idNude' => $post['idNude'],
            'type' => $post['type'],
            'keySelect' => $keySelect,
            'repository' => $repository,
            'nameRepository' => $nameRepository,
            'namePath' => $namePath,
            'arrayPath' => $arrayPath,
        ]);
    }
   
    
    public function nouveauTupleformAction() : string
    {
        $post = $this->request->getParsedBody();
        
       
        $key = explode(';', substr($post['key'], 0, -1));
        $keySelect = [];
        for ($i=0; $i < count($key); $i++) {
            $keySelect[$i] = explode('#', $key[$i]);
        }
        return $this->renderer->render('@ajax-filtre-avance/nouveau-tuple-form', [
            'id' => $post['id'],
            'keys' => $keySelect,
            'keyFiltreAvance' => addslashes($post['key'])
        ]);
    }
}
