<?php
	declare(strict_types = 1);
	namespace Kimendisch\Sbgg_Jetzt;
	use Flake\Lang;
	use Flake\Lang_Dict;
	use Flake\Page;
	use Flake\Project;
	use Flake\Excuse;
	use Flake\Csrf;
	
	// HANDLE AUTHENTICATION //
	require("./page/admin/auth_handler.php");
	
	
	// LOAD CONTENT //
	// get content name from url param
	$content_name = Project::param("content");
	
	// check whether this content exists
	if(!in_array($content_name, Newsletter::content_list())){
		Excuse::show("not_found");
	}
	
	// load content data
	$content = Newsletter::content_file_read(name: $content_name);
	
	
	// MAYBE PROVIDE PREVIEW HTML //
	if(isset($_GET["preview"])){
		// validate language
		$language = $_GET["preview"];
		if(!in_array($language, ["de", "en"])){
			http_response_code(400);
			echo("invalid preview language requested");
			die();
		}
		
		// render preview
		$preview = Newsletter::content_render_preview(content: $content, language: $language);
		
		// output preview
		echo($preview);
		die();
	}
	
	
	// 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::js(__DIR__ . "/iframe_magic.js");
	Page::js(__DIR__ . "/send_one.js");
	Page::js(__DIR__ . "/send_all.js");
	
	Page::font("ubuntu");
	Page::font("tabler");
?>





<div class="page-container full-page">
	<div class="page">
		<div class="section">
			<div class="content">
				<div class="button-list align-left">
					<a href="/admin/newsletter" class="button on-bg">
						<span class="icon ti ti-arrow-left"></span>
						<span class="text">Go Back</span>
					</a>
					<a href="/admin" class="button on-bg">
						<span class="icon ti ti-home"></span>
						<span class="text">Go Home</span>
					</a>
				</div>
			</div>
		</div>
		
		<div class="section">
			<div class="header">
				<span class="icon ti ti-news"></span>
				<span class="text">Preview</span>
			</div>
			<div class="content full-page rows">
				<div class="box">
					<span class="extra"><i class="ti ti-world"></i> Language: <span class="white">DE</span></span>
					<div class="preview-container">
						<iframe src="?preview=de" width="576" scrolling="no" onload="iframe_resize(this)"></iframe>
					</div>
				</div>
				
				<div class="box">
					<span class="extra"><i class="ti ti-world"></i> Language: <span class="white">EN</span></span>
					<div class="preview-container">
						<iframe src="?preview=en" width="576" scrolling="no" onload="iframe_resize(this)"></iframe>
					</div>
				</div>
			</div>
		</div>
		
		<div class="section">
			<div class="header">
				<span class="icon ti ti-mail-fast"></span>
				<span class="text">Delivery</span>
			</div>
			<div class="content rows">
				<div class="box">
					<span class="title">Send to One</span>
					
					<div id="newsletter-send-one-form-container" class="form-container">
						<div id="newsletter-send-one-form" class="form">
							<div class="key-value-pair">
								<div class="key">
									<span class="ti ti-at"></span>
								</div>
								<div class="value-list">
									<div class="inputwrapper">
										<input id="newsletter-send-one-form-mail-address" class="value" type="text" placeholder="Member Mail Address" autocomplete="off" />
									</div>
								</div>
							</div>
							
							<input id="newsletter-send-one-form-content-name" type="hidden" value="<?= $content_name ?>" />
							<input id="newsletter-send-one-form-csrf-token" type="hidden" value="<?= Csrf::token() ?>" />
							
							<button id="newsletter-send-one-form-submit" class="button primary">
								<span class="text">Send to One</span>
								<span class="icon ti ti-chevron-right"></span>
							</button>
						</div>
						
						<div id="newsletter-send-one-form-feedback" class="form-feedback gone">
							<div id="newsletter-send-one-form-feedback-wait" class="form-feedback-wait centertext gone">
								<span class="icon spinning ti ti-loader-2"></span>
								<span class="text">Adding job to queue</span>
							</div>
							<div id="newsletter-send-one-form-feedback-success" class="form-feedback-success centertext gone">
								<span class="icon ti ti-check"></span>
								<span class="text">Job successfully queued</span>
							</div>
							<div id="newsletter-send-one-form-feedback-failure" class="form-feedback-failure centertext gone">
								<span class="icon ti ti-x"></span>
								<span class="text">Queueing job failed</span>
							</div>
							<div id="newsletter-send-one-form-feedback-failure-no-member" class="form-feedback-failure centertext gone">
								<span class="icon ti ti-x"></span>
								<span class="text">Not a newsletter member</span>
							</div>
						</div>
					</div>
				</div>
				
				<div class="box danger">
					<span class="title">Send to All (<?= Newsletter::member_count() ?>)</span>
					
					<div id="newsletter-send-all-form-container" class="form-container">
						<div id="newsletter-send-all-form" class="form">
							<div class="key-value-pair">
								<div class="key">
									<span class="ti ti-shield"></span>
								</div>
								<div class="value-list">
									<span id="newsletter-send-all-form-safe-code" class="value select-none">&nbsp;</span>
									<div class="inputwrapper">
										<input id="newsletter-send-all-form-safe-code-repeat" class="value" type="text" placeholder="Safe Code" autocomplete="off" />
									</div>
								</div>
							</div>
							
							<input id="newsletter-send-all-form-content-name" type="hidden" value="<?= $content_name ?>" />
							<input id="newsletter-send-all-form-csrf-token" type="hidden" value="<?= Csrf::token() ?>" />
							
							<button id="newsletter-send-all-form-submit" class="button primary">
								<span class="text">Send to All</span>
								<span class="icon ti ti-chevron-right"></span>
							</button>
						</div>
						
						<div id="newsletter-send-all-form-feedback" class="form-feedback gone">
							<div id="newsletter-send-all-form-feedback-wait" class="form-feedback-wait centertext gone">
								<span class="icon spinning ti ti-loader-2"></span>
								<span class="text">Adding jobs to queue</span>
							</div>
							<div id="newsletter-send-all-form-feedback-success" class="form-feedback-success centertext gone">
								<span class="icon ti ti-check"></span>
								<span class="text">Jobs successfully queued</span>
							</div>
							<div id="newsletter-send-all-form-feedback-failure" class="form-feedback-failure centertext gone">
								<span class="icon ti ti-x"></span>
								<span class="text">Queueing jobs failed</span>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>



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