<?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);
$id_program = fetch_postget_val('program',$_POST, $_GET);
$pass = fetch_postget_val('pass',$_POST, $_GET);
$readonly = fetch_postget_val('readonly',$_POST, $_GET);
$name_id_program = $id_program;
if ($id_program < 10 && (!(substr($id_program, 0, 1) === '0')))
{
	$name_id_program = '0'.$id_program;
}

$indicator_Query = "SELECT A.*,B.desk_program
				  FROM m_pemohon A
				  LEFT JOIN i_program B ON B.id_program = A.id_program
				  WHERE A.id_pemohon = ?";
$PDO_stmt = $PDO->prepare($indicator_Query); 
$PDO_stmt->bindValue(1, $myID, PDO::PARAM_INT);
$PDO_stmt->execute();
$info_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

//get all i_dokumenprogram
$dok_Query = "SELECT i_dokumen.id_dokumen, i_dokumenprogram.flag_wajib, i_dokumen.desk_dokumen
				FROM i_dokumenprogram 
				LEFT JOIN i_dokumen ON i_dokumenprogram.id_dokumen = i_dokumen.id_dokumen
				WHERE id_program = $id_program";
$dok_Result = $PDO->prepare($dok_Query);
$dok_Result->execute();
// put all into hash for easier comparison
$dokList = array();
$dokWajibList = array();
while($res = $dok_Result->fetch(PDO::FETCH_ASSOC)) {
	if($res['flag_wajib'] == 1)
	{
		$dokWajibList[]= $res;
	}
	else
	{
		$dokList[]= $res;
	}
}
?>
<div class="modal-header">
	<h5 class="modal-title"><b>PENERIMAAN DOKUMEN</b><br>Pemohon : <b><?=$info_List['nama_pemohon']?></b><br>Program : <b><?=$info_List['desk_program']?></b></h5>
	<button type="button" class="close" data-dismiss="modal" aria-label="Close" id="closeBtn" name="closeBtn" ></button>
</div>
<div class="modal-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 require('general-papar.php');?>
				</div>
			</div>
			<!--end::Card-->										
		</div>
	</div>
	<form action="javascript:;" class="form-horizontal" name="tind_form" id="tind_form">
		<div class="form-body">
			<div class="row">
				<div class="col-md-12">
					<?php
					if($id_program != 4)
					{
					?>					
						<div class="card card-custom bg-danger gutter-b example-hover">
							<div class="card-body">
								<span class="svg-icon svg-icon-warning mr-5">
									<span class="svg-icon svg-icon-lg"> 
									<!--begin::Svg Icon | path:assets/media/svg/icons/Home/Library.svg--> 
									<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" />
											<path d="M5,3 L6,3 C6.55228475,3 7,3.44771525 7,4 L7,20 C7,20.5522847 6.55228475,21 6,21 L5,21 C4.44771525,21 4,20.5522847 4,20 L4,4 C4,3.44771525 4.44771525,3 5,3 Z M10,3 L11,3 C11.5522847,3 12,3.44771525 12,4 L12,20 C12,20.5522847 11.5522847,21 11,21 L10,21 C9.44771525,21 9,20.5522847 9,20 L9,4 C9,3.44771525 9.44771525,3 10,3 Z" fill="#000000" />
											<rect fill="#000000" opacity="0.3" transform="translate(17.825568, 11.945519) rotate(-19.000000) translate(-17.825568, -11.945519)" x="16.3255682" y="2.94551858" width="3" height="18" rx="1" />
										</g>
									</svg>
									<!--end::Svg Icon-->
									</span>
								</span>
								<a href="https://pengundi.spr.gov.my/" target="_blank" class="font-weight-bold text-inverse-info text-hover-primary font-size-lg mb-1">Sila membuat semakan daftar pemilih</a>
							</div>
						</div>
					<?php
					}
					?>
					
					<!--begin::Card-->
					<div class="card card-custom gutter-b example-hover">
						<div class="card-body"><b>Senarai Semak Lampiran Dokumen</b>
						<?php
							echo '<div class="checkbox-list"><br>';
							
								$checkDocList = '';
								foreach($dokWajibList as $key => $item) 
								{
									echo '<label class="checkbox '.($readonly?'checkbox-disabled':'').'">';
										echo '<input class="wajibClass" type="checkbox" '.($readonly?'disabled="disabled"':'').' id="doc_'.$item['id_dokumen'].'" name="doc_'.$item['id_dokumen'].'">';
										echo '<span></span>';
										echo $Wajib_svg.''.$item['desk_dokumen'];
									echo '</label>';
									$checkDocList .= $item['id_dokumen'].',';
								}
								echo '<input type="hidden" id="checkDocList" name="checkDocList" value="'.$checkDocList.'">';
								foreach($dokList as $key => $item) 
								{
									echo '<label class="checkbox '.($readonly?'checkbox-disabled':'').'">';
										echo '<input class="normalClass" type="checkbox" '.($readonly?'disabled="disabled"':'').' id="doc_'.$item['id_dokumen'].'" name="doc_'.$item['id_dokumen'].'">';
										echo '<span></span>';
										echo $item['desk_dokumen'];
									echo '</label>';
								}
								
							echo '</div>';
						?>
						</div>
					</div>
					<!--end::Card-->
					
					<!--begin::Card-->
					<div class="card card-custom gutter-b example-hover">
						<div class="card-body">
						<?php
							createDropDown($PDO, '<b>Perakuan Terima</b>', 'i_perakuan', 'id_perakuan', '', 'Pilih Perakuan', 4, 2, 12, $readonly);			
							createDateTB('<b>Tarikh Terima</b>', 'tkh_terima', 'Tarikh Terima',(formatDate($indicator_List['tkh_terima'], 'd M Y') == ''?date('d M Y'):formatDate($indicator_List['tkh_terima'], 'd M Y')), 2, 7, 0, 12);	
							createTextArea('<b>Ulasan</b>', 'ulasan_terima', 'Ulasan', '', $readonly, 2, 7, 12, 'text', 5);
						?>
						</div>
					</div>
					<!--end::Card-->
				</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="tind_simpanBtn">Simpan</button>';
		}
	?>
</div>


<script>

function hideAll()
{
	$('#tkh_terima_div').hide();
	$('#ulasan_terima_div').hide();
}

function showDiv()
{		
	if($('#id_perakuan').val() == 1)
	{
		$('#tkh_terima_div').show();
	}	
	else if($('#id_perakuan').val() == 2)
	{
		$('#ulasan_terima_div').show();
	}
}

$(document).ready(function(){  
	
	hideAll();
	showDiv();
	
	$('#id_perakuan').change(function(){
		hideAll();
		showDiv();
	});

	$('.myDatePicker').datepicker({
		//startDate: '-14d',
		endDate: '+0d',
		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%' }); 
	
	$('#tind_simpanBtn').click(function() {
		
		var pass = "<?=$pass?>";
		var allCheck = $('#tind_form input:checkbox').serialize();
		allCheck = allCheck.replace(/\&/g, "");
		allCheck = allCheck.replace(/doc\_/g, "");
		allCheck = allCheck.replace(/=on/g, ",");
		console.log(allCheck);
		
		var allWajibDok = $('#tind_form input:checkbox[class=wajibClass]').serialize();
		console.log(allWajibDok);
		allWajibDok = allWajibDok.replace(/\&/g, "");
		allWajibDok = allWajibDok.replace(/doc\_/g, "");
		allWajibDok = allWajibDok.replace(/=on/g, ",");
		console.log(allWajibDok);
		console.log($('#checkDocList').val());
		
		if($('#id_perakuan').val() == 1)
		{
			if(allWajibDok != $('#checkDocList').val())
			{
				swal.fire({
					text: "Sila tandakan semua dokumen yang wajib.",
					icon: "error",
					buttonsStyling: false,
					confirmButtonText: "OK!",
					customClass: {
						confirmButton: "btn font-weight-bold btn-light-primary"
					}
				}).then(function() {							
				});
			}
			else if(allCheck == "")
			{
				swal.fire({
					text: "Sila tandakan dokumen yang diterima.",
					icon: "error",
					buttonsStyling: false,
					confirmButtonText: "OK!",
					customClass: {
						confirmButton: "btn font-weight-bold btn-light-primary"
					}
				}).then(function() {							
				});
			}
			else
			{	
				$(this).prop("disabled",true);
				var param = $('#tind_form').serialize();
				
				$.ajax({
					type: "POST", 
					url: "general-terima_tind_insert.php", 
					data: param + '&dok=' + allCheck + '&id=' + "<?=$myID?>",

					success: function(res){
						if(res > 0)
						{
							swal.fire({
								text: "Berjaya Kemaskini Maklumat Penerimaan Dokumen",
								icon: "success",
								buttonsStyling: false,
								confirmButtonText: "OK!",
								customClass: {
									confirmButton: "btn font-weight-bold btn-light-primary"
								}
							}).then(function() {
								if(pass == 1)
								{
									window.location.reload(); 								
								}
								else
								{
									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	
			}	
		}
		else
		{	
			var indicator_list = [[]];
			indicator_list.push(['id_perakuan', 'Perakuan', 'dd']);
			if($('#id_perakuan').val() == 2)
			{
				indicator_list.push(['ulasan_terima', 'Ulasan', 'tb']);
			}
			
			if (checkInput(indicator_list))
			{
				$(this).prop("disabled",true);
				var param = $('#tind_form').serialize();
				
				$.ajax({
					type: "POST", 
					url: "general-terima_tind_insert.php", 
					data: param + '&id=' + "<?=$myID?>",

					success: function(res){
						if(res > 0)
						{
							swal.fire({
								text: "Berjaya Kemaskini Maklumat Penerimaan Dokumen",
								icon: "success",
								buttonsStyling: false,
								confirmButtonText: "OK!",
								customClass: {
									confirmButton: "btn font-weight-bold btn-light-primary"
								}
							}).then(function() {
								if(pass == 1)
								{
									window.location.reload(); 								
								}
								else
								{
									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>