<?php 
require('includes/header.php'); 
?>	

<!--begin::Content-->
<div class="content d-flex flex-column flex-column-fluid" id="kt_content">
	<!--begin::Subheader-->
	<div class="subheader py-2 py-lg-4 subheader-solid" id="kt_subheader">
		<div class="container-fluid d-flex align-items-center justify-content-between flex-wrap flex-sm-nowrap">
			<!--begin::Info-->
			<div class="d-flex align-items-center flex-wrap mr-2">
				<!--begin::Page Title-->
				<h5 class="text-dark font-weight-bold mt-2 mb-2 mr-5">Pengurusan Tetingkap</h5>
				<!--end::Page Title-->
				<!--begin::Actions-->
				<div class="subheader-separator subheader-separator-ver mt-2 mb-2 mr-4 bg-gray-200"></div>
			</div>
			<!--end::Info-->
		</div>
	</div>
	<!--end::Subheader-->
	<!--begin::Entry-->
	<div class="d-flex flex-column-fluid">
		<!--begin::Container-->
		<div class="container">
			<!--begin::Dashboard--> 
			<!--begin::Row-->
			<div class="row">
				<div class="col-md-12">
					<div class="card card-custom gutter-b">
						<div class="card-header">
							<div class="card-title col-md-12">
								<div class="col-md-6">
									<h3 class="card-label">Pengurusan Tetingkap</h3>
								</div>
								<div class="col-md-6 text-right">
									<button type="button" class="btn btn-primary" id="tetingkap_simpanBtn">Simpan</button>
								</div>
							</div>
						</div>
						<div class="card-body">
							<!--begin::Example-->
								<form action="javascript:;" class="form-horizontal" name="form2" id="form2">
								<?php
									$checkbox_child_List = [];
									$katParent = [];
									$kategoriQuery = "SELECT *
														FROM t_kategori
														ORDER BY id_parent, susunan";
									$kategori_Result = $PDO->prepare($kategoriQuery);
									$kategori_Result->execute();
									$kategoriArray = array();
									while($kategoriResult = $kategori_Result->fetch(PDO::FETCH_ASSOC)) 
									{
										$kategoriArray[$kategoriResult['id_parent']][] = $kategoriResult;
										$checkbox_child_List['k_'.$kategoriResult['id_parent']][] = 'k_'.$kategoriResult['id_kategori'];
										$katParent['k_'.$kategoriResult['id_kategori']] = 'k_'.$kategoriResult['id_parent'];
									}
									
									$tetingkapQuery = "SELECT *
														FROM t_tetingkap
														ORDER BY id_kategori, susunan";
									$tetingkap_Result = $PDO->prepare($tetingkapQuery);
									$tetingkap_Result->execute();
									$tingkapArray = array();
									while($tetingkapResult = $tetingkap_Result->fetch(PDO::FETCH_ASSOC)) 
									{
										$tingkapArray[$tetingkapResult['id_kategori']][] = $tetingkapResult;
										$checkbox_child_List['k_'.$tetingkapResult['id_kategori']][] = 't_'.$tetingkapResult['id_tetingkap'];
										$checkbox_child_List[$katParent['k_'.$tetingkapResult['id_kategori']]][] = 't_'.$tetingkapResult['id_tetingkap'];
									}
									
									echo '<ul class="nav nav-tabs" role="tablist">';
										$counter = 1;
										foreach($kategoriArray[0] as $key => $menu) 
										{
											echo '<li class="nav-item">';
												echo '<a class="nav-link '.($counter==1?'active':'').'" data-toggle="tab" href="#tab'.$counter.'">';
													echo '<span class="nav-text">'.$menu['desk_kategori'].'</span>';
												echo '</a>';
											echo '</li>';
											$counter++;
										}
									echo '</ul>';
									
									echo '<div class="tab-content">';
										$counter = 1;
										foreach($kategoriArray[0] as $key => $menu) 
										{
											echo '<div class="tab-pane fade '.($counter==1?'show active':'').'" id="tab'.$counter.'" role="tabpanel" aria-labelledby="tab'.$counter.'-tab">';	
												echo '<div class="card card-custom">';
													echo '<div class="card-header">';
														echo '<div class="card-title">';
															echo '<h3 class="card-label">';
																	echo '<div class="checkbox-list">';
																		echo '<label class="checkbox '.($readonly?'checkbox-disabled':'').'">';
																			echo '<input class="katCheckBox" type="checkbox" '.($readonly?'disabled="disabled"':'').' '.($menu['aktif']?'checked="checked"':'').' id="k_'.$menu['id_kategori'].'" name="k_'.$menu['id_kategori'].'" onchange="checkUncheckCB(\''.'k_'.$menu['id_kategori'].'\', \''.join(',', $checkbox_child_List['k_'.$menu['id_kategori']]).'\')">';
																			echo '<span></span>';
																			echo $menu['desk_kategori'];
																		echo '</label>';
																	echo '</div>';
															echo '</h3>';
														echo '</div>';
													echo '</div>';
													echo '<div class="card-body">';
														if (isset($tingkapArray[$menu['id_kategori']])) //show direct menu here
														{
															echo '<div class="card card-custom">';
																echo '<div class="card-body">';
																	echo '<div class="checkbox-list">';
																		foreach($tingkapArray[$menu['id_kategori']] as $key2 => $menu2) //show direct menu here
																		{
																			echo '<label class="checkbox '.($readonly?'checkbox-disabled':'').'">';
																				echo '<input type="checkbox" '.($readonly?'disabled="disabled"':'').' '.($menu2['aktif']?'checked="checked"':'').' id="t_'.$menu2['id_tetingkap'].'" name="t_'.$menu2['id_tetingkap'].'" >';
																				echo '<span></span>';
																				echo $menu2['desk_tetingkap'];
																			echo '</label>';
																		}
																	echo '</div>';
																echo '</div>';
															echo '</div>';
														}
														else
														{
															foreach($kategoriArray[$menu['id_kategori']] as $key2 => $menu2)
															{
																echo '<div class="card card-custom">';
																	echo '<div class="card-header">';
																		echo '<div class="card-title">';
																			echo '<h3 class="card-label">';
																				echo '<div class="checkbox-list">';
																					echo '<label class="checkbox '.($readonly?'checkbox-disabled':'').'">';
																						echo '<input class="katCheckBox" type="checkbox" '.($readonly?'disabled="disabled"':'').' '.($menu2['aktif']?'checked="checked"':'').' id="k_'.$menu2['id_kategori'].'" name="k_'.$menu2['id_kategori'].'" onchange="checkUncheckCB(\''.'k_'.$menu2['id_kategori'].'\', \''.join(',', $checkbox_child_List['k_'.$menu2['id_kategori']]).'\')">';
																						echo '<span></span>';
																						echo $menu['desk_kategori'].' - '.$menu2['desk_kategori'] ;
																					echo '</label>';
																				echo '</div>';
																			echo '</h3>';
																		echo '</div>';
																	echo '</div>';
																	echo '<div class="card-body">';
																		echo '<div class="checkbox-list" style="padding-left: 40px;">';
																			foreach($tingkapArray[$menu2['id_kategori']] as $key3 => $menu3) 
																			{
																				echo '<label class="checkbox '.($readonly?'checkbox-disabled':'').'">';
																					echo '<input type="checkbox" '.($readonly?'disabled="disabled"':'').' '.($menu3['aktif']?'checked="checked"':'').' id="t_'.$menu3['id_tetingkap'].'" name="t_'.$menu3['id_tetingkap'].'">';
																					echo '<span></span>';
																					echo $menu3['desk_tetingkap'];
																				echo '</label>';
																			}
																		echo '</div>';
																	echo '</div>';
																echo '</div>';
															}
														}
													echo '</div>';
												echo '</div>';
											echo '</div>';
											$counter++;
										}
									echo '</div>';
								?>
								</form>
							<!--end::Example-->
						</div>
					</div>
				</div>
			</div>
			<!--end::Row-->
			<!--end::Dashboard-->
		</div>
		<!--end::Container-->
	</div>
	<!--end::Entry-->
</div>
<!--end::Content-->
					
<?php 
require('includes/footer.php');
?>


<script>
function checkUncheckCB(callerId, allIds)
{
	var checked = false;
	if( $("#" + callerId).is(':checked')) {
        checked = true;
    }
	
	var allId = allIds.split(",");
	for(var num = 0; num < allId.length; num++)
	{
		$( "#" + allId[num] ).prop( "checked", checked );
	}
}

$(document).ready(function(){  

	$('.select2').select2({ width: '100%' }); 
	
	$('#tetingkap_simpanBtn').click(function() {
		
		var allCheck = $('#form2 input:checkbox').serialize();
		allCheck = allCheck.replace(/\&/g, "");
		allCheck = allCheck.replace(/=on/g, ",");
		
		$(this).prop("disabled",true);
		var param = $('#form1').serialize();
		$.ajax({
			type: "POST", 
			url: "in_tetingkap-insert.php", 
			data: 'tingkap=' + allCheck,

			success: function(res){
				if(res > 0)
				{
					swal.fire({
						text: "Berjaya Kemaskini Maklumat Tetingkap",
						icon: "success",
						buttonsStyling: false,
						confirmButtonText: "OK!",
						customClass: {
							confirmButton: "btn font-weight-bold btn-light-primary"
						}
					}).then(function() {
						window.location.reload();
						//$(this).prop("disabled",false);	
					});
				}
				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>