<?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);
$from = fetch_postget_val('from',$_POST, $_GET);
$readonly = fetch_postget_val('readonly',$_POST, $_GET);

$indicator_Query = "SELECT A.*,B.desk_program, D.*, E.desk_khairatkat, K.nama AS nama_keyinmati, L.desk_daerah AS daerah_keyinmati
				  FROM m_pemohon A
				  LEFT JOIN i_program B ON B.id_program = A.id_program
				  LEFT JOIN m_waris D ON D.id_pemohon = A.id_pemohon
				  LEFT JOIN i_khairatkat E ON E.id_khairatkat = A.id_khairatkat
				  LEFT JOIN t_pengguna K ON K.id_pengguna = A.id_keyinmati
				  LEFT JOIN i_daerah L ON L.id_daerah = K.id_daerah
				  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)) {
	$dokList[]= $res;
	if($res['flag_wajib'] == 1)
	{
		$dokWajibList[]= $res['id_dokumen'];
	}
}
?>
<div class="modal-header">
	<h5 class="modal-title"><b>PENGESAHAN PERMOHONAN KHAIRAT</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>
	<!--begin::Row-->
	<div class="accordion accordion-solid accordion-panel accordion-svg-toggle" id="accor2">
		<div class="card">
			<div class="card-header" id="head1">
				<div class="card-title" data-toggle="collapse" data-target="#col2">
					<div class="card-label">													
						<span class="svg-icon svg-icon-primary menu-icon">
							<?=$Tapisanhome_svg?>
						</span>Permohonan Khairat Kematian</div>
					<span class="svg-icon">
						<?=$Tapisan_svg?>
					</span>
				</div>
			</div>
			<div id="col2" class="collapse show" data-parent="#accor2">					
				<div class="row">
					<div class="col-md-12">
						<div class="card-body">
							<table>
								<tbody>
									<?php
										createtableLabels('Pendaftar', '10', $info_List['nama_keyinmati'].' ('.$info_List['daerah_keyinmati'].')');
										createtableLabels('Kategori Khairat', '10', $info_List['desk_khairatkat']);
										createtableLabels('Tarikh Kematian', '10', formatDate($info_List['tkh_mati'],'d M Y'));
										createtableLabels('Tarikh Daftar Kematian', '10', formatDate($info_List['tkh_keyinmati'],'d M Y'));
										createtableLabels('No Sijil Kematian', '10', $info_List['no_sijilmati']);
										createtableLabels('Ulasan', '10', $info_List['ulasan_mati']);
										
										$pathkhairat = 'upload_khairat/'.$myID.'/*';
										$imageskhairat = glob($pathkhairat);
										if(count($imageskhairat) > 0)
										{
											echo '<table>';
											echo '<br>Dokumen Khairat';
													for($numk = 0; $numk < count($imageskhairat); $numk++)
													{
														echo '<tr>';
															echo '<td>'.($numk + 1).'.<td>';
															echo '<td style="width:5%"></td>';
															echo '<td><a href="'.$imageskhairat[$numk].'" target="_blank">'.end(explode('/', $imageskhairat[$numk])).'</a></td>';
														echo '</tr>';
													}
											echo '</table>';
										}
									?>
								</tbody>
							</table>								
						</div>
					</div>
				</div>
			</div>
		</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">
					<!--begin::Card-->
					<div class="card card-custom gutter-b example-hover">
						<div class="card-body">
						<?php
							createDropDown($PDO, '<b>Perakuan Pengesahan Khairat</b>', 'i_perakuan', 'id_perakuan', '', 'Pilih Perakuan', 4, 3, 12, $readonly);
							createTextArea('<b>Ulasan</b>', 'ulasan_sahmati', 'Ulasan', '', $readonly, 3, 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()
{
	$('#ulasan_sahmati_div').hide();
}

function showDiv()
{	
	
	if($('#id_perakuan').val() == 2)
	{
		$('#ulasan_sahmati_div').show();
	}
}

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

	$('.select2').select2({ width: '100%' }); 
	
	$('#tind_simpanBtn').click(function() {
		
		var indicator_list = [[]];
		indicator_list.push(['id_perakuan', 'Pengesahan', 'dd']);
		if($('#id_perakuan').val() == 2)
		{
			indicator_list.push(['ulasan_sahmati', 'Ulasan', 'tb']);
		}
		
		if (checkInput(indicator_list))
		{
			$(this).prop("disabled",true);
			var from = "<?=$from?>";
			var param = $('#tind_form').serialize();
			
			$.ajax({
				type: "POST", 
				url: "general-khairat_sah_insert.php", 
				data: param + '&id=' + "<?=$myID?>",

				success: function(res){
					if(res > 0)
					{
						swal.fire({
							text: "Berjaya membuat tindakan pengesahan",
							icon: "success",
							buttonsStyling: false,
							confirmButtonText: "OK!",
							customClass: {
								confirmButton: "btn font-weight-bold btn-light-primary"
							}
						}).then(function() {
							if(from == 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>