<?php 
include_once ("includes/setup.inc.php");

$script_base = 'pht_listjanji';
$myID = fetch_postget_val('appointment',$_POST, $_GET);

$indicator_Query = "SELECT desk_ubat, quantity, ubat_remarks, price
				  FROM m_appointment_ubat
				  LEFT JOIN i_ubat ON m_appointment_ubat.id_ubat = i_ubat.id_ubat
				  WHERE id_vatklinik_appointment = ?";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->bindValue(1, $myID, PDO::PARAM_INT);
$PDO_stmt->execute();
$ubat_List = [];
while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
{
	$ubat_List[] = $result;
}

?>
<div class="modal-header">
	<h5 class="modal-title">Medicine & Done</h5>
	<button type="button" class="close" data-dismiss="modal" aria-label="Close" id="closeBtn" name="closeBtn" ></button>
</div>
<div class="modal-body">
	<form action="javascript:;" class="form-horizontal" name="indicator_form" id="indicator_form">
		<div class="form-body">
			<div class="row">
				<?php
					include_once ("general-appointment_detail.php");
				?>
				<div class="col-md-12">
					<h4><u>Doctor Check Result</u></h4>
					<?php echo ($appointment_List['check_result']==''?'Tiada':$appointment_List['check_result']); ?>
					<br>
					<br>
					<h4><u>Medicine List</u></h4>
					<table class="table table-sm thead-dark table-hover">
						<tr style="background-color:lightgray">
							<th style="width:10%;text-align:center">#</th>
							<th style="width:40%">Nama Ubat</th>
							<th style="width:40%">Remarks</th>
							<th style="width:10%;text-align:center">Quantity</th>
						</tr>
						<?php
							if(count($ubat_List) > 0)
							{
								for($num = 0; $num < count($ubat_List); $num++)
								{
									echo '<tr>';
										echo '<td style="text-align:center">'.($num+1).'</td>';
										echo '<td>'.$ubat_List[$num]['desk_ubat'].'</td>';
										echo '<td>'.$ubat_List[$num]['ubat_remarks'].'</td>';
										echo '<td style="text-align:center">'.$ubat_List[$num]['quantity'].'</td>';
									echo '</tr>';
								}
							}
							else
							{
								echo '<tr>';
									echo '<td colspan=4 style="text-align:center">Tiada Ubat Diperlukan</td>';
								echo '</tr>';
							}
						?>
					</table>
				</div>
				<div class="col-md-12" style="text-align:center">
					<button type="button" class="btn btn-primary" id="confirmBtn">Set Done</button>
				</div>
			</div>
			
		</div>
	</form>
</div>
<div class="modal-footer">
	<?php
		//echo '<button type="button" class="btn btn-secondary" data-dismiss="modal">Tutup</button>';
		if($readonly == 0)
		{
			//echo '<button type="button" class="btn btn-primary" id="appointment_simpanBtn">Simpan</button>';
		}
	?>
</div>

<script>
$(document).ready(function(){  

	$('.select2').select2({ width: '100%' }); 
	
	$('.numonly').keypress(function(evt){
		evt = (evt) ? evt : window.event;
		var charCode = (evt.which) ? evt.which : evt.keyCode;
		if ((charCode >= 48 && charCode <= 57)) {
			return true;
		}
		return false;
	});
	
	$('#confirmBtn').click(function() {
		Swal.fire({
		  title: 'Are you sure to set done?',
		  text: "You won't be able to revert this!",
		  icon: 'warning',
		  showCancelButton: true,
		  confirmButtonColor: '#3085d6',
		  cancelButtonColor: '#d33',
		  confirmButtonText: 'Yes'
		}).then((result) => {
		  if (result.isConfirmed) {
			$(this).prop("disabled",true);
			$.ajax({
				type: "POST", 
				url: "pht_listjanji-set_done_insert.php", 
				data: 'id=' + "<?=$myID?>",

				success: function(res){
					
					$(this).prop("disabled",false);
					if(res > 0)
					{
						swal.fire({
							text: "Set Done Success",
							icon: "success",
							buttonsStyling: false,
							confirmButtonText: "OK!",
							customClass: {
								confirmButton: "btn font-weight-bold btn-light-primary"
							}
						}).then(function() {
							window.parent.reloadTable();
							$("#closeBtn").click();
						});
					}
					else
					{
						swal.fire({
							text: "Error. " + res,
							icon: "error",
							buttonsStyling: false,
							confirmButtonText: "OK!",
							customClass: {
								confirmButton: "btn font-weight-bold btn-light-primary"
							}
						}).then(function() {
							$(this).prop("disabled",false);										
						});
					}
					
				} 
			});  //end ajax
		  }
		})
	});		
});		
</script>