<?php

function displayModule($req, $module, $service, $listModules)
{
	global $denom, $type, $code, $rechet, $ape;
	//Vérifier que le module existe
	if(array_key_exists($module, $listModules))
	{
		$rech = array();
		$rech = array(
			'denom' 	=> $denom,
		 	'type'		=> $type,
		 	'code'		=> $code,
		 	'rechet' 	=> $rechet,
		 	'ape' 		=> $ape,
			);

		$url = bdf_lien_module($module, $service, $req, $rech);

		if($module=='07'){
			$filename = false;
		}
		else
		{
			$filename = PATH_SITE.'/cache/bdf/'.
				'bdf_'.$service.'_'.$req.'_'.$module.'.html';
		}
		//@todo : Ajouter timeover
		if($filename!==false && file_exists($filename))
		{
			$content = file_get_contents($filename);
		}
		else
		{
			$content = trim(bdf_loadpage($url));
			if( !empty($content) && $filename!==false )
			{
				file_put_contents($filename, $content);
			}
		}
	}
	return $content;
}


?>
<script>
$(document).ready(function(){
	$('#listeModulesD').click(function(){
		var position = $(this).position();
		$('#listeModules').css('top', position.top);
		$('#listeModules').css('left', position.left-200);
		var display = $('#listeModules').css('display');
		if(display=='none')	$('#listeModules').css('display', 'block');
		else $('#listeModules').css('display', 'none');
	});

	$('#closelisteModules').click(function(){
		$('#listeModules').css('display', 'none');
		$('#modules > #selected').html('');
		$('input[name=module[]]').each(function(){
			if ($(this).attr('checked')){
				$('#modules > #selected').append('<br/>'+$(this).parent().text());
			}
		});
	});

	$('#addIdentifiant').click(function(){
		$('form[name=rFibenM] > #identifiant').append('<br/><label>Identifiant</label> <input type="text" name="identifiant[]" />');
	});
});
</script>
<div id="center">
<?php
//Affichage de la liste des modules
if( $module=='autres' || $module=='' )
{
?>
	<h1>Modules</h1>
	<h2>Recherche FIBEN / FCC identifiant unique</h2>
	<div class="blockh2">
	<form name="rFibenU" method="post" action="/?page=bdf">
	<input type="hidden" name="type" value="u"/>
	<input type="hidden" name="siret" value="<?=$siret?>"/>

	<div id="identifiant">
	<label>Identifiant</label> <input type="text" name="req" value="<?=$req?>"/>
	<br/><span>SIREN ou clé BDF</span>
	</div>
	<div id="modules" class="clearfix">
	<a href='#' id="listeModulesD">Liste des modules</a>
	<span id="selected">
	<?php
	if (isset($_SESSION['BDF']['module']) &&
		is_array($_SESSION['BDF']['module']))
	{
		$listModules = bdf_modules_fiben();
		foreach ($listModules as $id => $module) {
		    if (isset($module['liste']) == false || $module['liste'] !== false) {
		        if (in_array($id, $_SESSION['BDF']['module']))
		        {
   			    	echo '<br/>'.$module['titre'];
		        }
		    }
		}
		$listModules = bdf_modules_fcc();
		foreach ($listModules as $id => $module) {
		    if (isset($module['liste']) == false || $module['liste'] !== false) {
		    	if (in_array($id, $_SESSION['BDF']['module']))
		        {
   			    	echo '<br/>'.$module['titre'];
		        }
		    }
		}
	}
	?>
	</span>
	</div>
	<div id="listeModules">
	<a href="#" id="closelisteModules">Fermer</a>
	<ul>
	<?php
	$listModules = bdf_modules_fiben();
	foreach ($listModules as $id => $module) {
	    if (isset($module['liste']) == false || $module['liste'] !== false) {
	    	$checked = '';
	    	if (isset($_SESSION['BDF']['module']) &&
	    		is_array($_SESSION['BDF']['module']) &&
	        	in_array($id, $_SESSION['BDF']['module']))
		    {
		    	$checked = 'checked';
		    }
	?>
	<li>
		<input type="checkbox" name="module[]" value="<?=$id?>" <?=$checked?>/>
		<?=$module['titre']?>
	</li>
	<?php
	    }
	}
	$listModules = bdf_modules_fcc();
	foreach ($listModules as $id => $module) {
	    if (isset($module['liste']) == false || $module['liste'] !== false) {
	        $checked = '';
	        if (isset($_SESSION['BDF']['module']) &&
	        	is_array($_SESSION['BDF']['module']) &&
	        	in_array($id, $_SESSION['BDF']['module']))
		    {
		    	$checked = 'checked';
		    }
	?>
	<li>
		<input type="checkbox" name="module[]" value="<?=$id?>" <?=$checked?>/>
		<?=$module['titre']?>
	</li>
	<?php
	    }
	}
	?>
	</ul>
	<br/>
	</div>
	<input class="button" type="submit" name="rFiben" value="Afficher"/>
	</form>
	</div>
	<br/>

	<h2>Recherche FIBEN / FCC identifiants multiples</h2>
	<div class="blockh2">
	<form name="rFibenM" method="post" action="/?page=bdf">
	<input type="hidden" name="type" value="m"/>
	<input type="hidden" name="siret" value="<?=$siret?>"/>
	<div id="identifiant">
	<label>Identifiant</label>
	<input type="text" name="identifiant[]" value="<?=$req?>" />
	<a href="#" id="addIdentifiant">Ajouter</a>
	</div>

	<div id="modules" class="clearfix">
	<label>Module</label>
	<select name="module">
	<?php
	$listModules = bdf_modules_fiben();
	foreach ($listModules as $id => $module) {
	    if (isset($module['liste']) == false || $module['liste'] !== false) {
	        echo '<option value="'.$id.'">'.$module['titre'].'</option>';
	    }
	}
	$listModules = bdf_modules_fcc();
	foreach ($listModules as $id => $module) {
	    if (isset($module['liste']) == false || $module['liste'] !== false) {
   	        echo '<option value="'.$id.'">'.$module['titre'].'</option>';
	    }
	}
	?>
	</select>
	</div>
	<input class="button" type="submit" name="rFiben" value="Afficher"/>
	</form>
	</div>
	<br/>

	<h2>Recherche FICP</h2>
	<div class="blockh2">
	<form name="rFicp" method="post" action="./?page=bdf">
	<input type="hidden" name="module" value="G"/>
	<input type="hidden" name="service" value="ficp"/>
	<label>Clé BDF</label>
	<input type="text" name="req"/>
	<input class="button" type="submit" name="rFicp" value="Ok"/>
	</form>
	</div>

<?php
}
//Affichage du contenu
else
{
	FB::log($module, 'module');

	//Mode multi module
	if($_REQUEST['type']=='u')
	{
		if (is_array($module))
		{
			foreach($module as $m)
			{
				if (array_key_exists($m, bdf_modules_fiben())){
					$service = 'fiben';
				} elseif ($_REQUEST['service']=='ficp' &&
						array_key_exists($m, bdf_modules_ficp())){
					$service = 'ficp';
				} elseif (array_key_exists($m, bdf_modules_fcc())){
					$service = 'fcc';
				}
				$func_module = 'bdf_modules_'.$service;
				$listModules = $func_module();
				$content = displayModule($req, $m, $service, $listModules);
				$titreModule = $listModules[$m]['titre'];
				?>
				<h1>Module <?=$titreModule?></h1>
				<div class="blockh2">
				<?php
				if(!empty($content))
				{
					echo $content;
				}else{
					echo 'ERREUR';
				}
				?>
				</div>
				<?php
			}
		} else {
				if (array_key_exists($module, bdf_modules_fiben())){
					$service = 'fiben';
				} elseif ($_REQUEST['service']=='ficp' &&
						array_key_exists($module, bdf_modules_ficp())){
					$service = 'ficp';
				} elseif (array_key_exists($module, bdf_modules_fcc())){
					$service = 'fcc';
				}
				$func_module = 'bdf_modules_'.$service;
				$listModules = $func_module();
				$content = displayModule($req, $module, $service, $listModules);
				$titreModule = $listModules[$module]['titre'];
				?>
				<h1>Module <?=$titreModule?></h1>
				<div class="blockh2">
				<?php
				if(!empty($content))
				{
					echo $content;
				}else{
					echo 'ERREUR';
				}
				?>
				</div>
				<?php

		}
	}
	//Mode multi-identifiant
	elseif ($_REQUEST['type']=='m')
	{
		foreach( $_REQUEST['identifiant'] as $req )
		{
			if (!empty($req))
			{
				if (array_key_exists($module, bdf_modules_fiben())){
					$service = 'fiben';
				} elseif ($_REQUEST['service']=='ficp' &&
						array_key_exists($module, bdf_modules_ficp())){
					$service = 'ficp';
				} elseif (array_key_exists($module, bdf_modules_fcc())){
					$service = 'fcc';
				}
				$func_module = 'bdf_modules_'.$service;
				$listModules = $func_module();
				$content = displayModule($req, $module, $service, $listModules);
				$titreModule = $listModules[$module]['titre'];
				?>
				<h1><?=$req?> - Module <?=$titreModule?></h1>
				<div class="blockh2">
				<?php
				if(!empty($content))
				{
					echo $content;
				}else{
					echo 'ERREUR';
				}
				?>
				</div>
				<?php
			}
		}
	}
}
?>
</div>
<?php