<?php
global $myUser,$conf;
User::check_access('customiser','configure');

$themes = array(
	"default" => array(
		"label" => "Défaut",
		"folder" => "",
		"css-relative-url" => "",
		"path" => "",
		"checked" => $conf->get('core_theme') == "",
		"last-update" => 0
	)
);

foreach(glob(__DIR__.SLASH.'theme'.SLASH.'*'.SLASH.'app.json') as $themeFile){
	$path = dirname($themeFile);
	$theme = json_decode(file_get_contents($themeFile),true);
	$theme['folder'] = $path;

	$theme['css-relative-url'] = '/plugin/customiser/theme/'.basename($path).'/main.css';
	$theme['checked'] = $conf->get('core_theme') == $theme['css-relative-url'];
	$theme['last-update'] = filemtime($themeFile);
	$themes[] = $theme;
}
?>

<div class="row customiser">
	<div class="col-md-12">
		<br>
		<?php if($myUser->can('customiser', 'edit')) : ?>
			<div onclick="customiser_theme_save();" class="btn btn-success float-right"><i class="fas fa-check"></i> Enregistrer</div>
			
		<?php endif; ?>

		<h3>Choix du Thème</h3>
		<hr>
		<div class="row">
			<ul class="theme-cards">
				<?php foreach($themes as $theme): 

					$cover = ROOT_URL.'/plugin/customiser/';
					if(isset($theme['cover'])){
						$cover .= 'theme/'.basename($theme['folder']).'/'. $theme['cover'];
					}else{
						$cover .= 'img/default-cover.jpg';
					}
					?>
				<li >
					<div class="theme-card"  data-css="<?php echo ROOT_URL.$theme['css-relative-url']; ?>" onclick="customiser_preview(this);">
						<label>
							<img src="<?php echo $cover; ?>">
							<h5 class="mb-2 mt-2"><input type="radio" data-type="radio" <?php echo $theme['checked']?"checked='checked'":""; ?> value="<?php echo $theme['css-relative-url']; ?>" name="selected-theme"> <?php echo $theme['label']; ?></h5>
							<span class="text-muted"><i class="far fa-meh-blank"></i> <?php echo isset($theme['author']) ? $theme['author']['name'] : 'Auteur inconnu'; ?> - <i class="far fa-calendar-alt"></i> <?php echo relative_time($theme['last-update']); ?></span>
						</label>
					</div>
				</li>
				<?php endforeach; ?>
			</ul>
		</div>
	</div>
</div>

