<?php

if(isset($data[0])) {
	$row = $data[0];

	// if (strlen($patnumber) < 10) {
	// 	$patnumber = str_pad($patnumber, 10, "0", STR_PAD_LEFT);
	// }
?>
<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 width='18%'>Access Number</th> 
					<th>:</th> 
					<td width='31%'><?=$accessnumber;?></td>

					<th>Visit Description</th> 
					<th>:</th> 
					<td><?=$visit_description;?></td>
					
				</tr>
				<tr> 
					<th>Patient Number</th> 
					<th>:</th> 
					<td><?=$patnumber;?></td> 
					
					<th width='18%'>Payer Name</th> 
					<th>:</th> 
					<td width='31%'><?=$payer_name;?></td>
					
				</tr>
				<tr> 

					<th>Patient Name</th> 
					<th>:</th> 
					<td><?=$patient_fullname;?></td> 

					<th>Treating Doctor</th> 
					<th>:</th> 
					<td><?=$treating_doctor;?></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>
						<tr> 
							<td></td> <td></td> <td>All</td> 
							<td>
								<button type='button' class='btn btn-dark m-0 px-2 py-1' onclick="printAllLabel(<?=$accessnumber;?>, <?=$usercityid;?>)"> <h6 class='p-0 m-0'><i class='bi bi-printer'></i></h6> </button> 
								<button type='button' class='btn btn-success m-0 px-2 py-1' onclick='collectAll(<?=$accessnumber;?>)'><h6 class='p-0 m-0'>Coll.</h6></button> 
								<!-- <button class='badge bg-black text-white m-0 px-2 py-1' onclick='uncollectAll(<?=$accessnumber;?>)'>un-collect</button> -->
								<button type='button' class='btn btn-primary m-0 px-2 py-1' onclick='unreceiveAll(<?=$accessnumber;?>)'><h6 class='p-0 m-0'>Un-Rec.</h6></button> 
							</td>
						</tr>
						<tr> 
							<td></td> <td></td> <td>Collection</td> 
							<td><button type='button' class='btn btn-dark m-0 px-2 py-1' onclick="printCollectionLabel(<?=$accessnumber;?>, <?=$usercityid;?>)"><h6 class='p-0 m-0'> <i class='bi bi-printer'></i></h6></button></td>
						</tr>
						
					<?php
					foreach($data as $row) {
						$sampletype = $row['SAMPLETYPE'];
						$sampletext = $row['SHORTTEXT'];
						if ($sampletype == "200") {
							$sampletext = "Serum Kimia";
						} else if ($sampletype == '250') {
							$sampletext = "Serum Imun";
						}
						$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 ($sampletype)</td>"; 
						echo "<td> 
								<button type='button' class='btn btn-dark m-0 px-2 py-1' " . "onclick='printSingleLabel($accessnumber, $sampletype, $usercityid)'" . "><h6 class='p-0 m-0'><i class='bi bi-printer'></i></h6></button>
								<button type='button' class='btn btn-success m-0 px-2 py-1' onclick='collect($sampletype, $accessnumber)'><h6 class='p-0 m-0'>Coll.</h6></button>
								<button type='button' class='btn btn-warning m-0 px-2 py-1' onclick='uncollect($sampletype, $accessnumber)'><h6 class='p-0 m-0'>Un-Coll.</h6></button> 
								<button type='button' class='btn btn-primary m-0 px-2 py-1' onclick='unreceive($sampletype, $accessnumber)'><h6 class='p-0 m-0'>Un-Rec.</h6></button> 
							</td> ";
						echo "<td id='comment$sampletype'>$comment <h6 class='p-0 m-0'><i class='bi bi-pencil-square' role='button' onclick='comment($sampletype, $accessnumber, \"$sampletext\", \"$comment\")'></i></h6></td>";
						echo " </tr>";
					}
					?>
						
					</table>
				</div>
			</div>
		</div>
	</div>
</div>
<script>
function printCollectionLabel(access, usercityid){
	
	let url;
	if (usercityid == 2) {
		url = '<?=base_url();?>printLabelSby/collection/'+access.toString();	
	} else {
		url = '<?=base_url();?>printLabel/collection/'+access.toString();
	}

	console.log(url);
	
	fetch(url)
        .then(response => response.json())
        .then(data => {
            
			if (data['status']) {
				console.log(data['message']);
			} else {
				console.log(data['message']+"\n"+data['error']);
				message = data['message']+"\n"+data['error'];
				alert(message);
			}

        })
        .catch(error => {
            console.error('Error:', error);
        });
}
function printSingleLabel(access, sample, usercityid) {

	let url;
	if (usercityid == 2) {
		url = '<?=base_url();?>printLabelSby/single/'+access.toString()+'/'+sample.toString();
	} else {
		url = '<?=base_url();?>printLabel/single/'+access.toString()+'/'+sample.toString();
	}

	console.log(url);

	fetch(url)
        .then(response => response.json())
        .then(data => {
            
			if (data['status']) {
				console.log(data['message']);
			} else {
				console.log(data['message']+"\n"+data['error']);
				message = data['message']+"\n"+data['error'];
				alert(message);
			}

        })
        .catch(error => {
            console.error('Error:', error);
        });
}
function printAllLabel(access, usercityid) {
	
	let url;
	if (usercityid == 2) {
		url = '<?=base_url();?>printLabelSby/all/'+access.toString();
	} else {
		url = '<?=base_url();?>printLabel/all/'+access.toString();
	}

	console.log(url);

	fetch(url)
        .then(response => response.json())
        .then(data => {
			if (data['status']) {
				console.log(data['message']);
			} else {
				console.log(data['message']+"\n"+data['error']);
				message = data['message']+"\n"+data['error'];
				alert(message);
			}
        })
        .catch(error => {
            console.error('Error:', error);
        });
}

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	
}
?>