<?php 
if($_COOKIE['sejahtera_role'] == 10)
{
	include_once ("includes/setup_pemohon.inc.php");
}
else
{
	include_once ("includes/setup.inc.php");
}

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

$indicator_Query = "SELECT A.*,B.desk_program
				  FROM m_anakemas A
				  LEFT JOIN i_program B ON B.id_program = A.id_program
				  WHERE A.id_anakemas = ?";
$PDO_stmt = $PDO->prepare($indicator_Query); 
$PDO_stmt->bindValue(1, $myID, PDO::PARAM_INT);
$PDO_stmt->execute();
$info_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

//get all i_dokumenprogram
$indicator_Query = "SELECT A.*,B.desk_dokumen
				  FROM i_dokumenprogram A
				  LEFT JOIN i_dokumen B ON B.id_dokumen = A.id_dokumen
				  WHERE A.id_program = ?";
$PDO_stmt = $PDO->prepare($indicator_Query);

$PDO_stmt->bindValue(1, $id_program, PDO::PARAM_STR);
$PDO_stmt->execute();
?>
<div class="modal-header">
	<h5 class="modal-title"><b>PENGESAHAN PERMOHONAN</b><br>Pemohon : <b><?=$info_List['nama_anakemas']?></b><br>Program : <b><?=$info_List['desk_program']?></b></h5>
	<button type="button" class="close" data-dismiss="modal" aria-label="Close" id="closeBtn" name="closeBtn" ></button>
</div>
<div class="modal-body">
	<div class="row">
		<div class="col-md-12">		
			<!--begin::Card-->
			<div class="card card-custom gutter-b example-hover">
				<div class="card-body">
					<?php require('general-papar_sub.php');?>
				</div>
			</div>
			<!--end::Card-->										
		</div>
	</div>
	<div class="form-body">
		<div class="row">
			<div class="col-md-12">
				
				<!--begin::Card-->
				<div class="card card-custom gutter-b example-hover">
					<div class="card-body"><b>Dokumen perlu disemak:</b>
					<?php
					
					$indicator_Query = "SELECT A.*,B.desk_dokumen
									  FROM i_dokumenprogram A
									  LEFT JOIN i_dokumen B ON B.id_dokumen = A.id_dokumen
									  WHERE A.id_program = ?
									  ORDER BY A.flag_wajib DESC, A.id_dokumen";
					$PDO_stmt = $PDO->prepare($indicator_Query);
					
					$PDO_stmt->bindValue(1, $id_program, PDO::PARAM_STR);
					$PDO_stmt->execute();
					$num = 0; 
					echo '<table>';
					while($result2 = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
					{		
						$wajib = '';
						if($result2['flag_wajib'] == 1)
						{
							$wajib = ' <b>(WAJIB)</b>';							
						}
						echo '<tr><td>'.($num + 1).'.</td><td>'.$result2['desk_dokumen'].$wajib.'</td></tr>';
						$num += 1;
					}
					echo '</table>';
					?> 
					</div>
				</div>
				<!--end::Card-->
			</div>
		</div>
	</div>
	<form action="javascript:;" class="form-horizontal" name="tind_form" id="tind_form">
		<div class="form-body">
			<div class="row">
				<div class="col-md-12">
					<!--begin::Card-->
					<div class="card card-custom gutter-b example-hover">
						<div class="card-body">
						<?php
							createDropDown($PDO, '<b>Perakuan Pengesahan</b>', 'i_pengesahan', 'id_pengesahan', '', 'Pilih Pengesahan', 4, 2, 12, $readonly);
							createTextArea('<b>Ulasan</b>', 'ulasan_sah', 'Ulasan', '', $readonly, 2, 7, 12, 'text', 5);
						?>
						</div>
					</div>
					<!--end::Card-->
				</div>
			</div>
			<!--<div id="saheft_div">
				<div class="row">
					<div class="col-md-12">
						<div class="card card-custom gutter-b example-hover">
							<div class="card-body">
							<?php							
								createDropDown($PDO, '<b>Perakuan Pengesahan EFT</b>', 'i_perakuan', 'id_perakuan', '', 'Pilih Perakuan', 4, 3, 12, $readonly);
								createTextArea('<b>Ulasan</b>', 'ulasan_saheft', 'Ulasan', '', $readonly, 3, 7, 12, 'text', 5);			
							?>
							</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="tind_simpanBtn">Simpan</button>';
		}
	?>
</div>


<script>
var myuserlevel = "<?=$my_details->user_level?>";

function hideAll()
{
	$('#ulasan_sah_div').hide();
	//$('#saheft_div').hide();
	//$('#ulasan_saheft_div').hide();
}

function showDiv()
{	
	
	if($('#id_pengesahan').val() >= 2)
	{
		$('#ulasan_sah_div').show();
	}
	else if($('#id_pengesahan').val() == 1 && (myuserlevel <= 5 || myuserlevel == 8 || myuserlevel == 9 || myuserlevel == 11 || myuserlevel == 13))
	{
		/*$('#saheft_div').show();		
		if($('#id_perakuan').val() == 2)
		{
			$('#ulasan_saheft_div').show();
		}*/
	}
}

$(document).ready(function(){  
	
	hideAll();
	showDiv();
	
	$('#id_pengesahan').change(function(){
		hideAll();
		showDiv();
	});
	
	/*$('#id_perakuan').change(function(){
		hideAll();
		showDiv();
	});*/

	$('.select2').select2({ width: '100%' }); 
	
	$('#tind_simpanBtn').click(function() {
		
		var pass = "<?=$pass?>";
		var indicator_list = [[]];
		indicator_list.push(['id_pengesahan', 'Pengesahan', 'dd']);
		if($('#id_pengesahan').val() >= 2)
		{
			indicator_list.push(['ulasan_sah', 'Ulasan', 'tb']);
		}
		
		/*if($('#id_perakuan').val() >= 2)
		{
			indicator_list.push(['ulasan_saheft', 'Ulasan', 'tb']);
		}*/
		
		if (checkInput(indicator_list))
		{
			$(this).prop("disabled",true);
			var param = $('#tind_form').serialize();
			
			$.ajax({
				type: "POST", 
				url: "general-sah_tind_sub_insert.php", 
				data: param + '&id=' + "<?=$myID?>",

				success: function(res){
					if(res > 0)
					{
						swal.fire({
							text: "Berjaya membuat tindakan pengesahan",
							icon: "success",
							buttonsStyling: false,
							confirmButtonText: "OK!",
							customClass: {
								confirmButton: "btn font-weight-bold btn-light-primary"
							}
						}).then(function() {
							if(pass == 1)
							{
								window.location.reload(); 								
							}
							else
							{
								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>