{% from '_macros.html' import page_heading with context %}

{% block doc -%}
	<!DOCTYPE html>
	<html{% block html_attribs %} lang="en"{% endblock html_attribs %}>
	{%- block html %}
		<head>
			{%- block head %}
				<meta charset="utf-8">
				<meta name="description" content="Antergos Automated Build Server">
				<meta name="author" content="Antergos Developers">
				<link rel="shortcut icon" href="/static/images/fav.png">
				<title>{% block pagetitle %}{% endblock pagetitle %} | AntBS</title>

				{%- block metas %}
					<meta name="viewport" content="width=device-width, initial-scale=1.0">
				{%- endblock metas %}

				{% if '/package/' in request.path %}
					<link rel="stylesheet" type="text/css" href="/static/css/bootstrap-switch.css"/>
				{% endif %}

				{%- block styles %}
					<link href='//fonts.googleapis.com/css?family=Open+Sans:400,300,600,400italic,700,800' rel='stylesheet' type='text/css'>
					<link href='//fonts.googleapis.com/css?family=Raleway:400,300,200,100,700' rel='stylesheet' type='text/css'>
					<!-- Bootstrap core CSS -->
					<link href="/static/js/bootstrap/dist/css/bootstrap.css" rel="stylesheet">
					<link rel="stylesheet" type="text/css" href="/static/js/jquery.gritter/css/jquery.gritter.css"/>
					<link rel="stylesheet" href="/static/fonts/font-awesome-4/css/font-awesome.min.css">
					<link rel="stylesheet" type="text/css" href="/static/js/jquery.datatables/bootstrap-adapter/css/datatables.css"/>

					<link href="/static/css/skin-blue.css" rel="stylesheet"/>
					<link href="/static/css/monokai.css" rel="stylesheet"/>
					<link href="/static/js/jquery.nanoscroller/nanoscroller.css" rel="stylesheet"/>
					<link href="/static/css/style.css" rel="stylesheet"/>
				{%- endblock styles %}
			{%- endblock head %}
		</head>
		<body{% block body_attribs %} class="animated {{ self|tpl_name()|replace('.html', '') }}"{% endblock body_attribs %}>

		{% block body -%}
			{% include '_partials/_antergos_links_bar.html' %}
			<!-- WRAPPER -->
			<div id="cl-wrapper">

				{% block sidebar %}
					{% include '_partials/_sidebar.html' %}
				{% endblock sidebar %}

				<!-- MAIN CONTAINER -->
				<div class="container-fluid" id="pcont">

					{% block topbar %}
						{% include '_partials/_topbar.html' %}
					{%- endblock topbar %}

					{% block maincontainer %}
						<div id="main" class="cl-mcont">

							{% block heading %}
								{{ page_heading(self.pagetitle()) }}
							{% endblock heading %}

							<!-- TOP SECTION -->
							<div class="container-fluid">
								{% block topcontent -%} {%- endblock topcontent %}
							</div> <!-- END TOP SECTION -->

							<!-- BOTTOM SECTION -->
							<div class="container-fluid">
								{% block bottomcontent -%} {%- endblock bottomcontent %}
							</div> <!-- END BOTTOM SECTION -->

						</div>
					{% endblock maincontainer %}
				</div> <!-- END MAIN CONTAINER -->
			</div> <!-- END WRAPPER -->

			{% block scripts %}
				<script src="/static/js/jquery.js"></script>
				<script src="/static/js/jquery.cookie/jquery.cookie.js"></script>
				<script src="/static/js/jquery.pushmenu/js/jPushMenu.js"></script>
				<script type="text/javascript" src="/static/js/moment-with-locales.min.js"></script>
				<script type="text/javascript" src="/static/js/moment-timezone-with-data-2010-2020.min.js"></script>
				<script>
					(function ($) {
						$(document).ready(function () {
							setTimeout(function () {
								if ($('#cl-wrapper').hasClass('sb-collapsed')) {
									$('body').addClass('sb-collapsed');
									$('#top-bar div:last-child').width($(window).width() - 105);
								} else {
									$('body').removeClass('sb-collapsed');
									$('#top-bar div:last-child').width($(window).width() - 245);
								}
							}, 800);
						});
						$('#sidebar-collapse').click(function () {
							setTimeout(function () {
								if ($('#cl-wrapper').hasClass('sb-collapsed')) {
									$('body').addClass('sb-collapsed');
									$('#top-bar div:last-child').width($(window).width() - 105);
								} else {
									$('body').removeClass('sb-collapsed');
									$('#top-bar div:last-child').width($(window).width() - 245);
								}
							}, 200);
						});
						$(window).load(function () {
							$('#main').height($('#main').parent().height());
						})
					})(jQuery);
				</script>

				<script type="text/javascript" src="/static/js/jquery.ui/jquery-ui.js"></script>
				<script type="text/javascript" src="/static/js/jquery.nanoscroller/jquery.nanoscroller.min.js"></script>
				<script src="/static/js/bootstrap/dist/js/bootstrap.min.js"></script>
				<script type="text/javascript" src="/static/js/jquery.gritter/js/jquery.gritter.js"></script>
				<script type="text/javascript" src="/static/js/behaviour/core.js"></script>
				<script type="text/javascript" src="/static/js/jquery.datatables/jquery.datatables.min.js"></script>
				<script type="text/javascript" src="/static/js/jquery.datatables/bootstrap-adapter/js/datatables.js"></script>
				<script type="text/javascript" src="/static/js/jquery.datatables/bootstrap-adapter/js/dataTables.bootstrap.js"></script>
				<script type="text/javascript" src="/static/js/jquery.datatables/media/js/num-html.js"></script>
				<script>
					//sets up numeric sorting of links
					jQuery(document).ready(function () {

						{% if current_user.is_authenticated and 'repo_pkgs' in request.path %}
							$('.content').on('click', '.repo_actions a', function (e) {
								e.preventDefault();
								jQuery(this).addClass('clicked');
								var data = {
									pkg: jQuery(this).attr('data-pkg'),
									result: jQuery(this).attr('id'),
									dev: '{{ user.username }}'
								};
								jQuery.ajax({
									type: 'POST',
									url: '/api/ajax',
									data: JSON.stringify(data, null, '\t'),
									contentType: 'application/json;charset=UTF-8',
									dataType: 'json',
									success: function (result) {
										console.log(result);
										var res = $('.clicked').attr('id') == 'passed' ? 'span.passed' : 'span.failed';
										if (result['msg'] == 'ok') {
											jQuery('.clicked').parents('tr').remove();
										} else {
											alert('Unable to save result. Server Error Msg:\n' + result['msg']);
										}
									}

								});
							});

							$( '.content' ).on( 'click', '.manage a', function ( e ) {
								e.preventDefault();
								jQuery( this ).addClass( 'clicked' );
								{#var data = {
									pkg:    jQuery( this ).attr( 'data-pkg' ),
									result: jQuery( this ).attr( 'id' ),
									dev:    '{{ user.username }}'
								};
								jQuery.ajax( {
									type:        'POST',
									url:         '/api/ajax',
									data:        JSON.stringify( data, null, '\t' ),
									contentType: 'application/json;charset=UTF-8',
									dataType:    'json',
									success:     function ( result ) {
										console.log( result );
										var res = $( '.clicked' ).attr( 'id' ) == 'passed' ? 'span.passed' : 'span.failed';
										if ( result['msg'] == 'ok' ) {
											jQuery( '.clicked' ).parents( 'tr' ).remove();
										} else {
											alert( 'Unable to save result. Server Error Msg:\n' + result['msg'] );
										}
									}

								} );#}
							} );
						{% endif %}

						{% if current_user.is_authenticated %}
							$('#do_iso_release_confirm').on('click', function () {
								$.getJSON('/api/ajax?do_iso_release=True', function (data) {
									console.log(data);
								});
							});
							$('#reset_build_queue').on('click', function () {
								var $icon = $(this).find('i');
								$icon.addClass('fa-spin');
								$.getJSON('/api/ajax?reset_build_queue=True', function (data) {
									console.log(data);
									setTimeout(function ($the_icon) {
										$the_icon.removeClass('fa-spin');
									}, 2000, $icon);
								});
							});
							$('#do_update_repos').on('click', function() {
								var $icon = $(this).find('i');
								$icon.toggleClass('fa-database fa-spin');
								$.getJSON('/api/ajax?update_repos=True', function (data) {
									console.log(data);
									setTimeout(function ($the_icon) {
										$the_icon.toggleClass('fa-database fa-spin');
									}, 2000, $icon);
								});
							});
						{% endif %}

						{% with messages = get_flashed_messages(with_categories=true) %}
							{% if messages %}
								{% for category, message in messages %}
									setTimeout(function () {
										$.gritter.add({
											title: '{{ category|upper() }}',
											text: '{{ message }}',
											class_name: "clean{% if category == 'error' %}error{% else %}'info'{% endif %}{{ category }}",
											sticky: "{% if category == 'error' %}true{% else %}false{% endif %}"
										});
									}, {{ loop.index * 500 }});
								{% endfor %}
							{% endif %}
						{% endwith %}


						jQuery('#main pre').addClass('pre-scrollable');
						jQuery('.not-idle').show();
						var sse = new EventSource('/api/get_status');
						sse.addEventListener("status", function (message) {
							var current = jQuery('#status_bar_pre').text();
							if (message.data !== current) {
								jQuery('.idle').addClass('not-idle').removeClass('idle');
								jQuery('.fa-dot-circle-o').addClass('fa-spinner fa-spin').removeClass('fa-dot-circle-o');
								jQuery('.fa-spinner').addClass('fa-spin');
								jQuery('#status_bar').show();
								jQuery('#status_bar_pre').text(message.data);
								current = jQuery('#status_bar_pre').text();
							}
							if (message.data == 'Idle' || current == 'Idle') {

								jQuery('.status').find('i').removeClass('fa-spinner fa-spin').addClass('fa-dot-circle-o');
								jQuery('.fa-spin').removeClass('fa-spin');
								jQuery('#status_bar').hide();

							}
						});

					});

					jQuery(window).load(function () {
							jQuery('[data-toggle="tooltip"]').tooltip();

							{% if '/' != request.path %}
								jQuery('.loading').fadeIn();
								if (jQuery('table.no-border').length && !jQuery('#noDt').length) {
									jQuery.fn.dataTableExt.oSort['num-html-asc'] = function (a, b) {
										var x = a.replace(/<.*?>/g, "");
										var y = b.replace(/<.*?>/g, "");
										x = parseFloat(x);
										y = parseFloat(y);
										return ((x < y || isNaN(y) ) ? -1 : ((x > y || isNaN(x)) ? 1 : 0));
									};

									jQuery.fn.dataTableExt.oSort['num-html-desc'] = function (a, b) {
										var x = a.replace(/<.*?>/g, "");
										var y = b.replace(/<.*?>/g, "");
										x = parseFloat(x);
										y = parseFloat(y);
										return ((x < y || isNaN(x)) ? 1 : ((x > y || isNaN(y) ) ? -1 : 0));
									};

									if (jQuery('table.no-border').not('#issuetable').length) {

									}

									jQuery('table.no-border').not('#issuetable').on('init.dt', function (e, settings, data) {
										setTimeout(function () {
											jQuery('.loading').fadeOut();
										}, 750)
									}).dataTable({
										"aoColumns": [
											{% if '/repo/' in request.path and '/packages' in request.path %}
												{"sType": "num-html"}, null, null, {"sType": "num-html"}, null, null, null{% if '/group/' in request.path %}, null{% endif %}{% if '/monitored' in request.path %}, null, null{% endif %}{% if current_user.is_authenticated %}, null{% endif %}

											{% elif '/builds/' in request.path and '/completed' in request.path %}
												{"sType": "num-html"}, {"sType": "num-html"}, null, null, null, null, null, null, null

											{% elif '/builds/' in request.path %}
												{"sType": "num-html"}, {"sType": "num-html"}, null, null, null, null

											{% elif '/issues' in request.path %}
												{"sType": "num-html"}, {"sType": "num-html"}, null, null, null, null, null, null, null, null

											{% endif %}
										],
										"aaSorting": [
											[
												{% if '/repo/' in request.path and '/packages' in request.path %}
													1, "asc"
												{% else %}
													1, "desc"
												{% endif %}
											]
										]

									});

									jQuery('#issuetable').bind('issuesReady', function () {
										jQuery('#issuetable').on('init.dt', function (e, settings, data) {
											setTimeout(function () {
												jQuery('.loading').fadeOut();
											}, 750)
										}).dataTable({
											"aoColumns": [
												{"sType": "num-html"},
												null,
												null,
												null,
												null{% if '/builds/completed' in request.path or '/pkg_review' in request.path %},
												null,
												null,
												null{% endif %}{% if '/issues' in request.path %},
												null{% endif %}
											],
											"aaSorting": [
												[0, "desc"]
											]

										});

									});
									if (!jQuery('#issuetable').length) {
										jQuery('#DataTables_Table_0_paginate').replaceWith(jQuery('ul.pagination.custom'));
									}
								}
							{% endif %}
						});
				</script>
			{%- endblock scripts %}
		{%- endblock body %}
		</body>
	{%- endblock html %}
	</html>
{% endblock doc -%}
