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

$kp = fetch_postget_val('kp',$_POST, $_GET);
$tahun = fetch_postget_val('tahun',$_POST, $_GET);
$program = fetch_postget_val('program',$_POST, $_GET);
$daerah = fetch_postget_val('daerah',$_POST, $_GET);
$dun = fetch_postget_val('dun',$_POST, $_GET);

$PDO_stmt = $PDO->prepare("
	SELECT *
	FROM i_dun
");
$PDO_stmt->bindValue(1, $kp, PDO::PARAM_INT);
$PDO_stmt->execute();
$dun_List = [];
while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
{
	$dun_List[$result['id_dun']] = $result['desk_dun'];
}

$PDO_stmt = $PDO->prepare("
	SELECT *
	FROM i_kawalan
	WHERE id_program = ?
");
$PDO_stmt->bindValue(1, $program, PDO::PARAM_INT);
$PDO_stmt->execute();
$kawalan_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);
	
$PDO_stmt = $PDO->prepare("
	SELECT *
	FROM m_pemohon
	WHERE kpbaru_pemohon = ?
");
$PDO_stmt->bindValue(1, $kp, PDO::PARAM_INT);
$PDO_stmt->execute();
$pemohon_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

$error = '';
if($pemohon_List['id_pemohon'] == 0)
{
	$error = 'Pemohon tidak wujud dalam sistem.';
}
else if($dun > 0 && $pemohon_List['id_dun'] != $dun)
{
	$error = 'Pemohon Bukan Pengundi DUN '.$dun_List[$dun];
}
else if(strtotime($pemohon_List['tkh_sah']) > strtotime($kawalan_List['tkh_cutoff_tunai']))
{
	$error = 'Pemohon disahkan selepas tarikh cut off program.'.'<br><br>
	Tarikh Pengesahan : '.formatDate($pemohon_List['tkh_sah'],'d M Y').'<br>
	Tarikh Cut Off : '.formatDate($kawalan_List['tkh_cutoff_tunai'],'d M Y');
} 
else
{
	$indicator_Query = "SELECT id_pemohon
					  FROM m_pemohon A
					  WHERE A.id_pemohon = ? ";
					  
	//to check inside all program
	/*$PDO_stmt = $PDO->prepare("
		SELECT *
		FROM i_program
		WHERE aktif = 1
		AND id_program IN (1,2,3,4,7)
	");
	$PDO_stmt->execute();
	while($res = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
	{
		$mbayar_table = "m_bayaran_".$res['kod_program'];
		$indicator_Query .= ' AND A.id_pemohon NOT IN (SELECT id_pemohon FROM '.$mbayar_table.' WHERE tahun_bayar = '.$tahun.') ';
	}*/
		
	$indicator_Query .= ' AND A.id_pemohon NOT IN (SELECT id_pemohon FROM l_penerima) ';
	
	$PDO_stmt = $PDO->prepare($indicator_Query);
	$PDO_stmt->bindValue(1, $pemohon_List['id_pemohon'], PDO::PARAM_INT);
	$PDO_stmt->execute();
	$bayar_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);
	if($bayar_List['id_pemohon'] <= 0)
	{
		//$error = 'Pemohon telah menerima bayaran untuk tahun '.$tahun.' pada '.formatDate($bayar_List['tkh_bayar'], 'Y-m-d');
		$error = 'Pemohon telah menerima bayaran untuk program ini atau program-program lain bagi tahun '.$tahun;
	}
}

?>
<div class="modal-header">
	<h5 class="modal-title">Pengesahan Bayaran Tunai</h5>
	<button type="button" class="close" data-dismiss="modal" aria-label="Close" id="semakcloseBtn" name="semakcloseBtn" ></button>
</div>
<div class="modal-body">
	<form action="javascript:;" class="form-horizontal" name="indicator_form" id="indicator_form">
		<div class="form-body">
			<div class="row">
				<input type="hidden" id="semak_pemohon_id" value="<?=$pemohon_List['id_pemohon']?>">
				<input type="hidden" id="semak_program_id" value="<?=$program?>">
				<input type="hidden" id="semak_program_dun" value="<?=$dun?>">
				<input type="hidden" id="semak_program_daerah" value="<?=$daerah?>">
				<?php
					createLabels('Nama', 'semak_nama', $pemohon_List['nama_pemohon'], '', 3, 6, 1, 12);
					createLabels('No Kad Pengenalan', 'semak_nokp', $pemohon_List['kpbaru_pemohon'], '', 3, 6, 1, 12);
					/*
					createLabels('Program', 'semak_program', '', '', 2, 6, 1, 12);
					createLabels('Tahun Bayaran', 'semak_tahun', '', '', 2, 6, 1, 12);
					createLabels('Fasa Bayaran', 'semak_fasa', '', '', 2, 6, 1, 12);
					createLabels('Tarikh Cut Off', 'semak_cutoff', '', '', 2, 6, 1, 12);
					*/
					
					if($error == '' && $bayar_List['id_bayaran_'.$program_List['kod_program']] == 0)
					{
						$divSize = 12;
						$tbSize = 6;
						$labelsize = 3;
						$name = 'semak_kp';
						$label = 'Pengesahan No Kad Pengenalan';
						$placeholder = 'Pengesahan No Kad Pengenalan';
						echo '<div class="form-group row col-md-'.$divSize.'" id="'.$name.'_div">';
							echo '<label class="col-md-'.$labelsize.' col-form-label" id="'.$name.'_label">'.$label.'</label>';
							echo '<div class="col-md-'.$tbSize.'">';
							/*
								echo '<input type="text" class="form-control m-input" placeholder="'.$placeholder.'" id="'.$name.'" name="'.$name.'" value="'.$value.'"/>';
							echo '</div>';
							echo '<div class="col-md-2">';
								echo '<button type="button" class="btn btn-success" id="semakBtn">Semak</button>';
							echo '</div>';
							*/
							
								echo '<div class="input-group">';
									echo '<input type="text" style="margin-left:0px" class="form-control m-input" placeholder="'.$placeholder.'" id="'.$name.'" name="'.$name.'" value="'.$value.'" oncopy="return false" onpaste="return false" />';
									echo '<div class="input-group-append">';
										echo '<button type="button" class="btn btn-success" id="semakSahBtn">Sah Bayaran</button>';
									echo '</div>';
								echo '</div>';
							
							echo '</div>';
						echo '</div>';
					}
					else
					{
						createLabels('', 'err', '<h4>Pembayaran tidak dapat diteruskan.<br>'.$error.'</h4>', '', 0, 6, 1, 12);
					}
				?>
			</div>
		</div>
	</form>
</div>
<div class="modal-footer">
	<?php
		echo '<button type="button" class="btn btn-secondary" data-dismiss="modal">Tutup</button>';
	?>
</div>

<script>
$('#semak_kp').focus();
var digit = 12;
$('#semak_kp').keyup(function(){
	if(this.value.length == digit){
		$('#semakSahBtn').click();
	}
});

$(document).ready(function(){  
	
	$('#ajax-modal').on('hidden.bs.modal', function () {  // this is to clear modal cache each time modal load
		$('#bayaran_kp').val('');
		$('#bayaran_kp').focus();
	}); 
	
	$('.select2').select2({ width: '100%' }); 
	
	$('#semakSahBtn').click(function() {
		var indicator_list = [[]];
		indicator_list.push(['semak_kp', 'Pengesahan No Kad Pengenalan', 'tb']);
		
		if (checkInput(indicator_list))
		{
			if($('#semak_kp').val() == $('#semak_nokp').html())
			{
				var param = 'kp=' + $('#semak_kp').val();
				param += '&pemohon=' + $('#semak_pemohon_id').val();
				param += '&program=' + $('#semak_program_id').val();
				param += '&dun=' + $('#semak_program_dun').val();
				param += '&daerah=' + $('#semak_program_daerah').val();
				$.ajax({
					type: "POST", 
					url: "tu_senarai-semak_insert.php", 
					data: param,

					success: function(res){
						if(res > 0)
						{
							/*swal.fire({
								text: "Pembayaran Tunai Berjaya",
								icon: "success",
								buttonsStyling: false,
								confirmButtonText: "OK!",
								customClass: {
									confirmButton: "btn font-weight-bold btn-light-primary"
								}
							}).then(function() {
								//$("#semakcloseBtn").click();
								setTimeout(function() {
									$('#semakcloseBtn').click();
								}, 2000);
							});*/
							
							let timerInterval
							Swal.fire({
							  icon: "success",
							  title: 'Pembayaran Tunai Berjaya',
						      confirmButtonText: "OK!",
							  //html: 'I will close in <b></b> milliseconds.',
							  timer: 2000,
							  timerProgressBar: true,
							  didOpen: () => {
								Swal.showLoading()
								timerInterval = setInterval(() => {
								  const content = Swal.getHtmlContainer()
								  if (content) {
									const b = content.querySelector('b')
									if (b) {
									  b.textContent = Swal.getTimerLeft()
									}
								  }
								}, 100)
							  },
							  willClose: () => {
								clearInterval(timerInterval)
							  }
							}).then(function() {
								$("#semakcloseBtn").click();
							});
						}
						else
						{
							swal.fire({
								text: "Error. " + res,
								icon: "error",
								buttonsStyling: false,
								confirmButtonText: "OK!",
								customClass: {
									confirmButton: "btn font-weight-bold btn-light-primary"
								}
							}).then(function() {							
							});
						}
						
					} 
				});  //end ajax
			}
			else
			{
				swal.fire({
					text: "Sila masukkan No Kad Pengenalan yang tepat",
					icon: "error",
					buttonsStyling: false,
					confirmButtonText: "OK!",
					customClass: {
						confirmButton: "btn font-weight-bold btn-light-primary"
					}
				}).then(function() {	
					$('#semak_kp_label').css('color', 'red');
					$('#semak_kp').focus();
				});
			}
		}
	});		
});		
</script>