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

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

$indicator_Query = 'SELECT id_ubatkat AS id, desk_ubatkat as name
					FROM i_ubatkat
					WHERE aktif = 1
					order by name';
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->execute();
$ubatkat_List = [];
while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
{
	$ubatkat_List[] = array($result['id'], $result['name']);
}
?>
<div class="modal-header">
	<h5 class="modal-title">Tambah Temu Janji Klinik</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
					createDropDown_Array('Kategori Ubat', $ubatkat_List, 'ubatkat', 0, 'Pilih Kategori Ubat', 7, 3, 12, $readonly);
					
					$my_array = [];
					createDropDown_Array('Nama Ubat', $my_array, 'id_ubat', 0, '', 7, 3, 12, $readonly);
					createTextBox('Quantity Ubat', 'qty_ubat', 'Quantity Ubat', '', 3, $readonly, 7, 12, '', 'text');
					createTextBox('Remarks (Jika Berkenaan)', 'ubat_remarks', 'Remarks (Jika Berkenaan)', $indicator_List['remarks'], 3, $readonly, 7, 12, '', 'text');
				?>
			</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="checkresult_simpanBtn">Simpan</button>';
		}
	?>
</div>

<script>
$(document).ready(function(){  
	$('#id_ubat_div').hide();
	
	$('.myDatePicker').datepicker({
		autoclose: true,
		clearBtn: true,
		format: "dd M yyyy",
		todayHighlight: true,
		orientation: "bottom left",
		daysOfWeekDisabled: "0,6",
		startDate: "+0d",
		templates: {
			leftArrow: '<i class="la la-angle-left"></i>',
			rightArrow: '<i class="la la-angle-right"></i>'
		}
	});
	
	$('.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;
	});
	
	$('#ubatkat').change(function(){
		var indicator_list = [[]];
		indicator_list.push(['ubatkat', 'Kategori Ubat', 'dd']);
		
		if (checkInput(indicator_list))
		{
			$.ajax({
				type: "POST", 
				url: "pht_listjanji-check_result_tambah_checkubat.php", 
				data: 'ubatkat=' + $('#ubatkat').val(),

				success: function(res){
					$('#id_ubat_div').show();
					$('#id_ubat').html(res);
				} 
			});  //end ajax
		}
		else
		{
			$('#id_ubat_div').hide();
		}
	});
	
	$('#checkresult_simpanBtn').click(function() {
		var indicator_list = [[]];
		indicator_list.push(['id_ubat', 'Nama Ubat', 'dd']);
		indicator_list.push(['qty_ubat', 'Quantiti Ubat', 'tb']);
		
		if (checkInput(indicator_list))
		{
			$(this).prop("disabled",true);
			var param = $('#indicator_form').serialize();
			$.ajax({
				type: "POST", 
				url: "pht_listjanji-check_result_tambah_insert.php", 
				data: param+ '&appointment=' + "<?=$myID?>",

				success: function(res){
					
					$(this).prop("disabled",false);
					if(res > 0)
					{
						swal.fire({
							text: "Berjaya Tambah Ubat",
							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>