<?php
$readonly = 0;

//get program from pemohon
$indicator_Query = "SELECT *
					FROM m_pemohon
					WHERE id_pemohon = ?";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->bindValue(1, $myID, PDO::PARAM_INT);
$PDO_stmt->execute();

$pemohon_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

$id_pemohon = $pemohon_List['id_pemohon'];

if($pemohon_List['id_program'] > 0)
{
	$title = 'PEMOHONAN PERTUKARAN PROGRAM<br>Pemohon : <b>'.$pemohon_List['nama_pemohon'];
	$daftar_button = '<button type="button" class="btn btn-success" onclick="tukarProgram()" >Tukar Program Penghargaan</button>';
}
else
{
	$title = 'PEMOHONAN PROGRAM<br>Pemohon : <b>'.$pemohon_List['nama_pemohon'];
	$daftar_button = '<button type="button" class="btn btn-success" onclick="daftarFunc()" >Mohon Program Penghargaan</button>';
}
?>

<div class="modal-header">
	<h5 class="modal-title"><b><?=$title?></b></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="program_form" id="program_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
							$indicator_Query = "SELECT id_program AS id, desk_program as name 
												FROM i_program WHERE aktif = 1 
												AND id_modborang IN (1,2)
												AND id_program NOT IN (5,6)
												ORDER BY susunan";
							$PDO_stmt = $PDO->prepare($indicator_Query);
							$PDO_stmt->execute();
							$program_List = [];
							while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
							{
								$program_List[$result['id']] = $result['name'];
							}							

							$icno = $pemohon_List['kpbaru_pemohon'];
							$year = substr($icno , 0, 2);
							$year = (($year < date('y'))?('20'.$year):('19'.$year));
							$month = substr($icno , 2, 2);
							$day = substr($icno , 4, 2);							
							
							$age = dateDifference(date('Y-m-d') , $year.'-'.$month.'-'.$day, '%y' );
							
							//var_dump($age);
							//var_dump($icno);
							
							//FOR allowed program inside penang
							if($my_details->user_level == 1)
							{								
								$programAllow = array(1,2,4,7);
							}
							else
							{
								if($age < 18)
								{
									$programAllow = array(4);
								}
								else if($pemohon_List['kpbaru_pemohon'] % 2 == 0) //even number, perempuan
								{
									if($age <= 21)
									{
										$programAllow = array(4);
									}
									else if($age > 59)
									{
										$programAllow = array(2,4);
									}
									else
									{
										$programAllow = array(1,4,7);
									}
								}
								else
								{
									if($age > 59)
									{
										$programAllow = array(2,4);
									}
									else
									{
										$programAllow = array(4);
									}
								}
							}
							//END allowed program inside penang
							/*
							$programAllowList = [];
							$programAllowList['perempuan_kadun'] = array(1,2,4,5,6,7);
							$programAllowList['lelaki_kadun'] = array(2,4,5,6);
							$programAllowList['all_nonkadun'] = array(4, 6);
							
							if($kpbaru % 2 == 0) //even number, perempuan
							{
								$programAllow = $programAllowList['perempuan_kadun'];
							}
							else
							{
								$programAllow = $programAllowList['lelaki_kadun'];
							}
							*/
							
							$allow = [];
							$not_allow = [];
							$nonkadun_allow = [];
							$nonkadun_not_allow = [];
							$programAllowList = [];
							$programAllowList['all_nonkadun'] = array(4); //FOR allowed program outside penang
							foreach($program_List as $key=>$item)
							{
								$text = $item;
								$extra = '';
								if(!in_array($key, $programAllow))
								{
									$text = $text.' (TIDAK LAYAK)';
									$extra = 'class="tidaklayak" disabled ';
									$not_allow[] = array($key, $text, $extra);
								}
								else
								{
									if($key == $pemohon_List['id_program']) //existing program, dont allow choose again
									{
										$text = $text.' (PROGRAM SEMASA)';
										$extra = 'class="tidaklayak" disabled ';
										$not_allow[] = array($key, $text, $extra);
									}
									else
									{
										$allow[] = array($key, $text, $extra);
									}
								}
								
								//for pelajar
								if(!in_array($key, $programAllowList['all_nonkadun']))
								{
									$text = $text.' (TIDAK LAYAK)';
									$extra = 'class="tidaklayak" disabled ';
									$nonkadun_not_allow[] = array($key, $text, $extra);
								}
								else
								{
									$nonkadun_allow[] = array($key, $text, $extra);
								}
							}
							
							$my_sql = "SELECT id_dun AS id, CONCAT(kadun, ' - ', desk_dun) as name FROM i_dun ORDER BY id";
							createDropDown_sql_special_style($PDO, 'KADUN MENGUNDI', $my_sql, 'kadun', 0, 'SILA PILIH KADUN MENGUNDI', 12, $readonly, '', 'onchange="selectKadun()"');
							
							$myArray = [];
							foreach($allow as $key=>$item)
							{
								$myArray[] = $item;
							}
							foreach($not_allow as $key=>$item)
							{
								$myArray[] = $item;
							}
							createDropDown_Array_special_style('PROGRAM PENGHARGAAN', $myArray, 'program_kadun', 0, 'SILA PILIH PROGRAM PENGHARGAAN', 12, $readonly, '', 'onchange="selectOKU(\'program_kadun\')"');
							
							$myArray = [];
							foreach($nonkadun_allow as $key=>$item)
							{
								$myArray[] = $item;
							}
							foreach($nonkadun_not_allow as $key=>$item)
							{
								$myArray[] = $item;
							}
							createDropDown_Array_special_style('PROGRAM PENGHARGAAN', $myArray, 'program_nonkadun', 0, 'SILA PILIH PROGRAM PENGHARGAAN', 12, $readonly, 'display:none', 'onchange="selectOKU(\'program_nonkadun\')"');
							
							echo '<div id="okuDiv" style="display:none">';
								$my_sql = "SELECT id_oku AS id, CONCAT(kod_oku, ' - ', desk_oku) as name FROM i_oku ORDER BY id";
								createDropDown_sql_special_style($PDO, 'KATEGORI OKU', $my_sql, 'id_oku', 0, 'SILA PILIH KATEGORI OKU', 12, $readonly, '');
								
								createTextBox_special_style('NOMBOR OKU', 'no_oku', 'NOMBOR OKU', '', 0, 0, 0, 12);
								$my_sql = "SELECT id_daerah AS id, desk_daerah as name FROM i_daerah WHERE id_daerah != 6 ORDER BY id";
								createDropDown_sql_special_style($PDO, 'DAERAH MENDAFTAR OKU', $my_sql, 'id_daerahOKU', 0, 'SILA PILIH DAERAH MENDAFTAR OKU', 12, $readonly, '');
							echo '</div>';
						?>
						</div>
					</div>
					<!--end::Card-->
				</div>
			</div>
		</div>
	</form>
</div>
<div class="modal-footer">
	<?php
		echo $daftar_button;
		echo '<button type="button" class="btn btn-secondary" data-dismiss="modal">Tutup</button>';
	?>
</div>

<!--end::Accordion-->
<script>

function selectKadun()
{
	console.log('select kadun');
	if($('#kadun').val() == 1) //not kadun
	{
		$('#program_kadun_div').hide();
		$('#program_nonkadun_div').show();
	}
	else
	{
		$('#program_kadun_div').show();
		$('#program_nonkadun_div').hide();
	}
}
function selectOKU(select_name)
{
	if($('#' + select_name).val() == 4)
	{
		$('#okuDiv').show();
	}
	else
	{
		$('#okuDiv').hide();
	}
}

function daftarFunc(program=0)
{
	var ddname = 'program_kadun';
	if($('#kadun').val() == 1) //not kadun
	{
		ddname = 'program_nonkadun';
	}
	
	var indicator_list = [[]];
	indicator_list.push(['kadun', 'Kadun Mengundi. Untuk Program OKU, sila pilih Kadun tempat tinggal yang akan digunakan untuk penghantaran dokumen ke Pejabat Kebajikan Masyarat Daerah berhampiran dengan Kadun dipilih.', 'dd']);
	indicator_list.push([ddname, 'PROGRAM PENGHARGAAN', 'dd']);
	if($('#' + ddname).val() == 4)
	{
		indicator_list.push(['id_oku', 'KATEGORI OKU', 'dd']);
		indicator_list.push(['no_oku', 'NOMBOR OKU', 'tb']);
		indicator_list.push(['id_daerahOKU', 'DAERAH MENDAFTAR OKU', 'dd']);
	}
	if (program==5 || checkInput(indicator_list))
	{
		$.ajax({
		   type: "POST",
		   url: "<?=$insertURL?>", 
		   data: 'type=' + "<?=$type?>" + '&kadun=' + $('#kadun').val() + '&id_oku=' + $('#id_oku').val() + '&id_daerahOKU=' + $('#id_daerahOKU').val() + '&no_oku=' + $('#no_oku').val() + '&program=' + $('#' + ddname).val(),
		   
		   success: function(res){
				window.location.reload();
		   }
	   });  //end ajax
			window.location.reload();
		$('#daftarBtn').prop("disabled",true);
		var dataObject = [];
		//dataObject['pmh'] = 0;
		//dataObject['kp'] = $('#kpbaru').val();
		dataObject['dun'] = $('#kadun').val();
		dataObject['id_oku'] = $('#id_oku').val();
		dataObject['id_daerahOKU'] = $('#id_daerahOKU').val();
		dataObject['no_oku'] = $('#no_oku').val();
		dataObject['program'] = ((program > 0) ? program : $('#' + ddname).val() );
		post('pub_borang.php', dataObject, '_self');
	}
}

function tukarProgram()
{
	var ddname = 'program_kadun';
	if($('#kadun').val() == 1) //not kadun
	{
		ddname = 'program_nonkadun';
	}
	
	var indicator_list = [[]];
	indicator_list.push(['kadun', 'KADUN MENGUNDI', 'dd']);
	indicator_list.push([ddname, 'PROGRAM PENGHARGAAN', 'dd']);
	if($('#' + ddname).val() == 4)
	{
		indicator_list.push(['id_oku', 'KATEGORI OKU', 'dd']);
		indicator_list.push(['no_oku', 'NOMBOR OKU', 'tb']);
		indicator_list.push(['id_daerahOKU', 'DAERAH MENDAFTAR OKU', 'dd']);
	}
	if (checkInput(indicator_list))
	{
		Swal.fire({
			title: "Pastikah Anda?",
			text: "Pastikah untuk tukar program kepada '" + $( "#" + ddname +" option:selected" ).text() + "' ?",
			icon: "warning",
			showCancelButton: true,
			confirmButtonText: "Ya",
			cancelButtonText: "Tidak",
		}).then(function(result) {
			if (result.value) {
				$.ajax({
				   type: "POST",
				   url: "<?=$insertURL?>", 
				   data: 'type=' + "<?=$type?>" + '&id_pemohon=' + "<?=$id_pemohon?>" + '&kadun=' + $('#kadun').val() + '&id_oku=' + $('#id_oku').val() + '&id_daerahOKU=' + $('#id_daerahOKU').val() + '&no_oku=' + $('#no_oku').val() + '&program=' + $('#' + ddname).val(),
				   
				   success: function(res){
						window.location.reload();
				   }
			   });  //end ajax
			}
		});
	}
}

$(document).ready(function(){ 
$('.select2').select2({ width: '100%' }); 
		$('#program_kadun_div').hide();
		$('#program_nonkadun_div').hide();
});		
</script>