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

$myID = fetch_postget_val('id',$_POST, $_GET);
$readonly = fetch_postget_val('readonly',$_POST, $_GET);

$peranan_Query = "SELECT t_peranan.*
				  FROM t_peranan
				  WHERE id_peranan = $myID";
$peranan_Result = $PDO->prepare($peranan_Query);
$peranan_Result->execute();
$peranan_List = $peranan_Result->fetch(PDO::FETCH_ASSOC);

//get all user pages id
$userQuery = "SELECT id_tetingkap
			  FROM t_pautan 
			  WHERE id_peranan = $myID";
$user_Result = $PDO->prepare($userQuery);
$user_Result->execute();
// put all into hash for easier comparison
$userPages = array();
while($res = $user_Result->fetch(PDO::FETCH_ASSOC)) {
	$userPages[$res['id_tetingkap']]= 1;
}

//get all kategori and build main menu
$mainKatQuery = "SELECT id_kategori, id_parent, desk_kategori AS katName, susunan
				FROM t_kategori
				WHERE id_parent = 0
				AND aktif = 1
				ORDER BY susunan";
$mainKat_Result = $PDO->prepare($mainKatQuery);
$mainKat_Result->execute();
$mainMenu = array();
while($res = $mainKat_Result->fetch(PDO::FETCH_ASSOC)){
	$mainMenu[$res['id_kategori']] = $res;
}

//get all kategori and build sub menu
$katQuery = "SELECT id_kategori, id_parent, desk_kategori AS katName, susunan  
			FROM t_kategori
			WHERE aktif = 1
			ORDER BY susunan";
$kat_Result = $PDO->prepare($katQuery);
$kat_Result->execute();
while($katResult = $kat_Result->fetch(PDO::FETCH_ASSOC)) 
{
	//check in tetingkap table
	$tetingkapQuery = "SELECT id_tetingkap AS id, desk_tetingkap AS name, nama_skrip AS url, susunan
						FROM t_tetingkap
						WHERE id_kategori = {$katResult['id_kategori']}
						AND aktif = 1
						ORDER BY susunan";
	$tetingkap_Result = $PDO->prepare($tetingkapQuery);
	$tetingkap_Result->execute();
	$tingkapArray = array();
	while($tetingkapResult = $tetingkap_Result->fetch(PDO::FETCH_ASSOC)) 
	{
		$tingkapArray[] = $tetingkapResult;
	}
	
	if($katResult['id_parent'] == '0') //put as main menu
	{
		if(is_null($mainMenu[$katResult['id_kategori']]['child']))
		{
			$mainMenu[$katResult['id_kategori']]['menu'] = $tingkapArray;
		}
	}
	else //sub menu
	{
		$tempArray = array();
		$tempArray = $katResult;
		$tempArray['menu'] = $tingkapArray;
		$mainMenu[$katResult['id_parent']]['child'][] = $tempArray;
	}
}
?>

<div class="modal-header">
	<h5 class="modal-title">Pengurusan Peranan</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="form1" id="form1">
		<div class="form-body">
			<div class="row">
				<?php
					createTextBox('Deskripsi Peranan', 'desk', 'Deskripsi Peranan', $peranan_List['desk_peranan'], 3, $readonly, 7, 12);
					createTextBox('Susunan', 'susunan', 'Susunan', $peranan_List['susunan'], 3, $readonly, 7, 12);
					/*
					$myArray = [];
					$myArray[] = array(1, 'YA');
					createDropDown_Array('Aktif', $myArray, 'aktif', $peranan_List['aktif'], 'TIDAK', 7, 3, 12, $readonly);
					*/
					
				?>
			</div>
		</div>
	</form>
	<br><br>
	<form action="javascript:;" class="form-horizontal" name="form2" id="form2">
		<div class="form-body">
			<div class="row">
				<div class="col-md-12">
					<div class="card card-custom gutter-b">
						<div class="card-header">
							<div class="card-title">
								<h3 class="card-label">Maklumat Peranan</h3>
							</div>
						</div>
						<div class="card-body">
							<!--begin::Example-->
							<div class="example">
								<div class="example-preview">
								<?php
									if(count($mainMenu) > 0)
									{
										echo '<ul class="nav nav-tabs" role="tablist">';
											$counter = 1;
											foreach($mainMenu 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['katName'].'</span>';
													echo '</a>';
												echo '</li>';
												$counter++;
											}
										echo '</ul>';
										echo '<div class="tab-content">';
											$counter = 1;
											foreach($mainMenu as $key => $menu) 
											{
												echo '<div class="tab-pane fade '.($counter==1?'show active':'').'" id="tab'.$counter.'" role="tabpanel" aria-labelledby="tab'.$counter.'-tab">';	
												if (isset($menu['child'])) //for sub menu
												{
													foreach($menu['child'] 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 $menu['katName'].' - '.$menu2['katName'] ;
																	echo '</h3>';
																echo '</div>';
															echo '</div>';
															echo '<div class="card-body">';
																echo '<div class="checkbox-list">';
																if (count($menu2['menu']) > 0)
																{
																	foreach($menu2['menu'] as $key3 => $menu3) 
																	{
																		echo '<label class="checkbox '.($readonly?'checkbox-disabled':'').'">';
																			echo '<input type="checkbox" '.($readonly?'disabled="disabled"':'').' '.(array_key_exists($menu3['id'], $userPages)?'checked="checked"':'').' id="tingkap_'.$menu3['id'].'" name="tingkap_'.$menu3['id'].'">';
																			echo '<span></span>';
																			echo $menu3['name'];
																		echo '</label>';
																	}
																}
																echo '</div>';
															echo '</div>';
														echo '</div>';
													}
												}
												else if (isset($menu['menu'])) //got direct
												{
													echo '<div class="card card-custom">';
														echo '<div class="card-body">';
															echo '<div class="checkbox-list">';
																foreach($menu['menu'] as $key3 => $menu3) 
																{
																	echo '<label class="checkbox '.($readonly?'checkbox-disabled':'').'">';
																		echo '<input type="checkbox" '.($readonly?'disabled="disabled"':'').' '.(array_key_exists($menu3['id'], $userPages)?'checked="checked"':'').' id="tingkap_'.$menu3['id'].'" name="tingkap_'.$menu3['id'].'">';
																		echo '<span></span>';
																		echo $menu3['name'];
																	echo '</label>';
																}
															echo '</div>';
														echo '</div>';
													echo '</div>';
												}													
												echo '</div>';
												$counter++;
											}
										echo '</div>';
									}
									
								?>
								</div>
							</div>
							<!--end::Example-->
						</div>
					</div>
				</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="peranan_simpanBtn">Simpan</button>';
		}
	?>
	
</div>

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

	$('.select2').select2({ width: '100%' }); 
	
	$('#peranan_simpanBtn').click(function() {
		
		var allCheck = $('#form2 input:checkbox').serialize();
		allCheck = allCheck.replace(/\&/g, "");
		allCheck = allCheck.replace(/tingkap\_/g, "");
		allCheck = allCheck.replace(/=on/g, ",");
		
		var peranan_List = [[]];
		peranan_List.push(['desk', 'Deskripsi Peranan', 'tb']);
		
		if (checkInput(peranan_List))
		{
			$(this).prop("disabled",true);
			var param = $('#form1').serialize();
			$.ajax({
				type: "POST", 
				url: "in_peranan-tambah_insert.php", 
				data: param+ '&tingkap=' + allCheck + '&id=' + "<?=$myID?>",

				success: function(res){
					if(res > 0)
					{
						swal.fire({
							text: "Berjaya Kemaskini Maklumat Peranan",
							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>
	
