<?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);
$program = fetch_postget_val('program',$_POST, $_GET);
$uniqueId = time(); //to prevent duplicated form name

?>
<div class="modal-header">
	<h5 class="modal-title">Dokumen Lampiran</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="indicator_form_<?=$uniqueId?>" id="indicator_form_<?=$uniqueId?>">
		<div class="form-body">
			<div class="row">
				<div class="col-md-12">
					<?php
						//hardcode dropzone here
						//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 = $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;
							}
						}
						echo '<h3>Senarai Dokumen Yang Diperlukan</h3>';
						//echo '<div class="checkbox-list">';
						$doc_counter = 1;
							foreach($dokWajibList as $wkey => $witem) 
							{
								echo $doc_counter++.'. '.$witem['desk_dokumen'].'<a title="Dokumen Wajib">'.$Wajib_svg.'</a><br>';
								$checkDocList .= $witem['id_dokumen'].',';
							}
							foreach($dokList as $wkey => $witem) 
							{
								echo $doc_counter++.'. '.$witem['desk_dokumen'].'<br>';
							}
							echo '<hr>';
						//echo '</div>';
						echo '<div class="form-group row">
							<div class="col-md-12">
								<div class="dropzone dropzone-default" id="anakemas_DZ">
									<div class="dropzone-msg dz-message needsclick">
										<h3 class="dropzone-msg-title">Klik di sini untuk muat naik dokumen</h3>
										<span class="dropzone-msg-desc">Hanya gambar / pdf sahaja</span>
									</div>
								</div>
							</div>
						</div>';
						createModal('AE_large_photo-modal');
					?>
				</div>
			</div>
		</div>
	</form>
</div>
<div class="modal-footer">
	<?php
		echo '<button type="button" class="btn btn-secondary" data-dismiss="modal">Tutup</button>';
	?>
</div>

<?php
$jsItem['input_name'] = 'anakemas_DZ';
echo '<script type="text/javascript">';
	echo 'var uploadPath = "upload_anakemas";';
	
		echo 'var myDZ_'.$jsItem['input_name'].' = new Dropzone("div#'.$jsItem['input_name'].'", {
			acceptedFiles: "image/jpg,image/jpeg,image/png,image/bmp,application/pdf",
			//maxFiles: 1,
			maxFilesize: 2,//max file size in MB,
			url: "general-photo_upload.php",
			paramName: "uploadfile",
			dictDefaultMessage : \'<span style="font-style:italic"><h1>Drop files to upload</h1><h3>(or click)</h3></span>\',
			init: function() {
				$.get("general-photo_upload.php", {peranan:"'.$my_details->user_level.'", uploadId:"'.$myID.'", generalName:"", uploadFolder:uploadPath}, function(data) {
					$.each(data, function(key,value){
						var mockFile = { name: value.name, size: value.size };				
						myDZ_'.$jsItem['input_name'].'.emit("addedfile", mockFile);
						if(value.name.trim().endsWith(".pdf"))
						{
							myDZ_'.$jsItem['input_name'].'.emit("thumbnail", mockFile, uploadPath + "/pdf.jpg");
						}
						else
						{
							myDZ_'.$jsItem['input_name'].'.emit("thumbnail", mockFile, uploadPath + "/" + '.$myID.'+ "/" + value.name);
						}
						myDZ_'.$jsItem['input_name'].'.emit("complete", mockFile);
						myDZ_'.$jsItem['input_name'].'.files.push(mockFile);
					});
				});
			}
		});';

		echo '
		myDZ_'.$jsItem['input_name'].'.on("error", function(file, errorMessage) {
			alert("ERROR " + errorMessage);
			myDZ_'.$jsItem['input_name'].'.removeFile(file);
		});

		myDZ_'.$jsItem['input_name'].'.on("success", function(file, photoPath) {
			file.previewElement.querySelector("[data-dz-name]").innerHTML = photoPath;
			console.log(photoPath);
			console.log(photoPath.trim().endsWith(".pdf"));
			if(photoPath.trim().endsWith(".pdf"))
			{
				myDZ_'.$jsItem['input_name'].'.emit("thumbnail", file, uploadPath + "/pdf.jpg");
			}
			else
			{
				myDZ_'.$jsItem['input_name'].'.emit("thumbnail", file, uploadPath + "/" + '.$myID.' + "/" + photoPath);
			}
		});

		myDZ_'.$jsItem['input_name'].'.on("sending", function(file, xhr, formData){
			formData.append("uploadId", '.$myID.');
			formData.append("program", '.$program.');
			//formData.append("generalName", "banner.jpg");
			formData.append("uploadFolder", uploadPath);
			//myDropzone.removeAllFiles(); //remove all 1st
		});
			
		myDZ_'.$jsItem['input_name'].'.on("dictRemoveFileConfirmation", function(file, xhr, formData){
			formData.append("uploadId", '.$myID.');
		});

		myDZ_'.$jsItem['input_name'].'.on("addedfile", function(file) {
			var deleted = false;
			if(deleted == false)
			{
				file.previewElement.addEventListener("click", function() {
					var filesrc = uploadPath + "/" + '.$myID.' + "/" + file.previewElement.querySelector(\'[data-dz-name]\').innerHTML.trim();
					if(filesrc.endsWith(".pdf"))
					{
						window.open(filesrc);
					}
					else
					{
						loadMyModal(\'general-photo_view.php?src="\' + filesrc + \'"\', \'#AE_large_photo-modal\');
					}
				});
				
				// Create the remove button
				var removeButton = Dropzone.createElement(\'<a class="btn btn-xs red"><i class="fa fa-trash-o"></i>Remove Image</a>\');

				// Listen to the click event
				removeButton.addEventListener("click", function(e) {
					e.preventDefault();
					e.stopPropagation();

					var deleteFileName = file.previewElement.querySelector(\'[data-dz-name]\').innerHTML;
					x = confirm("Are you sure to delete " + deleteFileName + "?");
					if(!x)  return false;
					var name = deleteFileName;                
					$.ajax({
						type: "POST",
						url: "general-photo_delete.php",
						data: "uploadId=" + '.$myID.' + "&filename="+name+ "&uploadFolder=" + uploadPath,
						dataType: "html"
					});
					myDZ_'.$jsItem['input_name'].'.removeFile(file);
				});

				file.previewElement.appendChild(removeButton);
			}
		});';
echo '</script>'; 

?>
<script>
$(document).ready(function(){  
$('.select2').select2({ width: '100%' }); 

$('.myDatePicker').datepicker({
	autoclose: true,
	clearBtn: true,
	format: "dd-mm-yyyy",
	todayHighlight: true,
	orientation: "bottom left",
	templates: {
		leftArrow: '<i class="la la-angle-left"></i>',
		rightArrow: '<i class="la la-angle-right"></i>'
	}
});

$('#borang_kpbaru_anakemas').change(function(){
	var info = processKP($('#borang_kpbaru_anakemas').val());
	$('#borang_tkh_lahiranakemas').datepicker( "setDate", info[0]);
	$('#borang_jantina_anakemas').val(info[1]);
	$('#borang_jantina_anakemas').trigger('change');
	$('#borang_tkh_lahiranakemas').prop('disabled', true);
	$('#borang_jantina_anakemas').prop('disabled', true);
});
$('#borang_tkh_lahiranakemas').prop('disabled', true);
$('#borang_jantina_anakemas').prop('disabled', true);

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

});		
</script>