<?php
	$holdButtonDisabled = true;

	require_once "header.php";
	const SIDEBAR_ITEMS = SETTINGS_HEADER_ITEMS;
	const SIDEBAR_LINKS = SETTINGS_HEADER_LINKS;
	require_once "sidebar.php";
?>

<section class="content-wrapper" id="logs" data-heading="Settings" data-subheading="Logs">
	
	<div class="title-box">
		<h1>Event Logs</h1>
		<p>
			View and download internal event logs, for problem solving purposes. Logs from the current month can be viewed/downloaded as-is. Older logs are compressed to save space
			and must be downloaded and extracted in order to view. <em>Note: Times and dates are in UT.</em>
		</p>
	</div>
	
	<?php
		$eventLogCurrent = date('d_m_Y');
		$eventLogCurrent = 'logs/EventLogs/EventLog_' . $eventLogCurrent . '.log';
	?>
	
	<div class="flex-row log-controls">
		<div>
			<select id="log-select">
				<?php
					function get_date($file, &$datestring = "") 
                    {
						$date = 0;
						$filebase = basename($file);
						$datestring = preg_replace('/^EventLog_/', '', $filebase);
						if (preg_match('/.log$/', $file))
                        {
							$datestring = preg_replace('/.log$/', '', $datestring);
							$date = date_create_from_format('d_m_Y', $datestring);
							$datestring = date_format($date, 'dS M Y');
						} 
                        else if (preg_match('/.zip$/', $file))
                        {
							$datestring = preg_replace('/.zip$/', '', $datestring);
							$date = date_create_from_format('m_Y', $datestring);
							$datestring = date_format($date, 'M Y');
						}

                        return $date;
					}

					function sort_date($a, $b)
                    {
						$date_a = get_date($a);
						$date_b = get_date($b);

						if ($date_a == $date_b) return 0;
						return ($date_a < $date_b) ? 1 : -1;
					}

					$files = glob('logs/EventLogs/*');
					usort($files, "sort_date");

					foreach($files as $filename) 
                    {
						$datestring = "";
						$date = get_date($filename, $datestring);

						echo "<option value='" . $filename . "' data-type='log'>" . $datestring . "</option>";
					}
				?>
			</select>
		
			<a class="button small gray" id="reload-button">
				<div class="reload"></div>
			</a>
		</div>
		
		<a class="button medium gray" id="export-button" download="">
			Export
		</a>
	</div>
	
	<iframe id="log-frame" src="">
	</iframe>
	
</section>

<script>
$(document).ready(function() {
	
	$('#log-select').change(function() {
		if ( $('#log-select option:selected').attr('data-type') == 'log' ) {
			$('#log-frame').attr('src', $(this).find('option:selected').val() + '?' + Date.now());
		}
		$('#export-button').attr('href', $(this).find('option:selected').val());
	});
	
	$('#log-select option[value="<?php echo $eventLogCurrent;?>"]').attr('selected', 'selected');
	$('#log-select').change();
	
	//Refresh iframe
	$('#reload-button').click(function() {
		if ( $('#log-select option:selected').attr('data-type') == 'log' ) {
			$('#log-frame').attr('src', function(i, val) { return val + '?' + Date.now(); });
		}
	});
	
	$('#log-frame').on('load', function() {
		var $head = $('#log-frame').contents().find('head');                
		$head.append($("<link/>", { rel: "stylesheet", href: "/css/iframe.css", type: "text/css" }));
	});
});
</script>

<?php require_once "footer.php"?>