<?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" data-heading="Settings" data-subheading="SoftwareUpgrade">
	<div class="title-box">
		<h1>Software Upgrade</h1>
		<p>
			Update ATP and webserver software from a .zip file provided by A+T.
		</p>
	</div>
	
	<div class="top-wrapper box">
		<div class="flex-row">
			<div class="flex-half">
				<div class="flex-heading">
					<h2>Processor Version:</h2>
				</div>
				<div class="flex-item">
					<?php 
						echo str_replace("Version : ", "", readDB("Settings", "Version"));
					?>
				</div>
			</div>
			<div class="flex-half">
				<div class="flex-heading">
					<h2>Date Updated:</h2>
				</div>
				<div class="flex-item">
					<?php
						echo date("D, d M Y", readDB("Settings", "UpdateTimeStamp"));
					?>
				</div>
			</div>
		</div>
		<div class="flex-row">
			<div class="flex-half">
				<div class="flex-heading">
					<h2>Webserver Version:</h2>
				</div>
				<div class="flex-item">
					<?php echo $VERSION_NUMBER;?>
				</div>
			</div>
			<div class="flex-half">
				<div class="flex-heading">
					<h2>Release Date:</h2>
				</div>
				<div class="flex-item">
				<?php echo $VERSION_DATE;?>
				</div>
			</div>
		</div>
	</div>
	
	<div class="top-wrapper box">
		<form action="" method="post" enctype="multipart/form-data" id="upload-form">
			<fieldset>
				<legend><h2>Upgrade Software</h2></legend>
				<div class="flex-row">
					<div class="flex-heading">
						Select file to upload:
					</div>
					<div class="flex-item">
						<input type="file" name="fileToUpload" id="fileToUpload">
						<input type="submit" class="button gray medium" value="Upload Software"
							name="uploadSoftware" id="upload-button">
						<button type="button" hidden class="button gray medium"
							id="upgrade-button" style="display:none;">Upgrade Now</button>
					</div>
				</div>
			</fieldset>
			<div class="flex-row">
				<p>
					To upgrade the firmware of individual boards click <em style="text-decoration:underline" id="firmware">here</em>. This should not be needed for general use.
				</p>
			</div>
		</form>
	</div>
</section>

<script>
$(document).ready(function() {
	var pageHandler = new InputPageHandler('networkAddress.php', 'Settings');
	
	//Submit file to upload directory
	$('#upload-form').submit(function(e) {
		e.preventDefault();
		if ( $('#fileToUpload').prop('files').length == 0 ) {
			$('#dialog-text').text('Please select ATP software file to update.');
			$('#dialog').dialog( {
				title: "No File",
				buttons: {
					Ok : function() {
						$(this).dialog("close");
					}
				}
			}).dialog( "open" );
		} else {
			var formData = new FormData( this );
			var fileName = formData.get("fileToUpload").name;
			var regex = RegExp('ATProcessor.*\.zip$','g');
			if ( regex.test(fileName) ) {
				formData.append('action', 'Software');
				$.ajax( {
					url: 'upload.php',
					type: 'POST',
					data: formData,
					processData: false,
					contentType: false,
					cache: false,
				}).done(function() {
					$('#upgrade-button').css('display', 'inline-block');
				});
			} else {
				$('#dialog-text').text("Unexpected file. Software file should be a zip file beginning ATProcessor.");
				$('#dialog').dialog( {
					title: "Unexpected File",
					buttons: {
						Ok : function() {
							$(this).dialog("close");
						}
					}
				}).dialog( "open" );
			}
		}
	});
	
	//Upgrade processor
	$('#upgrade-button').click(function() {
		//Tell processor to start update
		$.ajax( {
			url: 'helpers.php',
			type: 'POST',
			data: {action: 'queryProcessor', query: 'Update=True'},
			success: function() {
				//Show loading spinner
				$('#upgrade-loader').show()
				//Query processor version each second
				intervalID = window.setInterval(function() {
					$.ajax( {
						url: 'helpers.php',
						type: 'POST',
						dataType: 'html',
						data: {action: 'queryProcessor', query: 'Version=True'},
						success: function(data) {
							//If result contains 'Version', update has completed
							if (data.length > 0) {
								window.clearInterval(intervalID);
								$('#dialog-text').text('Processor updated to ' . data);
								$('#dialog').dialog({
									title: 'Upgrade Successful',
									buttons: {
										Ok: function() {
											$(this).dialog("close");
											location.reload(true);
										}
									}
								}).dialog("open");
							}
						}
					});
				}, 1000);
			}
		});
	});
	
	//Firmware redirect
	$('#firmware').click(function() {
		$('#dialog-text').text('The functions on this page should be used with caution, there is potential to cause lasting damage to your system. \
			Do you still wish to continue?');
		$('#dialog').dialog( {
			title: "Firmware Upgrade Warning",
			buttons: {
				Ok : function() {
					$(this).dialog("close");
					location.href = "firmwareUpgrade.php";
				},
				Cancel: function() {
					$(this).dialog("close");
				}
			}
		}).dialog( "open" );
	});
});
</script>

<?php require_once "footer.php"?>