<?php
if(isset($data[0])) {
$row = $data[0];
$patnumber = $row['PATNUMBER'];
$host = $row['HOSTORDERNUMBER'];
$name = $row['NAME'];
?>
<div class="modal-header bg-soft-green text-white">
	<h1 class="modal-title fs-5" id="exampleModalToggleLabel">Detail Request </h1>
	<button type="button" class="btn-close text-white" data-bs-dismiss="modal" ></button>
</div>
<div class="modal-body" style='background-color:#F4F6FF'>
	<div class="row">
		<div class="col">
			<table class="table table-sm table-borderless">
				<tr> <th>Access#</th> <th>:</th> <td><?=$accessnumber;?></td> </tr>
				<tr> <th>Patient</th> <th>:</th> <td><?=$patnumber;?> - <?=$name;?></td> </tr>
			</table>
		</div>
	</div>
	<div class='row'>
		<div class="col-12">
			<div class="card bg-white">
				<div class="card-body">
					<div class="card-title"><h3>Sample List</h3></div>
					<table class='table'>
						<tr> <th class='text-center'>Coll.</th> <th class='text-center'>Recv.</th> <th>Sample Name</th> <th>Action</th> <th>Comment</th>  </tr>
					<?php
					foreach($data as $row) {
						$sampletype = $row['SAMPLETYPE'];
						$sampletext = $row['SHORTTEXT'];
						$tubestatus = $row['TUBESTATUS'];
						$collstatus = $row['COLLSTATUS'];
						$comment = $row['TUBECOMMENT'];
						echo "\r\n	<tr>";
						if($collstatus==1) { 
							echo " <td class='text-center'><input type='checkbox' class='form-check-input' id='coll$sampletype' checked disabled></td>"; 
						} else { 
							echo " <td class='text-center'><input type='checkbox' class='form-check-input' id='coll$sampletype' disabled></td>"; 
						}
						if($tubestatus==4) { 
							echo "<td class='text-center'><input type='checkbox' class='form-check-input' id='recv$sampletype' checked disabled></td>"; 
						} else { 
							echo "<td class='text-center'><input type='checkbox' class='form-check-input' id='recv$sampletype' disabled></td>"; 
						}
						echo "<td>$sampletext</td>"; 
						echo "<td> 
								<button class='badge text-bg-dark'><i class='bi bi-printer'></i></button>
								<button class='badge text-bg-success' onclick='collect($sampletype, $accessnumber)'>Coll.</button>
								<button class='badge text-bg-warning' onclick='uncollect($sampletype, $accessnumber)'>Un-Coll.</button> 
								<button class='badge text-bg-primary' onclick='unreceive($sampletype, $accessnumber)'>Un-Rec.</button> 
							</td> ";
						echo "<td id='comment$sampletype'>$comment <i class='bi bi-pencil-square' role='button' onclick='comment($sampletype, $accessnumber, \"$sampletext\", \"$comment\")'></i></td>";
						echo " </tr>";
					}
					?>
						<tr> 
							<td></td> <td></td> <td>Collection</td> 
							<td> <button class='badge badge-dark'><i class='bi bi-printer'></i></button> </td>
						</tr>
						<tr> 
							<td></td> <td></td> <td>All</td> 
							<td>
								<button class='badge text-bg-dark'><i class='bi bi-printer'></i></button> 
								<button class='badge text-bg-success' onclick='collectAll(<?=$accessnumber;?>)'>Coll.</button> 
								<!-- <button class='badge bg-black text-white' onclick='uncollectAll(<?=$accessnumber;?>)'>un-collect</button> -->
								<button class='badge text-bg-primary' onclick='unreceiveAll(<?=$accessnumber;?>)'>Un-Rec.</button> 
							</td>
						</tr>
					</table>
				</div>
			</div>
		</div>
	</div>
</div>
<script>
function collect(sample, access) {
	sample = sample.toString().padStart(3,'0');
	const url = '<?=base_url();?>tubes/collect/'+access+'/'+sample;
	fetch(url)
		.then(data => { 
			//console.log(data);
			//$("#coll"+sample).prop("checked", true);
			viewAccess(access);
			index();
		})
		.catch(error => { console.error('Error:',error); });
}

function collectAll(access) {
	const url = '<?=base_url();?>tubes/collectAll/'+access;
	fetch(url)
		.then(data => { 
			//console.log(data);
			//$('input[id^="coll"]').prop('checked', true);
			viewAccess(access);
			index();
		})
		.catch(error => { console.error('Error:',error); });
	
}

function uncollect(sample, access) {
	sample = sample.toString().padStart(3,'0');
	const url = '<?=base_url();?>tubes/uncollect/'+access+'/'+sample;
	fetch(url)
		.then(data => { 
			//console.log(data);
			//$("#coll"+sample).prop("checked", false);
			viewAccess(access);
			index();
		})
		.catch(error => { console.error('Error:',error); });
	
}

function uncollectAll(access) {
	const url = '<?=base_url();?>tubes/uncollectAll/'+access;
	fetch(url)
		.then(data => { 
			//console.log(data);
			//$('input[id^="coll"]').prop('checked', false);
			viewAccess(access);
			index();
		})
		.catch(error => { console.error('Error:',error); });
}

function unreceive(sample, access) {
	sample = sample.toString().padStart(3,'0');
	const url = '<?=base_url();?>tubes/unreceive/'+access+'/'+sample;
	fetch(url)
		.then(data => { 
			//console.log(data);
			//$("#recv"+sample).prop("checked", false);
			viewAccess(access);
			index();
		})
		.catch(error => { console.error('Error:',error); });
	
}

function unreceiveAll(access) {
	const url = '<?=base_url();?>tubes/unreceiveAll/'+access;
	fetch(url)
		.then(data => { 
			//console.log(data);
			//$('input[id^="recv"]').prop('checked', false);
			viewAccess(access);
			index();
		})
		.catch(error => { console.error('Error:',error); });
}

function comment(sample, access, sampletext, comments) {
	const url = '<?=base_url();?>tubes/comment/'+access+'/'+sample;
	let comment = prompt('Comment for sample '+sampletext, comments);	
	//$('#comment'+sample).html(comment +"<i class='bi bi-pencil-square' onclick='comment("+ sample +", "+ access +', "'+sampletext+'", "'+comment+'")\'></i>'); 
	fetch(url, {
		method: "POST",
		body: JSON.stringify({ comment : comment }),
		headers: { "Content-type": "application/json; charset=UTF-8" }
	}).then(data => { 
		//console.log(data);
		viewAccess(access);
	})
	.catch(error => { console.error('Error:',error); });
}
</script>
<?php
} else {
?>
<div class="modal-header bg-black text-white">
	<h1 class="modal-title fs-5" id="exampleModalToggleLabel">Detail Request </h1>
	<button type="button" class="btn-close" data-bs-dismiss="modal" ></button>
</div>
<div class="modal-body">
	<h3>Data not found</h3>
</div>
<?php	
}
?>