<?= $this->extend('layouts/main.php') ?>

<?= $this->section('content') ?>
<div class="page-wrapper">
	<div class="container-fluid">
		<div class="row page-titles">
			<div class="col-md-5 align-self-center">
				<h4 class="text-themecolor">LQMS REST-Server Message Log</h4>
			</div>					
		</div>
		<div class="row">
			<div class="col-12">
				<div class="card">
					<div class="card-body">
						<p><button class='btn btn-sm btn-danger' type='button' onclick='deleteLog()'>clear all</button> </p>
						<div class="table-responsive">
							<table class="table display table-striped border">
								<tbody>
									<?php
									foreach($logs as $log) {
										$mesid = $log['mesid'];
										$message = $log['message'];
										$mesdate = $log['mesdate'];
										//$json = json_decode($message);
										echo "<tr> <td><button class='btn btn-sm btn-warning' type='button' onclick='deleteMes(this, $mesid)'>delete</button><br/><pre>$message</pre></td> </tr>";
									} 
									?>
								</tbody>
							</table>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<?= $this->endSection() ?>

<?= $this->section('script') ?>
<script>
function deleteLog() {
	if (confirm("Are you sure?")) {
		var url = "<?=base_url();?>/lqms/log_clear";
		console.log(url);
		$.ajax({
			url: url,
			method: "GET", 
			success: function(data) {
				console.log("Data:", data);
				alert('please refresh this browser.');
			},
			error: function( textStatus, errorThrown) {
				console.error("Error:", textStatus, errorThrown); 
			}
		});
	} else {
		console.log("Request canceled by user.");
	}
}
function deleteMes(btn, mesid) {
	//console.log(this);
	var tableRow = btn.parentNode.parentNode;
    tableRow.parentNode.removeChild(tableRow);
	
	var url = "<?=base_url();?>/lqms/log/delete/"+mesid;
	//console.log(url);
	
	$.ajax({
		url: url,
		method: "GET", 
		success: function(data) { console.log("Data:", data); },
		error: function( textStatus, errorThrown) { console.error("Error:", textStatus, errorThrown); }
	});
	
}
</script>
<?= $this->endSection() ?>