<?php
	session_start();
	if ( ! isset( $_SESSION['MOB'] ) ) $_SESSION['MOB'] = 0;
?>
<section name="sidebar" class="sidebar shadow">
	<ul>
		<?php
			for ($i = 0; $i < max(count(SIDEBAR_ITEMS), count(SIDEBAR_LINKS)); $i++) {
				if ( is_array(SIDEBAR_ITEMS[$i] ) ) {
					$submenu = '<ul class="submenu">';
					for ($j = 1; $j < max(count(SIDEBAR_ITEMS[$i]), count(SIDEBAR_LINKS[$i])); $j++) {
						$submenu .= '
							<li>
								<a href="' . SIDEBAR_LINKS[$i][$j] . '">' . SIDEBAR_ITEMS[$i][$j] . '</a>
							</li>';
					}
					$submenu .= '</ul>';
					
					echo '
					<li>
						<a href="' . SIDEBAR_LINKS[$i][0] . '">' . SIDEBAR_ITEMS[$i][0] . ' <i class="fa fa-angle-double-down"></i></a>
							' . $submenu . '
					</li>';
				} else {
					echo '
					<li>
						<a href="' . SIDEBAR_LINKS[$i] . '">' . SIDEBAR_ITEMS[$i] . '</a>
					</li>';
				}
			}
		?>
	</ul>
	
	<div class="MOB-warning" hidden>
		<i class="fa fa-exclamation-triangle fa-3x"></i><br>
		Man-Overboard
	</div>
	
	<div class="sidebar-bottom">
		<div class="flex-row">
			<div class="flex-half">
				UT
			</div>
			<div class="flex-half" id="UTCTime">
				
			</div>
		</div>
		<div class="flex-row">
			<div class="flex-half">
				Local
			</div>
			<div class="flex-half" id="LocalTime">
				
			</div>
		</div>
	</div>
</section>

<script>
$(document).ready(function() {
	
	isMOBActive = <?php echo $_SESSION['MOB'] ? 1 : 0?>;
	if ( isMOBActive ) $('.MOB-warning').show();
	
	setInterval(function() {
		$.ajax({
			type: 'POST',
			url: '/helpers.php',
			data: {
				action: 'queryProcessor',
				query: 'FieldId=0x1140,0x1141,0x1142,0x1143,0x1117'
			},
			timeout: 1000,
			success: function(result) {
				var fieldMatchRegex = /(0x.*)=\s?(.*)\r/g;
				var match = null;
				var parseFields = {};
				
				while ((match = fieldMatchRegex.exec(result)) !== null) {
						parseFields[match[1]] = match[2];
				}
				$('#UTCTime').html(parseFields['0x1143'] + '<br>' + parseFields['0x1142']);
				$('#LocalTime').html(parseFields['0x1141'] + '<br>' + parseFields['0x1140']);
				var MOBField = (!!parseFields['0x1117'])?parseFields['0x1117']:'n/a';
				if (MOBField.trim() == 'n/a') {
					$('.MOB-warning').hide();
					if (isMOBActive) {
						isMOBActive = 0;
						$.ajax({
							type: 'POST',
							url: '/helpers.php',
							data: {
								action: 'updateSession',
								variable: 'MOB',
								value: '0'
							}
						});
					}
				} else {
					$('.MOB-warning').show();
					if (!isMOBActive) {
						isMOBActive = 1;
						$.ajax({
							type: 'POST',
							url: '/helpers.php',
							data: {
								action: 'updateSession',
								variable: 'MOB',
								value: '1'
							}
						});
					}
				}
			}
		});
	}, 1000);
	
	$('li').click(function() {
		$(this).find('ul').toggle();
		$i = $(this).find('i');
		if ( $i.hasClass('fa-angle-double-down') ) {
			$i.removeClass('fa-angle-double-down');
			$i.addClass('fa-angle-double-up');
		} else {
			$i.addClass('fa-angle-double-down');
			$i.removeClass('fa-angle-double-up');
		}
	});

});
</script>