<?php 
$urlbo = "/";
global $config;
if (!empty($config["urlbo"])) {
	$urlbo .= $config["urlbo"]."/";
}
api::auth(function($data) use($urlbo){
	if (empty($data["login"])) {
		avs::no("login","Veuillez saisir votre login");
	}else{
		if ($row = db::row("SELECT * FROM (SELECT id, login, password, nom, type, etat FROM ¤USERS union SELECT id, login, password, titre as nom, type, etat FROM ¤CLIENT) u WHERE login=:login ",[":login"=>$data["login"]])) {
			avs::ok("login");
			if (empty($data["pass"])) {
				avs::no("pass","Veuillez saisir la mot de passe");
			}else{
				if ($row["password"] === $data["pass"] ) {
					avs::ok("pass");
					if (!(bool)$row["etat"] && $row["type"] != "Administrateur") {
						app::apiRep("alert","Votre compte a été suspendu!");
					}else{
						// user::connect($row["login"],$row["password"]);
						$_SESSION["user"] = $row;
						global $config;
						$url = $urlbo;
						if(!empty($config["defaulturl"])){
							$url = $config["defaulturl"];
						}
						app::location($url);
					}
				}else{
					avs::no("pass","Mot de passe incorrect !");	
				}
			}
		}else{
			avs::no("login","Login invalid !");	
		}
	}
});
Route::is("$urlbo*",function() use($urlbo){
	if(Route::is($urlbo."auth") || Route::is("api") || Route::is("generateapp") || Route::is($urlbo."web/*") || Route::is("__*")) return ;
	if (empty($_SESSION["user"])) {
		app::location($urlbo."auth");
		exit();
	}else{
		View("Backoffice");
	}
});
Route::is(["logout",$urlbo."logout"],function($data) use($urlbo){
	user::logout();
	app::location($urlbo."auth");
});
Route::is($urlbo."auth",function($data) use($urlbo){
if (user::get()) {
	app::location($urlbo);
	return;
}
View::reset();
View::is("global");
global $config;
?>
<div style="height: 100vh;width: 100%;position: relative;display: flex;align-items: center;justify-content: center;background:url(<?= $config["bgAuth"] ?>);width: 100%;background-size: 100% 100%;">
	<div class="panel panel-primary"  style="max-width: 300px;min-width: 300px;margin: 10px;margin-top: -10%;background: rgba(255, 255, 255, 0.31);">
		<!-- <div class="panel-heading"> <div class="panel-title" style="text-align: center;">Authentification</div> </div> -->
		<div style="" class="panel-body" >
			<div style="display: flex;justify-content:center;align-items: center;padding: 20px;margin-bottom: 22px;">
				<img src="<?= $config["logoprint"] ?>" style="max-width: 80%;">
			</div>
			<div style="display: flex;width: 100%;flex-direction: column;">
				<div id="formauthloading" style="display: flex;align-items: center;justify-content: center;height: 100px;width:100%;">
						<md-progress-circular md-mode="indeterminate"></md-progress-circular>
				</div>
				<form id="formauth" class="form-horizontal" role="form" action="auth" method="POST" style="display: none;align-items: center;width: 100%;">
					<div style="width: 100%;">
						<div style="margin-bottom: 25px">
							<div class="input-group avs">
							    <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
							    <input id="login-username" type="text" class="form-control" name="login" value="" placeholder="Login" autofocus>
							</div>
						</div>
						<div style="margin-bottom: 25px">
							<div class="input-group avs">
								<span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
								<input id="login-password" type="password" class="form-control" name="pass" placeholder="Mot de passe">
							</div>
						</div>
						<div style="margin-top:10px" class="form-group">
							<div class="col-sm-12 controls">
								<button style="display: block;width: 100%;" type="submit" class="btn btn-primary">Connecter</button>
							</div>
						</div>  
					</div>
				</form> 
			</div>
		</div>                     
	</div>  
</div>
<span style="position: fixed;right: 15px;bottom: 15px;z-index: 1000;">Propulsé par <b>Mosaiqueweb</b></span>
<script>
	setTimeout(function(){
		$("#formauth").slideDown("fast");
		$("#formauthloading").slideUp("fast");
	},2000);
</script>
<?php
});