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

<?= $this->section('content') ?>

<style>
	#myTable {
    	cursor: pointer;
	}
</style>

<div id='stats' class="d-flex justify-content-between p-0">
</div>

<div class="card border-0">

	<div class="card-body">
		
		<div class='card-title'><u><h4>Request from Transmedic</h4></u></div>
		<div class="row d-flex align-items-center">
			<div class="col col-auto">
				<b>Date</b>&nbsp; &nbsp;:&nbsp; &nbsp;<input class='date1' type='date' value=''> - <input class='date2' type='date'> 
			</div>
			<div class="col col-auto">
				<button class='btn btn-sm btn-primary py-1 px-2 d-flex align-items-center' onclick='index()'><i class="bi bi-calendar2-event"></i>&nbsp;&nbsp;Search</button> 
			</div>
		</div>
	
		<div class="table-responsive mt-3">
			<table id="myTable" class="table table-hover">
				<thead class='text-start'>
					<th>(GMT)</th>
					<th>Reff#</th>
					<th>Access#</th>
					<th>RM#</th>
					<th>Patient Name</th>
					<th>Test</th>
					<th></th>
				</thead>
				<tbody id="table-body" class='text-start'>
				</tbody>
			</table>
		</div>
	</div>
</div>	

<div class="modal fade" id="modal" aria-hidden="true" tabindex="-1">
	<div class="modal-dialog modal-lg modal-dialog-centered modal-dialog-scrollable">
		<div class="modal-content">
		</div>
	</div>
</div>

<?= $this->endSection() ?>

<?= $this->section('script') ?>
<script>
let curDate = new Date().toJSON().slice(0, 10);
$('.date1').val(curDate);
$('.date2').val(curDate);

index();

function index() {
	let url = '<?=base_url();?>api/tm/index';
	date1 = $('.date1').val();
	date2 = $('.date2').val();
	$.ajax({
		url: url,
		method: 'POST',
		data : {date1:date1, date2:date2},
		success: function(response) {	
			$("#myTable").DataTable().destroy();
			$("#table-body").html("");
			var data = response['data'];
			for (var i = 0; i < data.length; i++) {
				console.log(data[i]);
				reqdate = data[i].REQDATE;
				logdate = data[i].LOGDATE;
				reffid = data[i].REFFID;
				accessnumber = data[i].SP_ACCESSNUMBER;
				patnumber = data[i].PATNUMBER;
				patname = data[i].FIRSTNAME+' '+data[i].LASTNAME;
				tests = data[i].TESTS;
				let datarow = "<tr class='align-middle main_table'>" + 
					"<td> req : " + reqdate + '<br / >log : ' +  logdate + '</td> <td>' + reffid + '</td> <td>' + accessnumber + '</td> <td>' + patnumber + '</td> <td>' + patname + '</td> <td>' + tests
					+ '</td> </tr>';					
				$("#table-body").append(datarow);
			}
			//$('#myTable').DataTable();
		},
		error: function(response) { console.log(response.responseJSON); }
	});
}

function viewAccess(access) {
	let url = '<?=base_url();?>admin/dashboard/viewAccess/'+access;
	$('.modal-content').load(url, function(){
		$('#modal').modal('show');
	});
}
function resultPdfAccess(access, event) {
	event.stopPropagation(); // Mencegah klik pada <tr> dieksekusi
	let url = '<?=base_url();?>printResult/'+access;
	window.open(url, '_blank');
}
</script>
<?= $this->endSection() ?>