<!DOCTYPE html>
<html lang="en-us">
	<head>
		<meta http-equiv="content-type" content="text/html; charset=utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<meta name="Description" content="mini-buildd ({{ mbd.daemon.get_title }})">
		<link rel="icon" type="image/svg+xml" href="{% static 'mini_buildd.svg' %}">

		<link rel="stylesheet" type="text/css" href="{% static 'javascript/jquery-datatables/css/jquery.dataTables.min.css' %}">
		<link rel="stylesheet" type="text/css" href="{% static 'mini_buildd.css' %}">

		<!-- JavaScript Setup -->
		<script>
			// On js error, display alert. Otherwise functionality might be silently missing for joe netizen.
			window.onerror = function(msg, url, line, col, err)
			{
				alert("JavaScript error detected. Please debug, patch and report ;). Or consider disabling JavaScript.");
			};
		</script>
		<script src="{% static 'javascript/jquery/jquery.min.js' %}"></script>
		<script src="{% static 'javascript/jquery-datatables/jquery.dataTables.min.js' %}"></script>
		<script src="{% static 'javascript/clipboard.js/clipboard.js' %}"></script>

		<title>mini-buildd ({{ mbd.daemon.get_title }}): {% block title %}{% endblock %}</title>
	</head>

	<body class="mbd">
		<header class="mbd-grid-lcr {{ mbd.daemon.is_alive|yesno:',mbd-off' }}">
			<nav>
				<ul>
					<li class="{% if request.path == mbd.config.URIS.home.view.uri %}mbd-body-menu-active{% endif %}">
						<a href="{% url 'home' %}">
							<img class="mbd-icon" src="{% static 'mini_buildd.svg' %}" alt="Home" title="Home: {{ mbd.daemon.get_title }}">
						</a>
					</li>
					{% for name in mbd.config.MAIN_MENU_LEFT %}
						{% include "mini_buildd/includes/main_menu_item.html" %}
					{% endfor %}
				</ul>
			</nav>

			<nav>
				<ul>
					<li>{% mbd_api "ls" %}</li>
				</ul>
			</nav>

			<nav>
				<ul>
					{% for name in mbd.config.MAIN_MENU_RIGHT %}
						{% include "mini_buildd/includes/main_menu_item.html" %}
					{% endfor %}
					<li>&vert;</li>
					<li>
						<div class="mbd-account {{ user.is_authenticated|yesno:',mbd-dim' }}">{{ user.username|default:"Logged Off" }}▼</div>
						<ul>
							{% if user.is_authenticated %}
								<li>{% mbd_accounts "logout" name="Logout" %}</li>
								<li>{% mbd_accounts "profile" name="Profile" %}</li>
								<li>{% mbd_accounts "password_change" name="Change Password" %}</li>
								<li>{% mbd_accounts "login" name="Change User" %}</li>
							{% else %}
								<li>{% mbd_accounts "login" name="Login" %}</li>
								<li>{% mbd_accounts "register" name="Register new account" %}</li>
								<li>{% mbd_accounts "password_reset" name="Reset password" %}</li>
							{% endif %}
						</ul>
					</li>
				</ul>
			</nav>
		</header>

		<noscript class="mbd-warning">JavaScript not available: <b>Usability limited</b></noscript>

		<main>
			<div id="api-popup" class="mbd-hidden">
				<div>
					<div>
						<header class="mbd-grid-lcr mbd-body-header">
							<nav>
								<ul>
									<li>
										<a href="{% url 'home' %}">
											<img class="mbd-icon" src="{% static 'mini_buildd.svg' %}" alt="Home" title="Home: {{ mbd.daemon.get_title }}">
										</a>
									</li>
								</ul>
							</nav>
							<nav>
							</nav>
							<nav>
								<ul>
									<li>
										<a href="{{ request.get_full_path }}">
											<img class="mbd-icon" src="{% static "icons/actions/view-refresh.svg" %}" alt="Reload" title="Reload Parent Page">
										</a>
									</li>
									<li>
										<a role="button" onclick="$('#api-popup').addClass('mbd-hidden');">
											<img class="mbd-icon" src="{% static "icons/actions/window-close.svg" %}" alt="Close" title="Close Popup">
										</a>
									</li>
								</ul>
							</nav>
						</header>
						<section id="api-popup-content"></section>
					</div>
				</div>
			</div>

			<header class="mbd-grid-lcr">
				<h1 title="{% block subtitle %}{% endblock %}">{% block page_title %}{% endblock %}</h1>
				<nav>
					<ul>
						{% if path %}
							<li>
								<a href="{% url name %}">
									<img class="mbd-icon" src="{% static 'icons/actions/go-top.svg' %}" alt="Top">
								</a>
							</li>
							{% url name path|mbd_parent as up %}
							{% if up %}
								<li>
									<a href="{{ up }}">
										<img class="mbd-icon" src="{% static 'icons/actions/go-up.svg' %}" alt="Up">
									</a>
								</li>
							{% endif %}
						{% endif %}
						{% if view.mbd_dirview %}
							{% if not view.mbd_isfile %}
								{% url route path as view_uri %}
								{% if view_uri %}
									<li>
										<a href="{{ view_uri }}">
											<img class="mbd-icon" src="{% static 'icons/mimetypes/text-html.svg' %}" alt="htmlview" title="Switch To Standard View">
										</a>
									</li>
								{% endif %}
							{% endif %}
						{% else %}
							{% url route|add:'-dir' path as dir_uri %}
							{% if dir_uri %}
								<li>
									<a href="{{ dir_uri }}">
										<img class="mbd-icon" src="{% static 'icons/places/folder.svg' %}" alt="dirview" title="Switch to Directory View">
									</a>
								</li>
							{% endif %}
						{% endif %}
						{% block menu_c %}{% endblock %}
					</ul>
				</nav>
				<nav>
					<ul>
						{% block menu_r %}{% endblock %}
						{% if view.mbd_since %}
							<li>
								<form action="{{ request.path }}" method="get">
									<input name="since" title="Since" type="date" value="{{ view.mbd_since|date:"Y-m-d" }}" onchange="this.form.submit();" required>
									<noscript>
										<input type="submit" value="Update">
									</noscript>
								</form>
							</li>
						{% endif %}
					</ul>
				</nav>
			</header>
			<section>
				{% block section %}{% endblock %}
			</section>
		</main>

		<footer class="mbd-grid-lcr">
			<div title="Version of mini-buildd">{{ mbd.version }}</div>
			<nav>
				<ul>
					<li title="Key id and fingerprint of instance">&raquo; <strong>{{ mbd.daemon.get_title }}</strong> ({{ mbd.daemon.model.mbd_gnupg.get_first_sec_key }})</li>
					<li>{% mbd_api_popup "pub_key" %}</li>
					<li>{% mbd_api_popup "sources_list" %}</li>
					<li>{% mbd_api_popup "dput_conf" %}</li>
				</ul>
			</nav>
			<nav>
				<ul>
					<li><a href="{% url "sitemap" %}">Sitemap</a></li>
					<li><a href="{{ mbd.config.URIS.homepage.view.uri }}">Homepage➚</a></li>
				</ul>
			</nav>
		</footer>
		{# <pre>{% debug %}</pre> #}

		<!-- JavaScript Init -->
		<script>
			// Activate clipboard support for class "mbd-clipboard"
			new ClipboardJS('.mbd-clipboard');

			// Show 'wait' cursor on load() (or other ajax functions)
			$(document)
				.ajaxStart(function(){$(document.body).css({'cursor': 'wait'});})
				.ajaxStop(function(){$(document.body).css({'cursor': 'default'});});

			// alert on next event (test code, for reference only).
			function mbd_event()
			{
				$.ajax({url: "/static/events.attach", dataType: "json", complete: function(xhr, status)
					{
						console.log("EVENT", xhr);
						alert(JSON.stringify(xhr.responseJSON, null, 2));
				}});
			}

			function mbd_api_call(form, command, needs_confirmation)
			{
				const api_uri = "/mini_buildd/api/";

				// Discard empty values mainly for convenience (shorter uris, nicer display) -- args w/ empty values are treated as 'not given' by API anyway.
				let args = $(form).serializeArray().filter(kv => kv.name != "output" && kv.name != "confirm" && kv.value != "");
				let args_serialized = jQuery.param(args);
				let args_text = "";
				for (let kv in args) { args_text += "  " + args[kv].name + "=" + args[kv].value + "\n"; }

				if (needs_confirmation == "False" || confirm("Please Confirm API Call: " + command + "\n" + args_text))
				{
					let get_uri = api_uri + command + "/?" + args_serialized;
					$("#api-popup-content").html("<details><summary>Running API Call <b>" + command + "</b></summary><pre>" + args_text + "</pre></details><progress style='width: 100%'></progress>");
					$("#api-popup-content").load(get_uri + "&confirm=" + command + "&output=snippet", function (response, status, xhr) { if (status == "error") { $("#api-popup-content").html(response);};});
					$("#api-popup").removeClass("mbd-hidden")
				};
				return false;  // Avoid non-js 'action=' to be performed
			}
		</script>
	</body>
</html>
