<?php 
require('includes/header.php');  

$myID = fetch_postget_val('id',$_POST, $_GET);

$ladang_List = [];
$ladang_List[] = array('ruminan', 'Ladang Ruminan');
$ladang_List[] = array('tenusu', 'Ladang Tenusu');
$ladang_List[] = array('babi', 'Ladang Babi');
$ladang_List[] = array('aneka', 'Ladang Aneka Haiwan');

$indicator_Query = "SELECT A.*, B.*, C.desk_jantina, D.desk_bangsa, F.desk_dun, G.desk_parlimen, H.desk_daerah, i_negeri.desk_negeri,
					i_status_ladang.desk_status_ladang, i_projek_kerajaan.desk_projek_kerajaan, i_katpemilik.desk_katpemilik,
					i_status_lesen.desk_status_lesen, i_sistem_pemeliharaan.desk_sistem_pemeliharaan, i_hakmilik.desk_hakmilik,
					i_kategori_tanah.desk_kategori_tanah, i_syarat_nyata_tanah.desk_syarat_nyata_tanah
					FROM m_ladang A
					LEFT JOIN m_pemilik B ON B.id_pemilik = A.id_pemilik
					LEFT JOIN i_jantina C ON C.id_jantina = B.id_jantina
					LEFT JOIN i_bangsa D ON D.id_bangsa = B.id_bangsa
					LEFT JOIN i_dun F ON F.id_dun = A.id_dun
					LEFT JOIN i_parlimen G ON G.id_parlimen = A.id_parlimen
					LEFT JOIN i_daerah H ON H.id_daerah = A.id_daerah
					LEFT JOIN i_negeri ON i_negeri.id_negeri = A.id_negeri
					LEFT JOIN i_status_ladang ON i_status_ladang.id_status_ladang = A.id_status_ladang
					LEFT JOIN i_projek_kerajaan ON i_projek_kerajaan.id_projek_kerajaan = A.id_projek_kerajaan
					LEFT JOIN i_katpemilik ON i_katpemilik.id_katpemilik = A.id_katpemilik
					LEFT JOIN i_status_lesen ON i_status_lesen.id_status_lesen = A.id_status_lesen
					LEFT JOIN i_sistem_pemeliharaan ON i_sistem_pemeliharaan.id_sistem_pemeliharaan = A.id_sistem_pemeliharaan
					LEFT JOIN i_hakmilik ON i_hakmilik.id_hakmilik = A.id_hakmilik
					LEFT JOIN i_kategori_tanah ON i_kategori_tanah.id_kategori_tanah = A.id_kategori_tanah
					LEFT JOIN i_syarat_nyata_tanah ON i_syarat_nyata_tanah.id_syarat_nyata_tanah = A.id_syarat_nyata_tanah
					WHERE A.id_ladang = ?";
$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);


if($info_List['id_ladang_ruminan'] > 0)
{
	$indicator_Query = "SELECT *
						FROM m_ladang_ruminan
						WHERE id_ladang_ruminan = ?";
	$PDO_stmt = $PDO->prepare($indicator_Query); 
	$PDO_stmt->bindValue(1, $info_List['id_ladang_ruminan'], PDO::PARAM_INT);
	$PDO_stmt->execute();
	$ruminan_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);
}

if($info_List['id_ladang_tenusu'] > 0)
{
	$indicator_Query = "SELECT *
						FROM m_ladang_tenusu
						WHERE id_ladang_tenusu = ?";
	$PDO_stmt = $PDO->prepare($indicator_Query); 
	$PDO_stmt->bindValue(1, $info_List['id_ladang_tenusu'], PDO::PARAM_INT);
	$PDO_stmt->execute();
	$tenusu_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);
}

if($info_List['id_ladang_babi'] > 0)
{
	$indicator_Query = "SELECT *
						FROM m_ladang_babi
						WHERE id_ladang_babi = ?";
	$PDO_stmt = $PDO->prepare($indicator_Query); 
	$PDO_stmt->bindValue(1, $info_List['id_ladang_babi'], PDO::PARAM_INT);
	$PDO_stmt->execute();
	$babi_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);
}

if($info_List['id_ladang_aneka'] > 0)
{
	$indicator_Query = "SELECT *
						FROM m_ladang_aneka
						WHERE id_ladang_aneka = ?";
	$PDO_stmt = $PDO->prepare($indicator_Query); 
	$PDO_stmt->bindValue(1, $info_List['id_ladang_aneka'], PDO::PARAM_INT);
	$PDO_stmt->execute();
	$aneka_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);
}
?>	

<!--begin::Content-->
<div class="content d-flex flex-column flex-column-fluid" id="kt_content">
	<!--begin::Subheader-->
	<div class="subheader py-2 py-lg-4 subheader-solid" id="kt_subheader">
		<div class="container-fluid d-flex align-items-center justify-content-between flex-wrap flex-sm-nowrap">
			<!--begin::Info-->
			<div class="d-flex align-items-center flex-wrap mr-2">
				<!--begin::Page Title-->
				<h5 class="text-dark font-weight-bold mt-2 mb-2 mr-5">Kemaskini Maklumat Penternak</h5>
				<!--end::Page Title-->
				<!--begin::Actions-->
				<div class="subheader-separator subheader-separator-ver mt-2 mb-2 mr-4 bg-gray-200"></div>
			</div>
			<!--end::Info-->
		</div>
	</div>
	<!--end::Subheader-->
	<!--begin::Entry-->
	<div class="d-flex flex-column-fluid">
		<!--begin::Container-->
		<div class="container">
			<div class="card card-custom">
				<div class="card-body p-0">
					<div class="col-xl-12 col-xxl-12">
						<form id="daftarForm">
							<?php
								require('daftar_general.php');
								
								echo '<div class="col-md-10 form-group row" id="jenis_ladang_div">';
									echo '<label class="col-md-3 col-form-label" id="jenis_ladang_label">Jenis Ladang</label>';
									echo '<div class="col-md-6">';
										echo '<div class="checkbox-list">';
											foreach($ladang_List as $key => $menu) 
											{
												echo '<label class="checkbox">';
													echo '<input type="checkbox" id="t_'.$menu[0].'" name="t_'.$menu[0].'" onclick="showTab(\''.$menu[0].'\')">';
													echo '<span></span>';
													echo $menu[1];
												echo '</label>';
											}
										echo '</div>';
									echo '</div>';
								echo '</div>';
							?>
							<hr>
							<div style="text-align:center;padding-bottom:20px">
								<button type="button" class="btn btn-primary" id="updateSimpanBtn">Simpan</button>
							</div>
							<hr>
						</form>
						<?php
							echo '<ul class="nav nav-tabs" role="tablist">';
								$allLadang = [];
								foreach($ladang_List as $key => $menu) 
								{
									if($info_List['id_ladang_'.$menu[0]] > 0)
									{
										$allLadang[] = $menu[0];
									}
									echo '<li class="nav-item" id="li_'.$menu[0].'" style="display:none">';
										echo '<a class="nav-link" data-toggle="tab" id="nav_'.$menu[0].'" href="#tab_'.$menu[0].'">';
											echo '<span class="nav-text">'.$menu[1].'</span>';
										echo '</a>';
									echo '</li>';
								}
							echo '</ul>';
							echo '<input type="hidden" id="allLadang" value="'.join(',', $allLadang).'">';
							echo '<div class="tab-content">';
								foreach($ladang_List as $key => $menu) 
								{
									require('daftar_'.$menu[0].'.php');
								}
							echo '</div>';
						?>
					</div>
				</div>
				<!--end::Wizard-->
			</div>
		</div>
		<!--end::Container-->
	</div>
	<!--end::Entry-->
</div>
<!--end::Content-->
					
<?php 
require('includes/footer.php');
?>


<script>
var autogenerate = true;
var activeLadang = [];
function showTab(ladang)
{
	if($('#t_' + ladang).is(":checked"))
	{
		$('.tab-pane').removeClass('active');
		$('#li_' + ladang).show();
		$('#tab_' + ladang).addClass( "active show" );
		$('#nav_' + ladang).click();
		activeLadang[ladang] = 1;
	}
	else
	{
		$('#li_' + ladang).hide();
		$('#tab_' + ladang).removeClass( "active show" );
		delete activeLadang[ladang];
		
		for (const ladang in activeLadang) {
		  $('#nav_' + ladang).click();
		  break;
		}
	}
}

var ajaxUrl = $('#script_base_name').val() + "-ajax.php";
function getData()
{
	return {
		sqlOnly: 0,
		daerah: $('#daerah_sc').val(),
		dun: $('#dun_sc').val(),
	};
}

function deleteFunction(id, desk)
{
	Swal.fire({
        title: "Pastikah Anda?",
        text: "Pastikah untuk selesaikan '" + desk + "' ?",
        icon: "warning",
        showCancelButton: true,
        confirmButtonText: "Ya",
		cancelButtonText: "Tidak",
    }).then(function(result) {
		if (result.value) {
			$.ajax({
			   type: "POST",
			   url: $('#script_base_name').val() + "-aktif.php",
			   data: 'id=' + id,
			   
			   success: function(res){
					swal.fire({
						text: "Berjaya selesaikan " + desk + ".",
						icon: "success",
						buttonsStyling: false,
						confirmButtonText: "OK!",
						customClass: {
							confirmButton: "btn font-weight-bold btn-light-primary"
						}
					}).then(function() {
						reloadTable();
					});
			   
			   }
		   });  //end ajax
		}
	});
}

function reloadTable()
{
	reinitTable($('#table_name').val(), this.ajaxUrl, this.getData(), 1, 0);
}

$(document).ready(function(){  

	$('#ajax-modal').on('shown.bs.modal', function () {  // this is to clear modal cache each time modal load
	 $(this).removeData('bs.modal');
	}); 
	
	var allLadang = $('#allLadang').val().split(",");
	for (var num = 0; num < allLadang.length; num++) {
		ladang = allLadang[num];
		$( "#t_" + ladang).prop( "checked", true );
		showTab(ladang);
	}
	
	
	$('#updateSimpanBtn').click(function() {
		var indicator_list = [[]];
		indicator_list.push(['nama_pemilik', 'Nama Pemilik', 'tb']);
		//indicator_list.push(['id_jenisaduan', 'Jenis Aduan', 'dd']);
		//indicator_list.push(['tajuk', 'Tajuk', 'tb']);
		//indicator_list.push(['laporan', 'Keterangan aduan', 'tb']);
		
		//if (checkInput(indicator_list))
		{
			//$(this).prop("disabled",true);
			var param = $('#daftarForm').serialize();
		  param += "&ladang=" + "<?=$info_List['id_ladang']?>";
		  param += "&pemilik=" + "<?=$info_List['id_pemilik']?>";
			console.log(activeLadang);
			for (const ladang in activeLadang) {
			  console.log(ladang);
			  param += "&active_" + ladang + "=1";
			  param += "&" + $('#daftarForm_' + ladang).serialize();
			}
			console.log(param);
			
			$.ajax({
				type: "POST", 
				url: "sen_penternak-update_insert.php", 
				data: param,

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