<?php 
require('includes/header.php'); 
?>	

<style>
.select2-container--default .select2-results__option[aria-disabled=true] {
    display: none;
}
</style>
<!--begin::Content-->
<div class="content d-flex flex-column flex-column-fluid" id="kt_content">
	<!--begin::Subheader-->
	<div class="subheader py-2 py-lg-4 subheader-solid" id="kt_subheader">
		<div class="container-fluid d-flex align-items-center justify-content-between flex-wrap flex-sm-nowrap">
			<!--begin::Info-->
			<div class="d-flex align-items-center flex-wrap mr-2">
				<!--begin::Page Title-->
				<h5 class="text-dark font-weight-bold mt-2 mb-2 mr-5">Pembayaran Pukal EFT Program Khairat Kematian</h5>
				<!--end::Page Title-->
				<!--begin::Actions-->
				<div class="subheader-separator subheader-separator-ver mt-2 mb-2 mr-4 bg-gray-200"></div>
			</div>
			<!--end::Info-->
		</div>
	</div>
	<!--end::Subheader-->
	<!--begin::Entry-->
	<div class="d-flex flex-column-fluid">
		<!--begin::Container-->
		<div class="container">
			<div class="accordion accordion-solid accordion-panel accordion-svg-toggle" id="accordionExample8">
				<div class="card">
					<div class="card-header" id="headingThree8">
						<div class="card-title" data-toggle="collapse" data-target="#collapseThree8">
							<div class="card-label">													
								<span class="svg-icon svg-icon-primary menu-icon">
									<?=$Tapisanhome_svg?>
								</span>Pembayaran Pukal EFT Program Khairat Kematian</div>
							<span class="svg-icon">
								<?=$Tapisan_svg?>
							</span>
						</div>
					</div>
					<div id="collapseThree8" class="" data-parent="#accordionExample8">
						<div class="card-body" style="padding-top:10px">
							<form id="semakanForm" name="semakanForm">
								<?php				
									echo '<div class="form-group m-form__group row col-md-12" id="tahun_bayar_div">';
									echo '<label class="col-form-label col-md-3" id="tahun">Tahun Bayar</label>';
									createThn_simple('tahun_bayar', date('Y'), false, 3, 0, 1, 0);
									echo '</div>';	
									createTextBox('No KP Baru ', 'kpbaru_pemohon', 'No KP Baru', '', 3, 0, 7, 12, '', 'numonly', 'Sila masukkan No KP Baru','maxlength="12"');
								?>
								<div style="text-align:center;padding-top:20px">
									<button type="button" class="btn btn-primary" id="semakanSubmitBtn">Tambah</button>
								</div>
							</form>
						</div>
					</div>
				</div>
			</div>	
			<div class="card" id="semakanResultOuterDiv" style="display:none">			
				<div id="semakanResultDiv"></div>
			</div>
			<?php
				createModal();
			?>
			<div class="row">
				<?php
					$tbColumn = array();
					//$tbColumn[] = array('Bil', 5);
					$tbColumn[] = array('Nama', 13);
					$tbColumn[] = array('No KP Baru', 11);
					$tbColumn[] = array('No Sijil Mati', 12);
					$tbColumn[] = array('Tarikh Kematian', 10);
					$tbColumn[] = array('Tarikh Daftar Kematian', 10);
					$tbColumn[] = array('Tindakan', 0);
					
					$userBtn = '<span><a class="btn btn-primary font-weight-bolder" id="bayarBtn">Bayar Semua</a></span><span>';
					createTable('program_table_bayar', $tbColumn, 'Senarai Untuk Pembayaran', $info_text, 0, $userBtn, 'ajax-modal2', 12);
				?>
				
			</div>
		</div>
		<!--end::Container-->
		
	</div>
	<!--end::Entry-->
</div>
<!--end::Content-->
					
<?php 
require('includes/footer.php');
?>

<script>
var digit = 12;
$('#kpbaru_pemohon').keyup(function(){
	if(this.value.length == digit){
	console.log('dfds');
		$('#semakanSubmitBtn').click();
	}
});

var tawaranList = [];
var tahun_bayaran = '';
var fasa_bayaran = '';
var fasa_text = '';
$('#bayarBtn').hide();
function tambah(allItem, id, print)
{
	if (typeof(print)==='undefined') print = 0;
	
	if(tawaranList[id] > 0)
	{
		/*
		swal.fire({
			text: "Pemohon telah berada dalam senarai",
			icon: "error",
			buttonsStyling: false,
			confirmButtonText: "OK!",
			customClass: {
				confirmButton: "btn font-weight-bold btn-light-primary"
			}
		}).then(function() {									
		});
		*/
	}
	else
	{
		var o_table = $('#program_table_bayar').DataTable();
		var newData = [];
		
		//newData.push(tawaranList.length + 1);
		newData.push(allItem.nama);
		newData.push(allItem.kpbaru);
		newData.push(allItem.sijil);
		newData.push(allItem.tkh_mati);
		newData.push(allItem.tkh_keyinmati);
		
		var svgicon = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24px" height="24px" viewBox="0 0 24 24" version="1.1"><g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd"><rect x="0" y="0" width="24" height="24"/><circle fill="#000000" opacity="0.3" cx="12" cy="12" r="10"/><rect fill="#000000" x="6" y="11" width="12" height="2" rx="1"/></g></svg>';
		newData.push('<a class="btn btn-sm btn-default btn-text-danger btn-hover-danger btn-icon mr-2" onclick="deleteTawaran(this, '+id+')"><span class="svg-icon svg-icon-danger svg-icon-2x">' + svgicon + '</span></a>');
		var rowNode = o_table.row.add( newData  ).draw().node();
		tawaranList[id] = 1;
		$('#tambahBtn_' + id).hide();
		$('#bayarBtn').show();
	}
}

function deleteTawaran(myBtn, id)
{
	var o_table = $('#program_table_bayar').DataTable();
	var row = myBtn.parentNode.parentNode;
	var myRow = o_table.row( row.rowIndex - 1);
	myRow.remove().draw();
	delete tawaranList[id];
	$('#tambahBtn_' + id).show();
	if(tawaranList.length == 0)
	{
		$('#bayarBtn').hide();
	}
}

$(document).ready(function(){
	
	$('#semakanSubmitBtn').click(function() {
		var indicator_list = [[]];
		indicator_list.push(['kpbaru_pemohon', 'No KP Baru', 'kp']);
		
		if (checkInput(indicator_list))
		{
			$('#semakanSubmitBtn').prop("disabled",true);
			var param = $('#semakanForm').serialize();
			$.ajax({
				type: "POST", 
				url: "general-frontbayar_kh_semak.php", 
				data: param,

				success: function(res){
					console.log(res);
					var allItem = JSON.parse(res);
					console.log(allItem);
					console.log(allItem.id);
					if(allItem.id > 0)
					{
						tambah(allItem, allItem.id, 0);
					}
					else
					{
						swal.fire({
							text: "Pemohon tersebut tidak tersenarai untuk pembayaran.",
							icon: "error",
							buttonsStyling: false,
							confirmButtonText: "OK!",
							customClass: {
								confirmButton: "btn font-weight-bold btn-light-primary"
							}
						}).then(function() {									
						});
					}
					$('#kpbaru_pemohon').val("");
					$('#semakanSubmitBtn').prop("disabled",false);
				} 
			});  //end ajax
		}
		else
		{
			$('#semakanResultDiv').html('');
			$('#semakanResultOuterDiv').hide();
		}
	});			
	
	$('#bayarBtn').click(function(){
		tahun_bayaran = $('#tahun_bayar_year').val();
		var param = '';
		for(var key in tawaranList)
		{
			param += key + ',';
		}
		if(param == '')
		{
			swal.fire({
				text: "Tiada Pemohon dalam Senarai Bayaran",
				icon: "error",
				buttonsStyling: false,
				confirmButtonText: "OK!",
				customClass: {
					confirmButton: "btn font-weight-bold btn-light-primary"
				}
			}).then(function() {									
			});
			return false;
		}
		else
		{
			loadMyModal('se_meninggal-bayar.php?id=' + param + '&tahun=' + tahun_bayaran, '#ajax-modal');
		}
	});
});	
</script>