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

$indicator_Query = "SELECT A.*
				  FROM i_kawalan A
				  WHERE A.id_program = 2";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->execute();
$kawalan_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

$kawalanyear = $kawalan_List['tahun_bayaran_tunai'];

$template5 = 'se_meninggal-warta.php';
$suratBtn = '';
$suratBtn .= '<div class="dropdown dropdown-inline mr-2" id="cetaksuratDiv" style="display:none">';
	$suratBtn .= '<button type="button" class="btn btn-success font-weight-bolder dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">';
	$suratBtn .= 'Cetakan Warta</button>';
	$suratBtn .= '<div class="dropdown-menu dropdown-menu-sm dropdown-menu-right">';
		$suratBtn .= '<ul class="navi flex-column navi-hover py-2">';
			$suratBtn .= '<li class="navi-header font-weight-bolder text-uppercase font-size-sm text-primary pb-2">Pilih Cetakan</li>';
				$suratBtn .= '
				<li class="navi-item">
						<a href="javascript:;" class="navi-link" onclick="cetakSurat(\''.$template5.'\')">
						<span class="navi-text">Cetak Warta</span>
					</a>
				</li>
				';
		$suratBtn .= '</ul>';
	$suratBtn .= '</div>';
$suratBtn .= '</div>';
?>	

<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">Pengurusan Pemohon 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 collapsed" data-toggle="collapse" data-target="#collapseThree8">
							<div class="card-label">													
								<span class="svg-icon svg-icon-primary menu-icon">
									<?=$Tapisanhome_svg?>
								</span>Tapisan</div>
							<span class="svg-icon">
								<?=$Tapisan_svg?>
							</span>
						</div>
					</div>
					<div id="collapseThree8" class="collapse" data-parent="#accordionExample8">
						<div class="card-body"><br>
							<?php
								createDropDown($PDO, 'Parlimen', 'i_parlimen', 'parlimen_sc', $my_details->user_parlimen, 'Semua Parlimen', 6, 2, 12, $readonly);
								createDropDown($PDO, 'Daerah', 'i_daerah', 'daerah_sc', $my_details->user_daerah, 'Semua Daerah', 6, 2, 12, $readonly);
							
								$Query = "SELECT id_dun AS id, desk_dun as name, id_daerah
											FROM i_dun
											WHERE aktif = 1
											ORDER BY susunan";
								$PDO_stmt = $PDO->prepare($Query);
								$PDO_stmt->execute();
								$dunArray = [];
								while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
								{
									$temp = array($result['id'], $result['name'], ' class="all_daerah daerah_'.$result['id_daerah'].'"');		
									$dunArray[] = $temp;
								}
								createDropDown_Array_Special('DUN', $dunArray, 'dun_sc', $my_details->user_dun, 'Semua Kadun', 6, 2, 12, $readonly);
								
								$mySQL = "SELECT id_bangsa AS id, desk_bangsa as name FROM i_bangsa WHERE id_bangsa IN (1,2,3,5) ORDER BY susunan";										
								createDropDown_sql($PDO, 'Bangsa', $mySQL, 'bangsa_sc', 0, 'Semua Bangsa', 6, 2, 12, $readonly,'');
								
								if($my_details->user_level == 8 || $my_details->user_level == 9 || $my_details->user_level == 13)
								{
									$mySQL = "SELECT id_program AS id, desk_program as name
											FROM i_program 
											WHERE id_program = 4
											ORDER BY id_program";									
									createDropDown_sql($PDO, 'Program', $mySQL, 'program_sc', 4, '', 6, 2, 12, $readonly);									
								}
								else
								{
									createDropDown($PDO, 'Program', 'i_program', 'program_sc', $indicator_List['id_program'], 'Semua Program', 6, 2, 12, $readonly);
								}
								
								echo '<div class="form-group m-form__group row col-md-12" id="tahun_mati_div">';
								echo '<label class="col-form-label col-md-2" id="tahun">Tahun Kematian</label>';
								createThn_simple('tahun_mati', '', false, 2, 0, 10, 0);
								echo ' * tahun berkenaan dan ke atas</div>';
								
								echo '<div class="form-group m-form__group row col-md-12" id="tahun_sah_div">';
								echo '<label class="col-form-label col-md-2" id="tahun">Tahun Daftar Kematian</label>';
								createThn_simple('tahun_sah', '', false, 2, 0, 10, 0);
								echo ' * tahun berkenaan dan ke atas</div>';
								
								echo '<div id="telahbayarprogram_div">';
								$myArray = [];
								$myArray[] = array(1, 'BELUM BAYAR');
								$myArray[] = array(2, 'TELAH BAYAR');
								createDropDown_Array('Status Bayaran Program', $myArray, 'telahbayarprogram_sc', '', 'SEMUA STATUS BAYARAN PROGRAM', 6, 2, 12, $readonly);
								createThn('Tahun Pembayaran Program', 'tahun_sc_year', $kawalanyear, 2, 12, 3, 1, 1, 0);
								echo '</div>';
							?>
						</div>
						<div class="row">
							<div class="col-md-12">
								<div class="card-body">
									<?php
										$info_text = 'Senarai Bayaran boleh ditambah sekiranya Pilihan DUN, Status Pengesahan (DISAHKAN), Status Bayaran (Belum Bayar) dan Tahun Pembayaran telah dipilih';
										echo '<h4>'.$info_text.'</h4>';
									?>
								</div>
							</div>
						</div>
						<div class="row">
							<div class="col-md-12">
								<div class="card-body">
									<?php
										
										//createDropDown($PDO, 'DUN *', 'i_dun', 'dun_sc', $my_details->user_dun, 'Semua DUN', 6, 2, 12, $readonly);
										createDropDown($PDO, 'Status Pengesahan', 'i_sahmati', 'sahmati_sc', $indicator_List['id_sahmati'], 'Semua Status', 6, 2, 12, $readonly);
										createDropDown($PDO, 'Kategori Khairat', 'i_khairatkat', 'khairatkat_sc', 0, 'Sila Pilih Kategori Khairat', 6, 2, 12, $readonly);
										$myArray = [];
										$myArray[] = array(1, 'BELUM BAYAR/TIDAK DIBAYAR');
										$myArray[] = array(2, 'TELAH BAYAR');
										createDropDown_Array('Status Bayaran Khairat', $myArray, 'telahbayar_sc', '', 'SEMUA STATUS BAYARAN', 6, 2, 12, $readonly);
										echo '<div class="form-group m-form__group row col-md-12" id="tahun_sc_div">';
										echo '<label class="col-form-label col-md-2" id="tahun">Tahun Pembayaran</label>';
										createThn_simple('tahun_sc', date('Y'), false, 6, 0, 20, 0);
										echo '</div>';
									?>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
			<!--begin::Row-->
			<div class="row">
				<?php
					$tbColumn = array();
					$tbColumn[] = array('Bil', 4);
					$tbColumn[] = array('Tarikh Daftar', 8);
					$tbColumn[] = array('Nama Pemohon', 10);
					$tbColumn[] = array('No KP', 10);
					$tbColumn[] = array('Tarikh Kematian', 8);
					$tbColumn[] = array('No Sijil Mati', 8);
					$tbColumn[] = array('Program', 8);
					$tbColumn[] = array('Status', 8);
					$tbColumn[] = array('Tindakan', 0);
					
					$printOnlyTbColumn = array();
					$printOnlyTbColumn[] = array('Kadun', 9); 
					$printOnlyTbColumn[] = array('Nama Waris', 9); 
					$printOnlyTbColumn[] = array('No KP Waris', 9); 
					$printOnlyTbColumn[] = array('Nama Bank', 9); 
					$printOnlyTbColumn[] = array('No Akaun', 9);
					$printOnlyTbColumn[] = array('No Telefon', 9);
					$printOnlyTbColumn[] = array('Alamat', 10); 
					$printOnlyTbColumn[] = array('Alamat(sambg)', 10); 
					$printOnlyTbColumn[] = array('Alamat(sambg)', 10); 
					$printOnlyTbColumn[] = array('Poskod', 10); 
					$printOnlyTbColumn[] = array('Bandar', 10); 
					$printOnlyTbColumn[] = array('Negeri', 10); 
					
					$userBtn = $suratBtn;
					createTable('indicator_table', $tbColumn, 'Pengurusan Pemohon Khairat Kematian', '', 2, $userBtn, 'ajax-modal', 12);
					
					echo '<input type="hidden" id="table_printonly_header" value=\''.json_encode($printOnlyTbColumn).'\'/>';
				?>
				
			</div>
			<div class="row">
				<?php
					if($my_details->user_level <= 5)
					{
						$userBtn = '<span><a class="btn btn-primary font-weight-bolder" id="bayarBtn">Bayar Semua Khairat Kematian</a></span><span>';
						createTable('program_table_tawar', $tbColumn, 'Senarai Bayaran Khairat Kematian', $info_text, 0, $userBtn, 'ajax-modal2', 12);
					}
				?>
				
			</div>
			<!--end::Row-->
		</div>
		<!--end::Container-->
	</div>
	<!--end::Entry-->
</div>
<!--end::Content-->
					
<?php 
require('includes/footer.php');
?>


<script>
function cetakSurat(file_name)
{
	var dataOption = [];
	dataOption.program_sc = $('#program_sc').val();
	dataOption.tahun_mati = $('#tahun_mati_year').val();
	dataOption.tahun_sah = $('#tahun_sah_year').val();
	dataOption.file_name = file_name;
	console.log(dataOption);
	post(file_name, dataOption);
}

var ajaxUrl = $('#script_base_name').val() + "-ajax.php";
function getData()
{
	return {
		sqlOnly: 0,
		dun_sc: $('#dun_sc').val(),
		parlimen_sc: $('#parlimen_sc').val(),
		daerah_sc: $('#daerah_sc').val(),
		bangsa_sc: $('#bangsa_sc').val(),
		program: $('#program_sc').val(),
		sahmati: $('#sahmati_sc').val(),
		telahbayar_sc: $('#telahbayar_sc').val(),
		telahbayarprogram_sc: $('#telahbayarprogram_sc').val(),
		tahun_sc: $('#tahun_sc_year').val(),
		bayaranfasa_sc: $('#bayaranfasa_sc').val(),
		khairatkat_sc: $('#khairatkat_sc').val(),
		tahun_sah: $('#tahun_sah_year').val(),
		tahun_mati: $('#tahun_mati_year').val(),
	};
}

function reloadTable()
{
	reinitTable($('#table_name').val(), this.ajaxUrl, this.getData(), 1, 0);
}

var tawaranList = [];
var tahun_bayaran = '';
var fasa_bayaran = '';
var fasa_text = '';
$('#bayarBtn').hide();
function tambah(myRow, 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 row = myRow.parentNode.parentNode;
		var o_table = $('#program_table_tawar').DataTable();
		var newData = [];
		//var tolakExceed_msg = "Pemohon " + row.cells[6].innerHTML + " (" + row.cells[5].innerHTML + ") tidak layak untuk ditawar kerana pemohon telah tolak tawaran " + MAX_TOLAK + " kali ke atas. \n\nAdakah anda ingin teruskan dengan tawaran rumah?";
		//var milikRumah_msg = "Pemohon " + row.cells[6].innerHTML + " (" + row.cells[5].innerHTML + ") tidak layak untuk ditawar kerana pemohon / pasangan telah milik rumah atau masih mempunyai tunggakan sewa senggara. \n\nAdakah anda ingin teruskan dengan tawaran rumah?";
		//if( (Number(row.cells[BIL_TOLAK_INDEX].innerHTML) < MAX_TOLAK || confirm(tolakExceed_msg)) && (row.cells[ADA_RUMAH_INDEX].innerHTML == 'TIDAK' || confirm(milikRumah_msg)))
		{
			for(var num = 0; num < row.cells.length - 1; num++)
			{
				newData.push(row.cells[num].innerHTML);
			}
			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_tawar').DataTable();
	var row = myBtn.parentNode.parentNode;
	var myRow = o_table.row( row.rowIndex - 1);
	//if(confirm("Pastikan penghapusan tawaran " + row.cells[6].innerHTML + " ( No KP Baru : " + row.cells[6].innerHTML + ") ?"))
	{
		myRow.remove().draw();
		delete tawaranList[id];
		$('#tambahBtn_' + id).show();
		if(tawaranList.length == 0)
		{
			$('#bayarBtn').hide();
		}
	}
}
$(document).ready(function(){ 

	if($('#program_sc').val() == 0)
	{
		$('#telahbayarprogram_div').hide();
		$('#telahbayarprogram_sc').val(0);
		$('#telahbayarprogram_sc').trigger('change');
		$('#tahun_sc_year').val(0);
		$('#tahun_sc_year').trigger('change');
	}
	else
	{
		$('#telahbayarprogram_div').show();
	}
	
	$('#program_sc').change(function() {
		if($('#program_sc').val() == 0)
		{
			$('#telahbayarprogram_div').hide();
			$('#telahbayarprogram_sc').val(0);
			$('#telahbayarprogram_sc').trigger('change');
			$('#tahun_sc_year').val(0);
			$('#tahun_sc_year').trigger('change');
		}
		else
		{
			$('#telahbayarprogram_div').show();
		}
	});
	$('#program_sc').trigger('change');

	$('#ajax-modal').on('shown.bs.modal', function () {  // this is to clear modal cache each time modal load
	 $(this).removeData('bs.modal');
	}); 
	
	$('#daerah_sc').change(function(){
		$('.all_daerah').attr('disabled', 'disabled');
		$('.daerah_' + $('#daerah_sc').val()).removeAttr('disabled');
		$('#dun_sc').val(0);
		$('#dun_sc').trigger("change");
	});
	$('#daerah_sc').trigger("change");
	
	$('#tahun_sc_div').hide();
	$('#bayaranfasa_sc_div').hide();
	
	$('#telahbayar_sc').change(function(){
		if($('#telahbayar_sc').val() == 1)//belum bayar
		{
			$('#tahun_sc_div').show();
			$('#bayaranfasa_sc_div').hide();
		}
		else if($('#telahbayar_sc').val() == 2)//telah bayar
		{
			$('#tahun_sc_div').hide();
			$('#bayaranfasa_sc_div').show();
		}
		else
		{
			$('#tahun_sc_div').hide();
			$('#bayaranfasa_sc_div').hide();
		}
	});
	
	initTable($('#table_name').val(), ajaxUrl, getData(), 1, 0);
	
	//print function
	$('#printExcel, #printPdf').click(function() {
		printFunc($('#table_name').val(), $(this).attr("value"), "normalPrint.php", $('#script_base_name').val(), $('#table_title').val(), $('#table_header').val(), getData(), $('#table_printonly_header').val(), 'L');
	});
	
	$('#dun_sc,#parlimen_sc,#daerah_sc,#bangsa_sc,#program_sc,#sahmati_sc,#telahbayar_sc,#telahbayarprogram_sc,#tahun_sc_year,#tahun_sah_year,#tahun_mati_year,#bayaranfasa_sc,#khairatkat_sc').change(function(){
		reloadTable();
		tahun_bayaran = $('#tahun_sc_year').val();
		
		if($('#program_sc').val() > 0 && $('#tahun_mati_year').val() > 0 && $('#tahun_sah_year').val() > 0 && $('#khairatkat_sc').val() == 1)
		{
			$('#cetaksuratDiv').show();
		}
		else
		{
			$('#cetaksuratDiv').hide();
		} 
		//fasa_bayaran = $('#bayaranfasa_sc').val();
		//fasa_text = $("#bayaranfasa_sc option:selected").text();
	});
	
	$('#bayarBtn').click(function(){
		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 + /*'&fasa=' + fasa_bayaran + '&fasatext=' + encodeURI(fasa_text) +*/ '&program=' + "<?=$program_List['id_program']?>", '#ajax-modal');
		}
	});
});
</script>