<?php 
require('includes/header_pemohon.php');
$myID = $my_details->usr_id;
$dateTimeNow = dateTimeNow();

$indicator_Query = "SELECT A.*
				  FROM m_borang A
				  WHERE A.id_pemohon = ?";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->bindValue(1, $myID, PDO::PARAM_INT);
$PDO_stmt->execute();
$indicator_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

?>	

<style>
.tidaklayak {
    color:red;
	background-color:#f8f9fa;
}

</style>
<!--begin::Content-->
<div class="content d-flex flex-column flex-column-fluid" id="kt_content">
	<!--begin::Entry-->
	<div class="d-flex flex-column-fluid">
		<!--begin::Container-->
		<div class="container">
			<!--begin::Dashboard--> 
			<!--begin::Row-->
			<div class="card card-custom card-stretch gutter-b">
				<!--begin::Header-->
				<div class="card-header">
					<h3 class="card-title">Borang Maklumat Penternak</h3>
				</div>
				
				<div class="card-body">
				<input type="hidden" id="dz_myID" value="<?=$indicator_List['id_borang']?>">
				<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 Penternak/Pemilik</span>
										</a>
									</li>
									<li class="nav-item">
										<a class="nav-link" id="kapasiti-tab" data-toggle="tab" href="#kapasiti">
											<span class="nav-text">Kapasiti Ladang</span>
										</a>
									</li>
									<li class="nav-item">
										<a class="nav-link" id="document-tab" data-toggle="tab" href="#document">
											<span class="nav-text">Dokumen Lampiran</span>
										</a>
									</li>
									<li class="nav-item">
										<a class="nav-link" id="akuan-tab" data-toggle="tab" href="#akuan">
											<span class="nav-text">Akuan Permohonan</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
													createTextBox('Nama Penternak', 'nama_pemohon', 'Nama Penternak', $indicator_List['nama_pemohon'], 3, 1, 7, 12, '', 'text');
													createTextBox('No KP Baru', 'kpbaru_pemohon', 'No KP Baru', $indicator_List['kpbaru_pemohon'], 3, 1, 7, 12, '', 'text');
													createDropDown($PDO, 'Status Perkahwinan', 'i_kahwin', 'id_kahwin', $indicator_List['id_kahwin'], 'Pilih Status Perkahwinan', 7, 3, 12, $readonly);
													createDropDown($PDO, 'Jantina', 'i_jantina', 'id_jantina', $indicator_List['id_jantina'], 'Pilih Jantina', 7, 3, 12, $readonly);
													createDropDown($PDO, 'Bangsa', 'i_bangsa', 'id_bangsa', $indicator_List['id_bangsa'], 'Pilih Bangsa', 7, 3, 12, $readonly);
													createDropDown($PDO, 'Tahap Pendidikan', 'i_tarafpendidikan', 'id_tarafpendidikan', $indicator_List['id_tarafpendidikan'], 'Pilih Tahap Pendidikan', 7, 3, 12, $readonly);
													createDropDown($PDO, 'DUN', 'i_dun', 'id_dun', $indicator_List['id_dun'], 'Pilih DUN', 7, 3, 12, $readonly);
													createDropDown($PDO, 'Parlimen', 'i_parlimen', 'id_parlimen', $indicator_List['id_parlimen'], 'Pilih Parlimen', 7, 3, 12, $readonly);
													createDropDown($PDO, 'Daerah', 'i_daerah', 'id_daerah', $indicator_List['id_daerah'], 'Pilih Daerah', 7, 3, 12, $readonly);
													createDropDown($PDO, 'Kategori Pemilik', 'i_katmilik', 'id_katmilik', $indicator_List['id_katmilik'], 'Pilih Kategori Pemilik', 7, 3, 12, $readonly);
													createTextBox('Keluasan Kandang', 'luas_kandang', 'Keluasan Kandang (Hektar)', $indicator_List['luas_kandang'], 3, $readonly, 7, 12, '', 'text');
													createTextBox('Keluasan Fodder', 'luas_fodder', 'Keluasan Fodder (Hektar)', $indicator_List['luas_fodder'], 3, $readonly, 7, 12, '', 'text');
													createTextBox('Keluasan Ragutan', 'luas_ragutan', 'Keluasan Ragutan (Hektar)', $indicator_List['luas_ragutan'], 3, $readonly, 7, 12, '', 'text');
													createDropDown($PDO, 'Hak Milik Tanah', 'i_hakmilik', 'id_hakmilik', $indicator_List['id_hakmilik'], 'Pilih Hak Milik Tanah', 7, 3, 12, $readonly);
													createDropDown($PDO, 'Projek Kerajaan', 'i_projekerajaan', 'id_projekerajaan', $indicator_List['id_projekerajaan'], 'Pilih Projek Kerajaan', 7, 3, 12, $readonly);
													createDropDown($PDO, 'Status Kandang', 'i_statuskandang', 'id_statuskandang', $indicator_List['id_statuskandang'], 'Pilih Status Kandang', 7, 3, 12, $readonly);
													createTextBox('No Lesen/Kod Ladang/ROC', 'no_lesen', 'No Lesen/Kod Ladang/ROC', $indicator_List['no_lesen'], 3, $readonly, 7, 12, '', 'text');
													createTextBox('No. Salt/VHM', 'no_salt', 'No. Salt/VHM', $indicator_List['no_salt'], 3, $readonly, 7, 12, '', 'text');
													createTextBox('Syarat Guna Tanah', 'syarat_tanah', 'Syarat Guna Tanah', $indicator_List['syarat_tanah'], 3, $readonly, 7, 12, '', 'text');
												?>
											</div>
											<?php
												if($readonly == 0)
												{
													echo '<div style="text-align:center;margin-top: 10px;">';
														echo '<button type="button" class="btn btn-primary" id="simpanBtn">Simpan & Seterusnya</button>';
													echo '</div>';
												}
											?>
										</div>
									</div>
									<div class="tab-pane fade show" id="kapasiti" role="tabpanel" aria-labelledby="kapasiti-tab">
										<div class="row">
											<div class="col-md-6" style="margin-top: 20px;">
											<?php											
												createTextBox('LEMBU PEDAGING', 'le_daging', 'Ekor', $indicator_List['le_daging'], 3, $readonly, 7, 12, '', 'numonly');
												createTextBox('LEMBU FIDLOT', 'le_fidlot', 'Ekor', $indicator_List['le_fidlot'], 3, $readonly, 7, 12, '', 'numonly');
												createTextBox('LEMBU TENUSU', 'le_tenusu', 'Ekor', $indicator_List['le_tenusu'], 3, $readonly, 7, 12, '', 'numonly');
												createTextBox('KERBAU', 'kerbau', 'Ekor', $indicator_List['kerbau'], 3, $readonly, 7, 12, '', 'numonly');
												createTextBox('KAMBING', 'kambing', 'Ekor', $indicator_List['kambing'], 3, $readonly, 7, 12, '', 'numonly');
												createTextBox('KAMBING TENUSU', 'kambing_tenusu', 'Ekor', $indicator_List['kambing_tenusu'], 3, $readonly, 7, 12, '', 'numonly');
												createTextBox('BEBIRI', 'bebiri', 'Ekor', $indicator_List['bebiri'], 3, $readonly, 7, 12, '', 'numonly');
												createTextBox('BABI', 'babi', 'Ekor', $indicator_List['babi'], 3, $readonly, 7, 12, '', 'numonly');
												createTextBox('AYAM PIRU', 'ayam_piru', 'Ekor', $indicator_List['ayam_piru'], 3, $readonly, 7, 12, '', 'numonly');
												createTextBox('PUYUH PEMBIAK BAKA', 'puyuh_pembiak', 'Ekor', $indicator_List['puyuh_pembiak'], 3, $readonly, 7, 12, '', 'numonly');
											?>
											</div>
											<div class="col-md-6" style="margin-top: 20px;">
											<?php											
												createTextBox('AYAM PEDAGING', 'ay_daging', 'Ekor', $indicator_List['ay_daging'], 3, $readonly, 7, 12, '', 'numonly');
												createTextBox('AYAM PENELUR', 'ay_telur', 'Ekor', $indicator_List['ay_telur'], 3, $readonly, 7, 12, '', 'numonly');
												createTextBox('AYAM KAMPUNG', 'ay_kampung', 'Ekor', $indicator_List['ay_kampung'], 3, $readonly, 7, 12, '', 'numonly');
												createTextBox('AYAM PEMBIAK BAKA DAGING', 'ay_pembiak_dg', 'Ekor', $indicator_List['ay_pembiak_dg'], 3, $readonly, 7, 12, '', 'numonly');
												createTextBox('AYAM PEMBIAK BAKA KAMPUNG', 'ay_pembiak_kg', 'Ekor', $indicator_List['ay_pembiak_kg'], 3, $readonly, 7, 12, '', 'numonly');
												createTextBox('ITIK PEDAGING', 'it_daging', 'Ekor', $indicator_List['it_daging'], 3, $readonly, 7, 12, '', 'numonly');
												createTextBox('ITIK PENELUR', 'it_telur', 'Ekor', $indicator_List['it_telur'], 3, $readonly, 7, 12, '', 'numonly');
												createTextBox('ITIK PEMBIAK BAKA', 'it_pembiak', 'Ekor', $indicator_List['it_pembiak'], 3, $readonly, 7, 12, '', 'numonly');
												createTextBox('PUYUH PEDAGING', 'pu_daging', 'Ekor', $indicator_List['pu_daging'], 3, $readonly, 7, 12, '', 'numonly');
												createTextBox('PUYUH PENELUR', 'pu_telur', 'Ekor', $indicator_List['pu_telur'], 3, $readonly, 7, 12, '', 'numonly');
											?>											
											
											</div>
											<?php							
												if($readonly == 0)
												{
													echo '<div style="text-align:center;margin-top: 10px;">';
														echo '<button type="button" class="btn btn-primary" id="simpanBtn2">Simpan & Seterusnya</button>';
													echo '</div>';
												}
											?>											
											
										</div>
									</div>
									<div class="tab-pane fade show" id="document" role="tabpanel" aria-labelledby="document-tab">
										<div class="row">
											<div class="col-md-12" style="margin-top: 20px;">
												<?php
													echo '<div class="form-group row">
														<div class="col-md-12">
															<div class="dropzone dropzone-default" id="borang_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>
															*Klik pada ruangan diatas untuk muatnaik dokumen (hanya format gambar/pdf sahaja)
														</div>
													</div>';
													createModal('AE_large_photo-modal');
													if($readonly == 0)
													{
														echo '<div style="text-align:center;margin-top: 10px;">';
															echo '<button type="button" class="btn btn-primary" id="simpandocBtn">Simpan & Seterusnya</button>';
														echo '</div>';
													}
												?>
											</div>
										</div>
									</div>
									<div class="tab-pane fade show" id="akuan" role="tabpanel" aria-labelledby="akuan-tab">
										<div class="row">								
											<div class="col-md-12" style="margin-bottom: 10px;">
											</div>
											
											<div class="col-md-12" style="margin-top: 20px;">
												<label class="checkbox">
													<input type="checkbox" id="akui_borang" onchange="checkUncheckCB_AE('akui_borang', 'ae_submitBtn')" >
													<span></span>
													&nbsp; &nbsp;Saya dengan ini mengesahkan maklumat yang diberikan adalah benar dan tepat.
												</label>
											</div>
											<div class="col-md-12" style="margin-top: 20px;">
												<div style="text-align:center;margin-top: 10px;">
													<button type="button" class="btn btn-primary" id="ae_submitBtn" disabled>Pengesahan Kemaskini Maklumat</button>
												</div>
											</div>
										</div>
									</div>
								</div>
							</div>
							
						</div>
					</div>
				</form>
			</div>
				<!--end::Row-->
				<!--end::Dashboard-->
			</div>
		</div>
		<!--end::Container-->
	</div>
	<!--end::Entry-->
</div>
<!--end::Content-->
					
<?php 
require('includes/footer.php');
?>	

<?php
$jsItem['input_name'] = 'borang_DZ';
echo '<script type="text/javascript">';
	echo 'var uploadPath = "upload_borang";';
	
		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:$(\'#dz_myID\').val(), 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 + "/" + $(\'#dz_myID\').val() + "/" + 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 + "/" + $(\'#dz_myID\').val() + "/" + photoPath);
			}
		});

		myDZ_'.$jsItem['input_name'].'.on("sending", function(file, xhr, formData){
			formData.append("uploadId", $(\'#dz_myID\').val());
			formData.append("uploadFolder", uploadPath);
			//myDropzone.removeAllFiles(); //remove all 1st
		});
			
		myDZ_'.$jsItem['input_name'].'.on("dictRemoveFileConfirmation", function(file, xhr, formData){
			formData.append("uploadId", $(\'#dz_myID\').val());
		});

		myDZ_'.$jsItem['input_name'].'.on("addedfile", function(file) {
			var deleted = false;
			if(deleted == false)
			{
				file.previewElement.addEventListener("click", function() {
					var filesrc = uploadPath + "/" + $(\'#dz_myID\').val() + "/" + 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=" + $(\'#dz_myID\').val() + "&filename="+name+ "&uploadFolder=" + uploadPath,
						dataType: "html"
					});
					myDZ_'.$jsItem['input_name'].'.removeFile(file);
					$(\'#file_count_'.$jsItem['input_name'].'\').val(Number($(\'#file_count_'.$jsItem['input_name'].'\').val()) - 1);
				});

				file.previewElement.appendChild(removeButton);
				$(\'#file_count_'.$jsItem['input_name'].'\').val(Number($(\'#file_count_'.$jsItem['input_name'].'\').val()) + 1);
			}
		});';
echo '</script>'; 

?>
<script>
function checkUncheckCB_AE(callerId, btnId)
{
	if( $("#" + callerId).is(':checked')) {
        $( "#" + btnId ).prop( "disabled", false );
    }
	else
	{
		$( "#" + btnId ).prop( "disabled", true );
	}
}

$(document).ready(function(){  

	$('.numonly').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;
	});

	$('.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%' }); 
	
	$('#simpanBtn').click(function() {
		var indicator_list = [[]];
		//indicator_list.push(['id_khairatkat', 'Kategori', 'dd']);		
		//indicator_list.push(['no_sijilmati', 'No. Sijil Mati', 'tb']);
		
		if (checkInput(indicator_list))
		{
			$(this).prop("disabled",true);
			var param = $('#indicator_form').serialize();
			$.ajax({
				type: "POST", 
				url: "general-borangternak_insert.php", 
				data: param + '&id=' + "<?=$myID?>" + '&part=1',

				success: function(res){
					if(res > 0)
					{
						swal.fire({
							text: "Berjaya Tambah/Kemaskini Maklumat Penternak",
							icon: "success",
							buttonsStyling: false,
							confirmButtonText: "OK!",
							customClass: {
								confirmButton: "btn font-weight-bold btn-light-primary"
							}
						}).then(function() {		
							$('#kapasiti-tab').click();					
							//window.location.reload();
						});
					}	
					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
		}
	});		
	
	$('#simpanBtn2').click(function() {
		var indicator_list = [[]];
		//indicator_list.push(['id_khairatkat', 'Kategori', 'dd']);		
		//indicator_list.push(['no_sijilmati', 'No. Sijil Mati', 'tb']);
		
		if (checkInput(indicator_list))
		{
			$(this).prop("disabled",true);
			var param = $('#indicator_form').serialize();
			$.ajax({
				type: "POST", 
				url: "general-borangternak_insert.php", 
				data: param + '&id=' + "<?=$myID?>" + '&part=2',

				success: function(res){
					if(res > 0)
					{
						swal.fire({
							text: "Berjaya Tambah/Kemaskini Maklumat Kapasiti",
							icon: "success",
							buttonsStyling: false,
							confirmButtonText: "OK!",
							customClass: {
								confirmButton: "btn font-weight-bold btn-light-primary"
							}
						}).then(function() {		
							$('#document-tab').click();					
							//window.location.reload();
						});
					}	
					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
		}
	});		
	
	$('#simpandocBtn').click(function() {
		$('#akuan-tab').click();
	});	
	
	var mylevel = "<?=$my_details->user_level?>";
	$('#ae_submitBtn').click(function(){
		
		Swal.fire({
			text: "Adakah anda pasti maklumat telah lengkap?.",
			icon: "success",
			showCancelButton: true,
			buttonsStyling: false,
			confirmButtonText: "Ya dan Hantar!",
			cancelButtonText: "Batal",
			customClass: {
				confirmButton: "btn font-weight-bold btn-primary",
				cancelButton: "btn font-weight-bold btn-default"
			}
		}).then(function (result) {
			if (result.value) {
				swal.fire({
					text: "Pengemaskinian Berjaya",
					icon: "success",
					buttonsStyling: false,
					confirmButtonText: "OK!",
					customClass: {
						confirmButton: "btn font-weight-bold btn-light-primary"
					}
				}).then(function() {
					if(mylevel == 10)
					{
						window.location.href = "index_pemohon.php";
					}
					else
					{								
						window.location.reload;									
					}
				});
			} 
			else if (result.dismiss === 'cancel') {
				Swal.fire({
					text: "Pengemaskinian ini tidak diteruskan!.",
					icon: "error",
					buttonsStyling: false,
					confirmButtonText: "Ok!",
					customClass: {
						confirmButton: "btn font-weight-bold btn-primary",
					}
				});
			}
		});
	});	
});		
</script>