<?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
$dok_Query = "SELECT i_dokumen.id_dokumen, i_dokumenprogram.flag_wajib, i_dokumen.desk_dokumen
				FROM i_dokumenprogram 
				LEFT JOIN i_dokumen ON i_dokumenprogram.id_dokumen = i_dokumen.id_dokumen
				WHERE id_program = $id_program";
$dok_Result = $PDO->prepare($dok_Query);
$dok_Result->execute();
// put all into hash for easier comparison
$dokList = array();
$dokWajibList = array();
while($res = $dok_Result->fetch(PDO::FETCH_ASSOC)) {
	$dokList[]= $res;
	if($res['flag_wajib'] == 1)
	{
		$dokWajibList[]= $res['id_dokumen'];
	}
}
?>
<div class="modal-header">
	<h5 class="modal-title"><b>PENGESAHAN PEMBAYARAN EFT</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>
	<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 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>
					<!--end::Card-->
				</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>

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

function showDiv()
{	
	
	if($('#id_perakuan').val() == 2)
	{
		$('#ulasan_saheft_div').show();
	}
}

$(document).ready(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_perakuan', 'Pengesahan', 'dd']);
		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_eft_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>