<?php 
if (isset($_GET["tokenauth"])) {
	session_id($_GET["tokenauth"]);
}
date_default_timezone_set("Africa/tunis");
include "./kernel/app.php";
global $config;
$config = storge("config");
$urlbo = "/";
if (!empty($config["urlbo"])) {
	$urlbo .= $config["urlbo"]."/";
}
Route::dispatchAdd("¤",$urlbo);
app::includeDir("./Com");
if (!isset($_GET["api"])||$_GET["api"]=="auth"||$_GET["api"]=="getdataapiutilisateurs") {
	app::includeDir("./Views");
	app::includeDir("./mdl");
}
app::includeDir("./app");
function decodeurl($url){
	return preg_replace("/¤/i", $urlbo, $url);
}
if (isset($_GET["api"])) {
	app::runApi();
	exit;
}
view::is("Backoffice");
view::set("titre",$config["titre"]);
 
$menu = $config["menu"];
foreach ($menu as $page) {
	if (!empty($page["data"]["url"])) {
		Route::is(decodeurl($page["data"]["url"]),function() use ($page,$config){
			$q = http_build_query($_GET);
			header("location:$urlbo?url=".urlencode(decodeurl($page["data"]["url"]))."&$q");
			return ;
			$r = [];
			foreach ($_GET as $key => $value) {
				if (is_string($key)) {
					if (is_numeric($value)) {
						$r[$key] = [
							"hide" => true,
							"value" => ["id"=>$value]
						];
					}
				}
			}
			if (!empty($r)) {
				$r = "filter='".json_encode($r)."'";
			}else{
				$r = "";
			}
			?>
			<div style="display: flex;width: 100%;align-items: center;">
				<img src="<?= empty($page["data"]["icon"])?$config["logoprint"]:$page["data"]["icon"] ?>" style="margin-right: 15px;height: 40px;">
				<h1 style="flex: 1;margin: 0;font-size: 25px;"><?= $page["text"] ?></h1>
				<div style="display: flex;align-items: center;">
					<div style="flex:1;margin-right: 8px;text-align: right;color: #797979;">
						<div style="font-weight: bold;"><?= user::get("nom") ?></div>
						<div ><?= user::get("type") ?></div>
					</div>
					<img src="/public/logo/logo-11.png" style="height: 46px;">
				</div>
			</div>
			<hr style="width: 100%;margin-top: 3px;">
			<div style="flex:1;width: 100%;display: flex;flex-direction: column;margin:-15px 0;">
				
				<?php if (!empty($page["data"]["directive"])): ?>
					<?php if ((bool)$page["data"]["rules"][user::get("type")]["show"] || user::get("type")=="Administrateur"): ?>
						<<?= $page["data"]["directive"] ?> widget="mdl" <?= $r ?>  rules='<?= user::get("type")=="Administrateur"?"true":json_encode($page["data"]["rules"][user::get("type")]) ?>'></<?= $page["data"]["directive"] ?>>
					<?php else: ?>
						<div style="flex: 1;width: 100%;display: flex;align-items: center;justify-content: center;flex-direction: column;text-align: center;">
							
							<h2>Accès interdit</h2>
							<p>Vous avez pas le droit d'accès a cette page.</p>
						</div>
					<?php endif ?>
				<?php else: ?>
				<div style="flex: 1;width: 100%;display: flex;align-items: center;justify-content: center;flex-direction: column;text-align: center;">
					
					<h2>Page en cours de construction</h2>
					<p>Cette page est en cours de construction</p>
				</div>
				<?php endif ?>
			</div>
			<?php
		});
	}
}
Route::isJs(function(){
	ob_start();
	?>
	<div>
	</div>
	<?php
	$code=ob_get_clean();
	?>
	<script>
		app.directive('urldetect', [function () {
			return {
				restrict: 'EA',
				transclude : false,
				replace : false,
				template : <?= json_encode(preg_replace("/\t|\n|\r/i", "", $code)) ?>,
				scope : {
					
				},
				controller : function($scope){
					$("a").click(function(e){
						if((this.href || "") == "" || (this.href || "").match(/logout|\#/)) return;
						e.preventDefault();
						var u = (this.href || "").split(window.location.host).pop();
						$scope.$root.showpages = false;
						$scope.$root.url = u;
						setTimeout(function(){
							$scope.$root.showpages = true;
							$scope.$apply();
						},50)
						
						$scope.$apply();
					});
				},
				link: function ($scope, iElement, iAttrs) {
					setTimeout(function(){
						$scope.$root.showpages = true;
						$scope.$apply();
					},100);	
					setTimeout(function(){
						window.history.pushState("", "", '/');
						$scope.$root.hideloading = true;
						$scope.$apply();
					},200);	
				}
			};
		}]);
	</script>
	<?php
});
Route::is($urlbo,function() use ($menu){
	View("Backoffice-one");
?>
<div style="flex:1;display:flex;flex-direction: column;align-items: center;justify-content: center;background: rgba(255,255,255,0.5)" ng-hide="$root.showpages">
	
	<md-progress-circular md-mode="indeterminate"  md-diameter="48"></md-progress-circular> 
</div>
  <urldetect ng-init="$root.showpages = false;$root.url = '<?= preg_match("/^\//i",$_GET["url"])?$_GET["url"]:"/".$_GET["url"] ?>'"></urldetect>
  <div ng-if="$root.showpages" ng-cloak  style="flex:1;display:flex;flex-direction: column;">
  <div ng-switch="$root.url" style="flex:1;display:flex;flex-direction: column;" ng-cloak>
<div ng-switch-when="/" style="flex:1;display:flex;flex-direction: column;" >
			<div style="display: flex;width: 100%;align-items: center;">
				<img src="<?= empty($page["data"]["icon"])?$config["logoprint"]:$page["data"]["icon"] ?>" style="margin-right: 15px;height: 40px;">
				<h1 style="flex: 1;margin: 0;font-size: 25px;"><?= $page["text"] ?></h1>
				<div style="display: flex;align-items: center;">
					<div style="flex:1;margin-right: 8px;text-align: right;color: #797979;">
						<div style="font-weight: bold;"><?= user::get("nom") ?></div>
						<div ><?= user::get("type") ?></div>
					</div>
					<img src="/public/logo/logo-11.png" style="height: 46px;">
				</div>
			</div>
			<hr style="width: 100%;margin-top: 3px;">
			<div style="flex:1;width: 100%;display: flex;flex-direction: column;">
				<pageacc></pageacc>
			</div>
</div>
<?php
foreach ($menu as $page) {
	if (!empty($page["data"]["url"])) {
 ?>
 <div  style="flex:1;display:flex;flex-direction: column;" ng-switch-when="<?= preg_match("/^\//i", $page["data"]["url"])?$page["data"]["url"]:"/".$page["data"]["url"] ?>">
 <?php
			$r = [];
			foreach ($_GET as $key => $value) {
				if (is_string($key)) {
					if (is_numeric($value)) {
						$r[$key] = [
							"hide" => true,
							"value" => ["id"=>$value]
						];
					}
				}
			}
			if (!empty($r)) {
				$r = "filter='".json_encode($r)."'";
			}else{
				$r = "";
			}
			?>
			<div style="display: flex;width: 100%;align-items: center;">
				<img src="<?= empty($page["data"]["icon"])?$config["logoprint"]:$page["data"]["icon"] ?>" style="margin-right: 15px;height: 40px;">
				<h1 style="flex: 1;margin: 0;font-size: 25px;"><?= $page["text"] ?></h1>
				<div style="display: flex;align-items: center;">
					<div style="flex:1;margin-right: 8px;text-align: right;color: #797979;">
						<div style="font-weight: bold;"><?= user::get("nom") ?></div>
						<div ><?= user::get("type") ?></div>
					</div>
					<img src="/public/logo/logo-11.png" style="height: 46px;">
				</div>
			</div>
			<hr style="width: 100%;margin-top: 3px;">
			<div style="flex:1;width: 100%;display: flex;flex-direction: column;margin:-15px 0;">
				
				<?php if (!empty($page["data"]["directive"])): ?>
					<?php if ((bool)$page["data"]["rules"][user::get("type")]["show"] || user::get("type")=="Administrateur"): ?>
						<<?= $page["data"]["directive"] ?> widget="mdl" <?= $r ?> rules='<?= user::get("type")=="Administrateur"?"true":json_encode($page["data"]["rules"][user::get("type")]) ?>'></<?= $page["data"]["directive"] ?>>
					<?php else: ?>
						<div style="flex: 1;width: 100%;display: flex;align-items: center;justify-content: center;flex-direction: column;text-align: center;">
							
							<h2>Accès interdit</h2>
							<p>Vous avez pas le droit d'accès a cette page.</p>
						</div>
					<?php endif ?>
				<?php else: ?>
				<div style="flex: 1;width: 100%;display: flex;align-items: center;justify-content: center;flex-direction: column;text-align: center;">
					
					<h2>Page en cours de construction</h2>
					<p>Cette page est en cours de construction</p>
				</div>
				<?php endif ?>
			</div>
			<?php
 ?>
 </div>
 <?php
	}
}
?>
 </div>
 </div>
<?php
});

app::run();