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

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

$program_Query = "SELECT *
				  FROM i_program
				  WHERE id_program = $myID";
$program_Result = $PDO->prepare($program_Query);
$program_Result->execute();
$program_List = $program_Result->fetch(PDO::FETCH_ASSOC);

//get all borangdiv
$indicator_Query = "SELECT *
				  FROM u_borangdiv 
				  ORDER BY susunan";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->execute();
$borangDivList = [];
while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
{
	$borangDivList[$result['id_borangdiv']] = $result;
}

//get all inputtype
$indicator_Query = "SELECT *
				  FROM u_inputtype";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->execute();
$inputTypeList = [];
while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
{
	$inputTypeList[$result['id_inputtype']] = $result['desk_inputtype'];
}

//get all borangitem
$indicator_Query = "SELECT *
				  FROM u_borangitem 
				  WHERE aktif = 1
				  ORDER BY susunan";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->execute();
$borangItemList = [];
$borangItemActionList = [];
while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
{
	if (substr($inputTypeList[$result['id_inputtype']], 0, 6) === 'Action')
	{
		$borangItemActionList[$result['id_borangdiv']][] = $result;
	}
	else
	{
		$borangItemList[$result['id_borangdiv']][] = $result;
	}
}

//get all program borangitem
$borangQuery = "SELECT id_borangitem, susunan, is_check_input
				FROM u_pautanborang 
				WHERE id_program = $myID";
$borang_Result = $PDO->prepare($borangQuery);
$borang_Result->execute();
// put all into hash for easier comparison
$borangPages = array();
while($res = $borang_Result->fetch(PDO::FETCH_ASSOC)) {
	$borangPages[$res['id_borangitem']]= $res;
}


//get all dokumen
$dok_Query = "SELECT *
				  FROM i_dokumen 
				  ORDER BY susunan";
$PDO_stmt = $PDO->prepare($dok_Query);
$PDO_stmt->execute();
$dokumenList = [];
while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
{
	$dokumenList[] = $result;
}

//get all i_dokumenprogram
$dok_Query = "SELECT id_dokumen, flag_wajib
				FROM i_dokumenprogram 
				WHERE id_program = $myID";
$dok_Result = $PDO->prepare($dok_Query);
$dok_Result->execute();
// put all into hash for easier comparison
$dokWajibList = array();
while($res = $dok_Result->fetch(PDO::FETCH_ASSOC)) {
	$dokWajibList[$res['id_dokumen']]= $res;
}

?>

<div class="modal-header">
	<h5 class="modal-title">Pengurusan Daftar Borang</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 Bantuan', 'desk_program', 'Deskripsi Bantuan', $program_List['desk_program'], 3, $readonly, 7, 12);
					createTextBox('Kod Bantuan', 'kod_program', 'Kod Program', $program_List['kod_program'], 3, $readonly, 7, 12);
					createTextArea('Penerangan Bantuan', 'penerangan', 'Sila nyatakan keterangan bantuan', $program_List['penerangan'], 0, 3, 7, 12, '', 5);
					createTextArea('Syarat Bantuan', 'syarat', 'Sila nyatakan syarat bantuan', $program_List['syarat'], 0, 3, 7, 12, '', 5);
					//createDropDown($PDO, 'Mod Borang', 'i_modborang', 'id_modborang', $program_List['id_modborang'], 'Pilih Mod Borang', 7, 3, 12, $readonly);
					//createTextBox('Jumlah Bayaran', 'jum_bayar', 'Jumlah Bayaran', $program_List['jum_bayar'], 3, $readonly, 7, 12, '', 'numonly');
					createTextBox('Susunan', 'susunan', 'Susunan', $program_List['susunan'], 3, $readonly, 7, 12);
					
					$flag_aktif = 1;
					if($myID > 0)
					{
						$flag_aktif = $program_List['aktif'];						
					}					
					$myArray = [];
					$myArray[] = array(1, 'YA');
					createDropDown_Array('Aktif', $myArray, 'aktif', $flag_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 Daftar Borang</h3>
							</div>
						</div>
						<div class="card-body">
							<?php
								echo '<ul class="nav nav-tabs" role="tablist">';
									$counter = 1;
									foreach($borangDivList 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['title'].'</span>';
											echo '</a>';
										echo '</li>';
										$counter++;
									}
								echo '</ul>';
								echo '<div class="tab-content">';
									$counter = 1;
									foreach($borangDivList 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 $menu['title'];
														echo '</h3>';
													echo '</div>';
												echo '</div>';
												echo '<div class="card-body">';
													echo '<div class="card card-custom">';
														echo '<div class="card-header">';
															echo '<div class="card-title">';
																echo '<h3 class="card-label">';
																	echo 'Data Input & Compulsary Checking';
																echo '</h3>';
															echo '</div>';
														echo '</div>';
														echo '<div class="card-body">';
															echo '<div class="checkbox-list">';
																foreach($borangItemList[$key] as $key2 => $menu2) 
																{
																	echo '<label class="checkbox '.($readonly?'checkbox-disabled':'').'">';
																		echo '<input class="normal" type="checkbox" '.($readonly?'disabled="disabled"':'').' '.(array_key_exists($menu2['id_borangitem'], $borangPages)?'checked="checked"':'').' id="borangitem_'.$menu2['id_borangitem'].'" name="borangitem_'.$menu2['id_borangitem'].'">';
																		echo '<span></span>';
																			echo '<label class="checkbox '.($readonly?'checkbox-disabled':'').'" style="padding-left:15px;padding-right:15px">';
																				echo '<span class="switch switch-sm switch-outline switch-icon switch-success">';
																					echo '<label>';
																						echo '<input class="checking" type="checkbox" '.($readonly?'disabled="disabled"':'').' '.((array_key_exists($menu2['id_borangitem'], $borangPages)&&$borangPages[$menu2['id_borangitem']]['is_check_input']==1)?'checked="checked"':'').' id="borangitemCheck_'.$menu2['id_borangitem'].'" name="borangitemCheck_'.$menu2['id_borangitem'].'"/>';
																						echo '<span></span>';
																					echo '</label>';
																				echo '</span>';
																			echo '</label>';
																		echo '['.$inputTypeList[$menu2['id_inputtype']].'] - '.$menu2['label_show'];
																	echo '</label>';
																}
															echo '</div>';
														echo '</div>';
													echo '</div>';
													if(count($borangItemActionList[$key]) > 0)
													{
														echo '<div class="card card-custom">';
															echo '<div class="card-header">';
																echo '<div class="card-title">';
																	echo '<h3 class="card-label">';
																		echo 'Action';
																	echo '</h3>';
																echo '</div>';
															echo '</div>';
															echo '<div class="card-body">';
																echo '<div class="checkbox-list">';
																	foreach($borangItemActionList[$key] as $key2 => $menu2) 
																	{
																		echo '<label class="checkbox '.($readonly?'checkbox-disabled':'').'">';
																			echo '<input class="normal" type="checkbox" '.($readonly?'disabled="disabled"':'').' '.(array_key_exists($menu2['id_borangitem'], $borangPages)?'checked="checked"':'').' id="borangitem_'.$menu2['id_borangitem'].'" name="borangitem_'.$menu2['id_borangitem'].'">';
																			echo '<span></span>';
																			echo '['.$inputTypeList[$menu2['id_inputtype']].'] - '.$menu2['label_show'];
																		echo '</label>';
																	}
																echo '</div>';
															echo '</div>';
														echo '</div>';
													}
												echo '</div>';
											echo '</div>';
																
										echo '</div>';
										$counter++;
									}
								echo '</div>';
								
							?>
						</div>
					</div>
				</div>
			</div>-->
			<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">Dokumen Pengesahan Permohonan</h3>
							</div>
						</div>
						<div class="card-body">
							<?php
								echo '<div class="checkbox-list">';
									foreach($dokumenList as $key2 => $menu2) 
									{
										echo '<label class="checkbox '.($readonly?'checkbox-disabled':'').'">';
											echo '<input class="normal_dok" type="checkbox" '.($readonly?'disabled="disabled"':'').' '.(array_key_exists($menu2['id_dokumen'], $dokWajibList)?'checked="checked"':'').' id="dokitem_'.$menu2['id_dokumen'].'" name="dokitem_'.$menu2['id_dokumen'].'">';
											echo '<span></span>';
												echo '<label class="checkbox '.($readonly?'checkbox-disabled':'').'" style="padding-left:15px;padding-right:15px">';
													echo '<span class="switch switch-sm switch-outline switch-icon switch-success">';
														echo '<label>';
															echo '<input class="wajib_dok" type="checkbox" '.($readonly?'disabled="disabled"':'').' '.((array_key_exists($menu2['id_dokumen'], $dokWajibList)&&$dokWajibList[$menu2['id_dokumen']]['flag_wajib']==1)?'checked="checked"':'').' id="dokWajib_'.$menu2['id_dokumen'].'" name="dokWajib_'.$menu2['id_dokumen'].'"/>';
															echo '<span></span>';
														echo '</label>';
													echo '</span>';
												echo '</label>';
											echo $menu2['desk_dokumen'];
										echo '</label>';
									}
								echo '</div>';
							?>
						</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="program_simpanBtn">Simpan</button>';
		}
	?>
	
</div>

<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%' }); 
	
	tinymce.remove(); //need remove 1st before init again
	tinymce.init({
		selector: '#penerangan, #syarat',
		menubar: false,
		toolbar: ['undo redo | styleselect | bold italic underline strikethrough | bullist numlist | outdent indent |',
				'alignleft aligncenter alignright alignjustify | forecolor backcolor | preview | link'],
		plugins : 'advlist lists preview link'
	});
	
	$('#program_simpanBtn').click(function() {
		
		var allCheck = $('#form2 input:checkbox[class=normal]').serialize();
		//console.log(allCheck);
		allCheck = allCheck.replace(/\&/g, "");
		allCheck = allCheck.replace(/borangitem\_/g, "");
		allCheck = allCheck.replace(/=on/g, ",");
		//console.log(allCheck);
		
		var allChecking = $('#form2 input:checkbox[class=checking]').serialize();
		//console.log(allChecking);
		allChecking = allChecking.replace(/\&/g, "");
		allChecking = allChecking.replace(/borangitemCheck\_/g, "");
		allChecking = allChecking.replace(/=on/g, ",");
		//console.log(allChecking);
		
		//dokumen
		var allNormalDok = $('#form2 input:checkbox[class=normal_dok]').serialize();
		//console.log(allCheck);
		allNormalDok = allNormalDok.replace(/\&/g, "");
		allNormalDok = allNormalDok.replace(/dokitem\_/g, "");
		allNormalDok = allNormalDok.replace(/=on/g, ",");
		//console.log(allCheck);
		
		var allWajibDok = $('#form2 input:checkbox[class=wajib_dok]').serialize();
		//console.log(allChecking);
		allWajibDok = allWajibDok.replace(/\&/g, "");
		allWajibDok = allWajibDok.replace(/dokWajib\_/g, "");
		allWajibDok = allWajibDok.replace(/=on/g, ",");
		//console.log(allChecking);
			
		var indicator_list = [[]];
		indicator_list.push(['desk_program', 'Deskripsi Prpgram', 'tb']);
		indicator_list.push(['kod_program', 'Kod Prpgram', 'tb']);
		indicator_list.push(['penerangan', 'Penerangan Program', 'tinymce']);
		indicator_list.push(['syarat', 'Syarat Program', 'tinymce']);
		indicator_list.push(['jum_bayar', 'Jumlah Bayaran', 'num']);
		//indicator_list.push(['aktif', 'Aktif', 'dd']);
		
		if (checkInput(indicator_list))
		{
			$(this).prop("disabled",true);
			var penerangan_content = tinymce.get('penerangan').getContent();
			var syarat_content = tinymce.get('syarat').getContent();
			var param = $('#form1').serialize();
			param += '&penerangan_content=' + encodeURIComponent(penerangan_content);
			param += '&syarat_content=' + encodeURIComponent(syarat_content);
			param += '&id=' + "<?=$myID?>";
			param += '&borangitem=' + allCheck;
			param += '&checkingitem=' + allChecking;
			param += '&normalDok=' + allNormalDok;
			param += '&wajibDok=' + allWajibDok;
			$.ajax({
				type: "POST", 
				url: "in_program-tambah_insert.php", 
				data: param,

				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.parent.reloadTable();
							$("#closeBtn").click();
							//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>
	
