<?php
	declare(strict_types = 1);
	namespace Kimendisch\Sbgg_Jetzt;
	use Flake\Url_Redirect;
	use Flake\Request;
	use Flake\Lang;
	use Flake\Lang_Dict;
	use Flake\Page;
	use Flake\Cookieaccept;
	use Flake\Csrf;
	
	// CHECK AUTHENTICATION //
	// redirect to start page when logged in
	$login = $_SESSION[__NAMESPACE__]["admin"]["login"] ?? null;
	if($login === true){
		Url_Redirect::location("http" . (Request::has_ssl() ? "s" : "") . "://" . Request::domain_raw_full() . "/admin");
		die();
	}
	
	
	// HANDLE LOGIN //
	require(__DIR__ . "/login_handler.php");
	
	
	// LANGUAGE MANAGER //
	// hack: fake get param from constant
	$_GET["lang"] = "en";
	
	// initialize
	$lang = new Lang(list: ["de", "en"], default: "en");
	
	// load dict
	$dict = new Lang_Dict($lang);
	require("./page/strings.php");
	
	
	// PAGE INIT //
	Page::start();
	
	Page::title("SBGG.jetzt - Admin Area");
	Page::icon("./asset/logo-256.png");
	
	Page::lang($lang->get());
	Page::viewport(scale: 1, zoom: true);
	
	Page::robots(index: false, follow: false);
	Page::author("Kim Endisch");
	
	Page::$head["analytics"] = '<script defer data-domain="sbgg.jetzt" src="https://analytics.tjdev.de/js/script.js"></script>';
	Page::css("./page/start/style.css");
	Page::css(__DIR__ . "/style.css");
	
	Page::font("ubuntu");
	Page::font("tabler");
?>





<?php if(!Cookieaccept::is_accepted()){ ?>
	<div class="cookie-notice-required">
		<div class="box important">
			<span class="title"><i class="ti ti-cookie"></i>Cookies</span>
			
			<div class="description">
				<span>This page needs cookies to function correctly.</span>
				<span>Cookies are only used for required purposes.</span>
				<span>You can read more about this in our <a href="<?= $dict->get("link_privacy_policy") ?>" target="_blank"><?= $dict->get("text_privacy_policy") ?> <i class="ti ti-external-link"></i></a>.</span>
			</div>
			
			<div class="button-list">
				<a class="button" href="?cookieaccept=1">
					<span class="icon ti ti-check"></span>
					<span class="text">Accept</span>
				</a>
			</div>
		</div>
	</div>
<?php } ?>



<div class="page-container">
	<div class="page">
		<div class="section">
			<div class="content rows">
				<div id="login" class="box">
					<span class="title">Admin Area</span>
					
					<form id="login-form" class="form" method="post" action="">
						<div class="key-value-pair">
							<div class="key">
								<span class="ti ti-key"></span>
							</div>
							<div class="value-list">
								<input id="login-form-token" class="value" type="password" name="token" placeholder="Authentication Token" autocomplete="off" required />
							</div>
						</div>
						
						<input type="hidden" name="csrf_token" value="<?= Csrf::token() ?>" />
						
						<button id="login-form-submit" class="button primary">
							<span class="text">Login</span>
							<span class="icon ti ti-chevron-right"></span>
						</button>
					</form>
					
					<?php if(isset($_GET["login_failure"])){ ?>
						<span id="login-feedback-negative">Login failed</span>
					<?php } ?>
				</div>
			</div>
		</div>
	</div>
</div>



<?php require(dirname(__DIR__) . "/footer.php"); ?>
