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

$myID = fetch_postget_val('id',$_POST, $_GET);
$myID = rtrim($myID, ','); //it will remove a char if the last char is a comma
$tahun = fetch_postget_val('tahun',$_POST, $_GET);
$fasa = fetch_postget_val('fasa',$_POST, $_GET);
$fasatext = fetch_postget_val('fasatext',$_POST, $_GET);
$program = fetch_postget_val('program',$_POST, $_GET);

$PDO_stmt = $PDO->prepare("
	SELECT *
	FROM i_program
	WHERE id_program = ?
");
$PDO_stmt->bindValue(1, $program, PDO::PARAM_INT);
$PDO_stmt->execute();
$program_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);
$eft_table = "m_bayaran_".$program_List['kod_program']."_EFT";
$success_table = "m_bayaran_".$program_List['kod_program'];

$QUERIES = 'SELECT m_pemohon.nama_pemohon, m_pemohon.kpbaru_pemohon, m_pemohon.tkh_daftar, i_caramohon.desk_caramohon, i_carabayar.desk_carabayar, 
			m_pemohon.id_pemohon, m_pemohon.id_dun, m_pemohon.id_carabayar, m_pemohon.id_caramohon, m_pemohon.flag_saheft, 
			m_pemohon.id_statushidup, m_pemohon.ulasan_terima, m_pemohon.ulasan_sah, m_pemohon.id_statusmohon ,
			i_daerah.desk_daerah, i_dun.desk_dun
			FROM m_pemohon LEFT JOIN i_caramohon ON m_pemohon.id_caramohon = i_caramohon.id_caramohon 
			LEFT JOIN i_carabayar ON m_pemohon.id_carabayar = i_carabayar.id_carabayar 
			LEFT JOIN i_statushidup ON m_pemohon.id_statushidup = i_statushidup.id_statushidup 
			LEFT JOIN i_statusmohon ON m_pemohon.id_statusmohon = i_statusmohon.id_statusmohon 
			LEFT JOIN i_dun ON m_pemohon.id_dun = i_dun.id_dun 
			LEFT JOIN i_daerah ON i_dun.id_daerah = i_daerah.id_daerah 
			LEFT JOIN i_parlimen ON i_dun.id_parlimen = i_parlimen.id_parlimen 
			WHERE m_pemohon.id_pemohon IN ('.$myID.') 
			ORDER BY m_pemohon.nama_pemohon
			';
$PDO_stmt = $PDO->prepare($QUERIES);
$PDO_stmt->bindValue(1, $id_program, PDO::PARAM_INT);
$PDO_stmt->execute();
$pemohon_List = [];
while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
{
	$pemohon_List[] = $result;
}
?>
<div class="modal-header">
	<h5 class="modal-title">Pengesahan Pembayaran</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="indicator_form" id="indicator_form">
		<div class="form-body">
			<div class="row">
				<?php
					createLabels('Daerah', 'daerah_bayar', $pemohon_List[0]['desk_daerah'], '', 2, 5, 1, 12);
					createLabels('DUN', 'dun_bayar', $pemohon_List[0]['desk_dun'], '', 2, 5, 1, 12);
					createLabels('Pembayaran', 'pembayaran_bayar', 'RM '.$program_List['jum_bayar'], '', 2, 5, 1, 12);
					createLabels('Tahun Bayaran', 'tahun_bayar', 'Tahun '.$tahun, '', 2, 5, 1, 12);
					//createLabels('Fasa Bayaran', 'fasa_bayar', $fasatext, '', 2, 5, 1, 12);
					createDateTB('Tarikh Bayar', 'tkh_bayar', 'Tarikh Bayar', '', 2, 5, 0, 12);
					createDropDown($PDO, 'Kategori Bayaran', 'i_bayarankat', 'id_bayarankat', 1, 'Sila Pilih Kategori Bayaran', 7, 2, 12);
					createTextArea('Catatan', 'catatan', 'Catatan', '', $readonly, 2, 7, 12, 'text', 5);
				?>
			</div>
			<div class="row">
				<div class="col-md-12">
					<?php
						echo '<table class="table table-striped table-bordered table-hover statTable">';
							echo '<thead>';
								echo '<tr>';
									echo '<th>Bil</th>';
									echo '<th>Nama Pemohon</th>';
									echo '<th>No KP</th>';
									echo '<th>Tarikh Daftar</th>';
									echo '<th>Cara Mohon</th>';
									echo '<th>Cara Bayar</th>';
								echo '</tr>';
							echo '</thead>';
							echo '<tbody>';
								foreach($pemohon_List as $result)
								{
									$counter += 1;
									echo '<tr>';
										echo '<td>'.$counter.'</td>';
										echo '<td>'.$result['nama_pemohon'].'</td>';
										echo '<td>'.$result['kpbaru_pemohon'].'</td>';
										echo '<td>'.$result['tkh_daftar'].'</td>';
										echo '<td>'.$result['desk_caramohon'].'</td>';
										echo '<td>'.$result['desk_carabayar'].'</td>';
									echo '</tr>';
								}
							echo '</tbody>';
						echo '</table>';
					?>
					<div style="text-align:center">
						<button type="button" class="btn btn-primary" id="bayar_simpanBtn">Pastikan Pembayaran</button>
					</div>
				</div>
			</div>
		</div>
	</form>
</div>
<div class="modal-footer">
	<?php
		echo '<button type="button" class="btn btn-secondary" data-dismiss="modal">Tutup</button>';
	?>
</div>

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

	$('.select2').select2({ width: '100%' });
	
	$('.myDatePicker').datepicker({
		//startDate: '-14d',
		endDate: '+0d',
		autoclose: true,
		clearBtn: true,
		format: "dd M yyyy",
		todayHighlight: true,
		orientation: "bottom left",
		templates: {
			leftArrow: '<i class="la la-angle-left"></i>',
			rightArrow: '<i class="la la-angle-right"></i>'
		}
	});
	
	$('#bayar_simpanBtn').click(function() {
		var indicator_list = [[]];
		indicator_list.push(['id_bayarankat', 'Kategori Bayaran', 'dd']);
		indicator_list.push(['tkh_bayar', 'Tarikh Bayar', 'tb']);
		indicator_list.push(['catatan', 'Catatan', 'tb']);
		
		if (checkInput(indicator_list))
		{
			//$(this).prop("disabled",true);
			var param = $('#indicator_form').serialize();
			$.ajax({
				type: "POST", 
				url: "general-pemohon_list_bayar_insert.php", 
				data: param+ '&id=' + "<?=$myID?>"+ '&tahun=' + "<?=$tahun?>"+ '&fasa=' + "<?=$fasa?>"+ '&program=' + "<?=$program?>",

				success: function(res){
					if(res > 0)
					{
						swal.fire({
							text: "Berjaya membuat pembayaran",
							icon: "success",
							buttonsStyling: false,
							confirmButtonText: "OK!",
							customClass: {
								confirmButton: "btn font-weight-bold btn-light-primary"
							}
						}).then(function() {
							//window.parent.reloadTable();
							window.location.reload()
							$("#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>
