<?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">Kemaskini Status Aktif<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="aktif-tab" data-toggle="tab" href="#aktif">
								<span class="nav-text">Maklumat Status Aktif</span>
							</a>
						</li>
					</ul>
					<div class="tab-content" id="myTabContent">
						<div class="tab-pane fade show active" id="aktif" role="tabpanel" aria-labelledby="aktif-tab">
							<div class="row">
								<div class="col-md-12" style="margin-top: 20px;">
									<?php
										
										$mySQL = "SELECT id_aktifmohon AS id, desk_aktifmohon as name 
												FROM i_aktifmohon
												WHERE aktif = 1
												ORDER BY susunan";
										
										createDropDown_sql($PDO, 'Status Aktif', $mySQL, 'id_aktifmohon', $pemohon_List['id_aktifmohon'], 'Status Aktif', 4, 3, 12, $readonly);
										createTextArea('Ulasan', 'ulasan_aktifmohon', 'Sila nyatakan ulasan', $pemohon_List['ulasan_aktifmohon'], 0, 3, 7, 12, '', 10);								
									?>
								
								</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 Status Aktif</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(){  

	$('.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%' }); 	
	
	$('#indicator_simpanBtn').click(function() {
		var indicator_list = [[]];
		indicator_list.push(['id_aktifmohon', 'Status Aktif', 'dd']);		
		indicator_list.push(['ulasan_aktifmohon', 'Ulasan', 'tb']);
		
		if (checkInput(indicator_list))
		{
			$(this).prop("disabled",true);
			var param = $('#indicator_form').serialize();
			$.ajax({
				type: "POST", 
				url: "general-aktif_insert.php", 
				data: param+ '&id=' + "<?=$myID?>" + '&id_program=' + "<?=$id_program?>",

				success: function(res){
					if(res > 0)
					{
						swal.fire({
							text: "Berjaya Tambah/Kemaskini Maklumat Status Aktif",
							icon: "success",
							buttonsStyling: false,
							confirmButtonText: "OK!",
							customClass: {
								confirmButton: "btn font-weight-bold btn-light-primary"
							}
						}).then(function() {
							window.parent.reloadTable();
							$("#closeBtn").click();
						});
					}
					else if (res == -888)
					{
						swal.fire({
							text: "Tiada perubahan status aktif pemohon",
							icon: "error",
							buttonsStyling: false,
							confirmButtonText: "OK!",
							customClass: {
								confirmButton: "btn font-weight-bold btn-light-primary"
							}
						}).then(function() {
							$(this).prop("disabled",false);									
						});							
					}
					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>