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

$script_base = 'sen_appointment';
$myID = fetch_postget_val('id',$_POST, $_GET);
$readonly = fetch_postget_val('readonly',$_POST, $_GET);
$temp = ($my_details->usr_id).'_'.time();

$indicator_Query = "SELECT A.*
				  FROM m_vatklinik_appointment A
				  WHERE A.id_vatklinik_appointment = ?";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->bindValue(1, $myID, PDO::PARAM_INT);
$PDO_stmt->execute();
$indicator_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);


?>
<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($PDO, 'Klinik Vaterinar', 'i_vatklinik', 'klinik_vat', $indicator_List['id_haiwan_peliharaan'], 'Pilih Klinik Vaterinar', 7, 3, 12, $readonly, 'nama_');
					
					$pet_sql = 'SELECT id_pemilik_pet AS id, concat(nama_pet, " (", desk_haiwan_peliharaan, ")") as name
								FROM m_pemilik_pet
								JOIN i_haiwan_peliharaan ON m_pemilik_pet.id_haiwan_peliharaan = i_haiwan_peliharaan.id_haiwan_peliharaan
								WHERE id_pemilik = '.$my_details->usr_id;
					createDropDown_sql($PDO, 'Haiwan Peliharaan', $pet_sql, 'id_pet', $indicator_List['id_pemilik_pet'], 'Pilih Haiwan Peliharaan', 7, 3, 12, $readonly);
					createDateTB('Tarikh Temu Janji', 'tkh_appointment', 'Tarikh Temu Janji', null, 3, $readonly, 7, 12);
					
					$my_array = [];
					createDropDown_Array('Masa Temu Janji', $my_array, 'timeslot', 0, '', 7, 3, 12, $readonly);
					createTextBox('Remarks (Jika Berkenaan)', '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="appointment_simpanBtn">Simpan</button>';
		}
	?>
</div>

<script>
$(document).ready(function(){  
	$('#timeslot_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;
	});
	
	$('#tkh_appointment').change(function(){
		var indicator_list = [[]];
		indicator_list.push(['klinik_vat', 'Klinik Vaterinar', 'dd']);
		
		if (checkInput(indicator_list))
		{
			$.ajax({
				type: "POST", 
				url: "<?=$script_base?>-tambah_checktimeslot.php", 
				data: 'klinik_vat=' + $('#klinik_vat').val() + '&appoint_date=' + $('#tkh_appointment').val(),

				success: function(res){
					$('#timeslot_div').show();
					$('#timeslot').html(res);
				} 
			});  //end ajax
		}
		else
		{
			$('#timeslot_div').hide();
		}
	});
	
	$('#appointment_simpanBtn').click(function() {
		var indicator_list = [[]];
		indicator_list.push(['klinik_vat', 'Klinik Vaterinar', 'dd']);
		indicator_list.push(['id_pet', 'Haiwan Peliharaan', 'dd']);
		indicator_list.push(['tkh_appointment', 'Tarikh Temu Janji', 'tb']);
		indicator_list.push(['timeslot', 'Masa Temu Janji', 'dd']);
		
		if (checkInput(indicator_list))
		{
			$(this).prop("disabled",true);
			var param = $('#indicator_form').serialize();
			$.ajax({
				type: "POST", 
				url: "<?=$script_base?>-tambah_insert.php", 
				data: param+ '&id=' + "<?=$myID?>",

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