<?php 
if($_COOKIE['sejahtera_role'] == 10)
{
	include_once ("includes/setup_pemohon.inc.php");
}
else
{
	include_once ("includes/setup.inc.php");
}

$myID = fetch_postget_val('id',$_POST, $_GET);
$readonly = 0;

$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);

$indicator_Query = "SELECT *
					FROM m_waris
					WHERE id_pemohon = ?
					AND status = 1";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->bindValue(1, $myID, PDO::PARAM_INT);
$PDO_stmt->execute();
$waris_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

$id_program = $pemohon_List['id_program'];
?>

<div class="modal-header">
	<h5 class="modal-title">Pertukaran Ke Rumah Orang Tua<br>Pemohon : <b><?=$pemohon_List['nama_pemohon']?></b><br>No KP : <b><?=$pemohon_List['kpbaru_pemohon']?></h5>
	<button type="button" class="close" data-dismiss="modal" aria-label="Close" id="closeBtn" name="closeBtn" ></button>
</div>
<div class="modal-body">
	<input type="hidden" id="dz_myID" value="<?=$myID?>">
	<form action="javascript:;" class="form-horizontal" name="indicator_form" id="indicator_form">
		<div class="form-body">
			<div class="row">
				<div class="col-md-12">
					<ul class="nav nav-tabs" id="myTab" role="tablist">
						<li class="nav-item">
							<a class="nav-link active" id="info-tab" data-toggle="tab" href="#info">
								<span class="nav-text">Maklumat Rumah Orang Tua</span>
							</a>
						</li>
						<li class="nav-item">
							<a class="nav-link <?php echo ($myID > 0)?'':'disabled'; ?>" id="waris-tab" data-toggle="tab" href="#waris">
								<span class="nav-text">Maklumat Waris</span>
							</a>
						</li>
					</ul>
					<div class="tab-content" id="myTabContent">
						<div class="tab-pane fade show active" id="info" role="tabpanel" aria-labelledby="info-tab">
							<div class="row">
								<div class="col-md-12" style="margin-top: 20px;">
									<?php
										createDropDown($PDO, 'Rumah Orang Tua', 'i_rumahorgtua', 'id_rumahorgtua', $pemohon_List['id_rumahorgtua'], 'Pilih Rumah Orang Tua', 7, 3, 12, $readonly);
										createTextArea('Ulasan', 'ulasan_rumahorgtua', 'Sila nyatakan ulasan', $pemohon_List['ulasan_rumahorgtua'], 0, 3, 7, 12, '', 10);			
										if($readonly == 0)
										{
											echo '<div style="text-align:center;margin-top: 10px;">';
												echo '<button type="button" class="btn btn-primary" id="simpaninfoBtn">Seterusnya</button>';
											echo '</div>';
										}						
									?>
								</div>
							</div>
						</div>
						<div class="tab-pane fade show" id="waris" role="tabpanel" aria-labelledby="waris-tab">
							<div class="row">
								<div class="col-md-12" style="margin-top: 20px;">
									<?php
										createTextBox('Nama Waris', 'nama_waris', 'Nama Waris', $waris_List['nama_waris'], 3, $readonly, 7, 12, '', 'text', 'Sila masukkan nama waris');
										createTextBox('No KP Waris', 'kpbaru_waris', 'No KP Waris', $waris_List['kpbaru_waris'], 3, $readonly, 7, 12, '', 'numonly', 'Sila masukkan No KP Waris','maxlength="12"');
										createTextBox('Alamat', 'alamat_waris', 'Alamat', $waris_List['alamat_waris'], 3, 0, 7, 12, '', 'text');
										createTextBox('', 'alamat_waris2', 'Alamat', $waris_List['alamat_waris2'], 3, 0, 7, 12, '', 'text');
										createTextBox('', 'alamat_waris3', 'Alamat', $waris_List['alamat_waris3'], 3, 0, 7, 12, '', 'text');
										
										$mySQL = "SELECT id_poskod AS id, CONCAT(desk_poskod, '-', bandar) as name, bandar AS cTag1, id_negeri AS cTag2 FROM i_poskod ORDER BY id_poskod";
										
										createDropDown_sql($PDO, 'Poskod', $mySQL, 'id_poskodwaris', $waris_List['id_poskodwaris'], 'Poskod', 4, 3, 12, $readonly);
										createTextBox('Bandar', 'bandar_waris', 'Bandar', $waris_List['bandar_waris'], 3, 0, 4, 12, '', 'text');
										createDropDown($PDO, 'Negeri', 'i_negeri', 'id_negeriwaris', $waris_List['id_negeriwaris'], 'Negeri', 4, 3, 12, $readonly);
										createTextBox('Tel HP', 'tel_hpwaris', 'Tel HP', $waris_List['tel_hpwaris'], 3, 0, 4, 12, '', 'numonly', '','maxlength="11"');
										createTextBox('Tel Rumah', 'tel_rumahwaris', 'Tel Rumah', $waris_List['tel_rumahwaris'], 3, 0, 4, 12, '', 'numonly', '','maxlength="9"');
										createTextBox('Emel', 'emel_waris', 'Emel', $waris_List['emel_waris'], 3, 0, 4, 12, '', 'text');
										
										$mySQL = "SELECT id_bank AS id, desk_bank as name, digit_bank_min AS cTag1, digit_bank AS cTag2 FROM i_bank ORDER BY susunan";
										
										createDropDown_sql($PDO, 'Bank', $mySQL, 'id_bankwaris', $waris_List['id_bankwaris'], 'Bank', 4, 3, 12, $readonly);
										createTextBox('No Akaun', 'no_akaunwaris', 'No Akaun', $waris_List['no_akaunwaris'], 3, 0, 4, 12, '', 'text');								
									?>
								
								</div>
							</div>
							<div class="row" style="text-align:center;margin-top: 10px;">
								<div class="col-md-12" style="margin-top: 20px;">
								
									<?php
										if($readonly == 0)
										{
											echo '<button type="button" class="btn btn-primary" id="indicator_simpanBtn">Hantar Pertukaran</button>';
										}						
									?>
								
								</div>
							</div>
						</div>
					</div>
				</div>
				
			</div>
		</div>
	</form>
</div>
<div class="modal-footer">
	<?php
		echo '<button type="button" class="btn btn-secondary" data-dismiss="modal">Tutup</button>';
		if($readonly == 0)
		{
			//echo '<button type="button" class="btn btn-primary" id="indicator_simpanBtn">Hantar</button>';
		}
	?>
</div>

<script>

$(document).ready(function(){  
	$('.kp_tb, .numonly, #kpbaru_waris, #tel_hpwaris, #tel_rumahwaris, #no_akaunwaris').keypress(function(evt){
		evt = (evt) ? evt : window.event;
		var charCode = (evt.which) ? evt.which : evt.keyCode;
		if ((charCode >= 48 && charCode <= 57)) {
			return true;
		}
		return false;
	});
	
	$("#id_poskodwaris").change(function(){ 
		if($("#id_poskodwaris").val() > 0)
		{
			var element = $(this).find('option:selected'); 
			var cTag1 = element.attr("cTag1"); 
			var cTag2 = element.attr("cTag2"); 

			$('#bandar_waris').val(cTag1); 
			$('#id_negeriwaris').val(cTag2); 
			$('#id_negeriwaris').trigger('change');
			$('#bandar_waris').prop('disabled', true);
			$('#id_negeriwaris').prop('disabled', true);
		}
	}); 
	$('#id_poskodwaris').trigger('change');

	$('#id_bankwaris').change(function(){
		var element = $(this).find('option:selected'); 
		var cTag1 = element.attr("cTag1"); 
		var cTag2 = element.attr("cTag2"); 
		$('#no_akaunwaris').val('');
		$("#no_akaunwaris").prop('minLength', cTag1);
		$("#no_akaunwaris").prop('maxLength', cTag2);
	});
	$('#id_bankwaris').trigger('change');

	$('.myDatePicker').datepicker({
		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>'
		}
	});

	$('.select2').select2({ width: '100%' }); 	
	
	$('#simpaninfoBtn').click(function() {
		$('#waris-tab').click();
	});	
	
	$('#indicator_simpanBtn').click(function() {
		var indicator_list = [[]];
		indicator_list.push(['id_rumahorgtua', 'Rumah Orang Tua', 'dd']);	
		indicator_list.push(['ulasan_rumahorgtua', 'Ulasan', 'tb']);
		indicator_list.push(['id_bankwaris', 'Bank', 'dd']);
		indicator_list.push(['no_akaunwaris', 'No. Akaun', 'tb']);
		
		if (checkInput(indicator_list))
		{
			$(this).prop("disabled",true);
			var param = $('#indicator_form').serialize();
			$.ajax({
				type: "POST", 
				url: "general-orangtua_insert.php", 
				data: param+ '&id=' + "<?=$myID?>" + '&id_program=' + "<?=$id_program?>",

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