<?php
global $myUser,$conf;
if(!$myUser->can('customiser','configure')) throw new Exception("Vous n'avez pas la permission pour executer cette fonctionnalité",403);

$themePath = __DIR__.SLASH.'css'.SLASH.'theme.css';
if(!file_exists($themePath)) 
	copy(__DIR__.SLASH.'css'.SLASH.'theme.sample.css',__DIR__.SLASH.'css'.SLASH.'theme.css');

$themeStream = file_get_contents($themePath);

$types = array(
	'background-color' => array('label'=>'Couleur de fond','type'=>'color'),
	'border-color' => array('label'=>'Couleur de bordure','type'=>'color'),
	'background-image'=>array('label'=>'Image de fond','type'=>'image'),
	'color'=>array('label'=>'Couleur de texte','type'=>'color'),
	'font-size'=>array('label'=>'Taille de texte','type'=>'size'),
	'font-family'=>array('label'=>'Police','type'=>'font'),
);

preg_match_all('$/\*::([^\*]*)\*/
[\n\r\s\t]*([#.]?[^\{]*){([^\}]*)
}$iUs', $themeStream , $matches,PREG_SET_ORDER);

$css = array();
foreach ($matches as $match) {
	preg_match_all('$(.*):(.*);$iU', $match[3] , $contentMatches,PREG_SET_ORDER);
	$properties = array();
	foreach($contentMatches as $contentMatch){
		$property = array(
			'instruction' => trim($contentMatch[1]),
			'value' => trim($contentMatch[2]),
		
			'id' =>  base64_encode($match[2].'-'.trim($contentMatch[1])),
		);

		if(!isset($types[$property['instruction']])) continue;

		$property['type'] = $types[$property['instruction']]['type'];
		$property['description'] = $types[$property['instruction']]['label'];


		$properties[] = $property;
	}
	if(count($properties)==0) continue;
	if(!isset($css[trim($match[1])])) $css[trim($match[1])] = array();
	$css[trim($match[1])][] = array(
		'comment' => trim($match[1]),
		'signature' =>  trim($match[2]),
		'content' => $properties
	);
}
?>

<div class="row">
	<div class="col-md-12">
		<br>
		<?php if($myUser->can('customiser', 'edit')) : ?>
			<div onclick="customiser_setting_save();" class="btn btn-success float-right"><i class="fas fa-check"></i> Enregistrer</div>
			<div onclick="if(confirm('Cette action est irreverssible, êtes vous sûre de vouloir réinitialiser le thème?'))window.location='action.php?action=customiser_reset_theme';" class="btn btn-danger float-right mr-2"><i class="fas fa-undo-alt"></i> Réinitialiser</div>
		<?php endif; ?>
		<h3>Réglages Thème</h3>
		<div class="clear"></div>
		<hr>
	</div>
</div>
<div class="row">
	<!-- search results -->
	<div class="col-xl-12">
		<table id="customiser-setting-form" class="table table-striped">
			<tbody>
				<?php foreach($css as $comment=>$blocks): ?>
				<tr>
					<th colspan="2"><h5><i class="fas fa-angle-right"></i> <?php echo $comment;?></h5></th>
				</tr>
				<?php foreach($blocks as $block): ?>
					<?php foreach($block['content'] as $property): ?>
					<tr>
						<th class="align-middle"><?php echo $property['description']; ?></th>
						<td class="align-middle"><?php 
						$attributes = array();
						if(isset($infos['placeholder'])) $attributes[] = 'placeholder="'.$infos['placeholder'].'"';
						$attributes['class'] = 'class="form-control"';
						$attributes[] = 'id="'.$property['id'].'"';
						$attributes[] = 'data-signature="'.$block['signature'].'"';
						$attributes[] = 'data-instruction="'.$property['instruction'].'"';
						$attributes[] = 'data-field-type="'.$property['type'].'"';
						$attributes['value'] = 'value="'.$property['value'].'"';
						$attributes['change'] = 'onchange="customiser_preview(this);"';
						switch($property['type']){
							case 'color':
							if(strlen($property['value']) == 4){
								$property['value']  = '#'.str_repeat(substr($property['value'],1), 2);
								$attributes['value'] = 'value="'.$property['value'].'"';
							}
							$input = '<input type="color" '.implode(' ',$attributes).'  >'; 
							break;
							case 'image':
								$property['value'] = str_replace(array("url","'",'"',"(",")"),'',$property['value']);
								$property['value'] = str_replace(array("../"),'',$property['value']);
								$attributes['value'] = 'value="'.$property['value'].'"';
								//$attributes['data-type'] = 'data-type="image"';
								$input = '<input type="file" '.implode(' ',$attributes).'  >'; 
							break;
							case 'font':
								$property['value'] = str_replace(array("'",'"'),'',$property['value']);
								$attributes['value'] = 'value="'.$property['value'].'"';
								$input = '<input type="text" '.implode(' ',$attributes).'  >'; 
							break;
							default:
								$input = '<input type="text" '.implode(' ',$attributes).'  >'; 
							break;
						}
						echo $input; ?>
						</td>
					<?php endforeach; ?>
				<?php endforeach; ?>
				</tr>
				<?php endforeach; ?>
			</tbody>
		</table>
	</div>
</div>