<?php
	require_once "../header.php";
	const SIDEBAR_ITEMS = COMMISSION_HEADER_ITEMS;
	const SIDEBAR_LINKS = COMMISSION_HEADER_LINKS;
	require_once "../sidebar.php";
?>

<section class="content-wrapper" id="commission" data-heading="Commission" data-subheading="Accelerometer">
	<div class="title-box">
		<h1>Commissioning</h1>
		<p>
			Commissioning for the ATP. Intended to be set once at point of manufacturing, should not need changing.
		</p>
	</div>
	
	<div class="top-wrapper box">
		<div class="flex-row">
			<div class="flex-heading">
				<h2>Serial Number</h2>
			</div>
			<div class="flex-input">
				<input type="text" class="database-input" style="width:200px;"
					id="serial-number" data-section="Settings"
					data-key="SerialNumber" value="<?php echo readDB("Settings", "SerialNumber");?>">
			</div>
		</div>
	</div>
	
	<div class="top-wrapper box">
		<div class="flex-row">
			<div class="flex-half">
				<div class="flex-heading">
					<h2>Clear Event Logs</h2>
				</div>
				<a class="button gray medium" id="clearEventLog">Clear</a>
			</div>
			<div class="flex-half">
				<div class="flex-heading">
					<h2>Clear Data Logs</h2>
				</div>
				<a class="button gray medium" id="clearDataLog">Clear</a>
			</div>
		</div>
	</div>
	
	<div class="top-wrapper box">
		<div class="flex-row">
			<div class="flex-heading" style="align-self:flex-start">
				<h2>Connected Boards</h2>
			</div>
			<div class="flex-column" style="flex:1 1 auto">
				<div class="flex-row">
					<div class="flex-heading">
						Wind Board
					</div>
					<input type="checkbox" name="board" data-section="Boards"
						data-key="Wind Board:0"
						<?php echo (readDB('Boards', 'Wind Board:0')?'checked':'')?>>
				</div>
				<div class="flex-row">
					<div class="flex-heading">
						Speed Board
					</div>
					<input type="checkbox" name="board" data-section="Boards"
						data-key="Speed Board:0"
						<?php echo (readDB('Boards', 'Speed Board:0')?'checked':'')?>>
				</div>
				<div class="flex-row">
					<div class="flex-heading">
						Speed/Depth Board
					</div>
					<input type="checkbox" name="board" data-section="Boards"
						data-key="Speed Depth Board:0"
						<?php echo (readDB('Boards', 'Speed Depth Board:0')?'checked':'')?>>
				</div>
				<div class="flex-row">
					<div class="flex-heading">
						Analogue Board
					</div>
					<input type="checkbox" name="board" data-section="Boards"
						data-key="Analogue Board:1"
						<?php echo (readDB('Boards', 'Analogue Board:1')?'checked':'')?>>
				</div>
			</div>
		</div>
	</div>
	
	<div class="middle-wrapper box box-top">
		<div class="flex-row">
			<div class="flex-heading">
				<h2>Calibrate Internal Sensors</h2>
			</div>
			<div>
				<a class="button gray medium" id="calibrate-bmi">Calibrate BMI</a>
				<a class="button gray medium" id="calibrate-atp">Calibrate ATP</a>
			</div>
		</div>
	</div>
	
	<div class="middle-wrapper box box-bottom">
		<div class="flex-row">
			<div class="flex-heading">
			
			</div>
			<div class="flex-item">
				<a id="ok-button" class="button gray medium submit-button">OK</a>
				<a id="cancel-button" class="button gray medium submit-button">Cancel</a>
				<a id="apply-button" class="button gray medium submit-button">Apply</a>
			</div>
		</div>
	</div>
</section>

<script>
$(document).ready(function() {
	
	var pageHandler = new InputPageHandler('commission.php', 'accelerometer');
	
	$('#calibrate-bmi').click(function() {
		$('#dialog-text').text('ATP must be oriented with the motherboard vertical and the cable gland plate facing downwards. Press Ok when ready.');
		$('#dialog').dialog( {
			title: "No File",
			buttons: {
				Ok : function() {
					$.ajax({
						type: 'POST',
						url: '/helpers.php',
						data: {action: 'queryProcessor', query: 'heelsensorcalibrate=bmicalibrate'},
						success: function(response) {
							var xml = response, xmlDoc = $.parseXML( xml ), $xml = $( xmlDoc ), $body = $xml.find( "BODY" );
							alert($body.text());
						}
					});
					$(this).dialog("close");
				},
				Cancel : function() {
					$(this).dialog("close");
				}
			}
		}).dialog( "open" );
	});

	$('#calibrate-atp').click(function() {
		$.ajax({
			type: 'POST',
			url: '/helpers.php',
			data: {action: 'queryProcessor', query: 'heelsensorcalibrate=atpcalibrate'},
			success: function(response) {
				var xml = response, xmlDoc = $.parseXML( xml ), $xml = $( xmlDoc ), $body = $xml.find( "BODY" );
				alert($body.text());
			}
		});
	})
	
	$('#clearEventLog').click(function() {
		$.ajax( {
			type: 'POST',
			url: '/helpers.php',
			data: {
				action: 'clearEventLogs'
			},
			success: function(response) {
				console.log(response);
			}
		});
	});
	
	$('#clearDataLog').click(function() {
		$.ajax( {
			type: 'POST',
			url: '/helpers.php',
			data: {
				action: 'clearDataLogs'
			},
			success: function(response) {
				console.log(response);
			}
		});
	});
	
	pageHandler.validateForm = function() {
		
		deleteSection('Boards').done(function(data, status, xhr) {
			if (xhr.responseText == "Success") {
				getServerMillis().done(function(milliseconds) {
					writeSingle('Boards', 'Main Interface Board:0', milliseconds);
					$('[name="board"]:checked').each(function() {
						writeSingle('Boards', $(this).attr('data-key'), milliseconds);
					});
				});
			} else {
				$('#dialog-text').text('Deleting boards from database failed.');
				$('#dialog').dialog( {
					title: "Delete Failed",
					buttons: {
						Ok : function() {
							$(this).dialog("close");
						}
					}
				}).dialog( "open" );
			}
		});
		
		return $.Deferred().resolve(true);
		
	}
	
});
</script>

<?php require_once "../footer.php";?>