<?php 
include_once ("includes/setup.inc.php");

$script_base = 'pht_pemilik';
$myID = fetch_postget_val('id',$_POST, $_GET);
$readonly = fetch_postget_val('readonly',$_POST, $_GET);
$temp = ($my_details->usr_id).'_'.time();

$indicator_Query = "SELECT A.*
				  FROM m_pemilik A
				  WHERE A.id_pemilik = ?";
$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);

?>
<div class="modal-header">
	<h5 class="modal-title"><?php echo ($myID > 0) ? 'Kemaskini Maklumat Pemilik':'Daftar Pemilik Baru'; ?></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" id="indicator_form">
		<div class="form-body">
			<div class="row">
				<?php
					createTextBox('Nama Pemilik', 'nama_pemilik', 'Nama Pemilik', $indicator_List['nama_pemilik'], 3, $readonly, 6, 10);
					createTextBox('No. KP Baru', 'kpbaru_pemilik', 'KP Baru', $indicator_List['kpbaru_pemilik'], 3, (($myID > 0) ? 1 : 0), 6, 10);
					createDropDown($PDO, 'Bangsa', 'i_bangsa', 'bangsa_pemilik', $indicator_List['id_bangsa'], 'Pilih Bangsa Pemilik', 6, 3, 10, $readonly);
					createTextBox('No. Telefon', 'tel_pemilik', 'No. Telefon', $indicator_List['tel_pemilik'], 3, $readonly, 6, 10);
					createTextBox('Alamat Rumah / Pejabat', 'alamat_pemilik', 'Alamat Rumah / Pejabat', $indicator_List['alamat_pemilik'], 3, $readonly, 9, 10);
				?>
			</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="indicator_simpanBtn">Simpan</button>';
		}
	?>
</div>

<script>
$(document).ready(function(){  

	$('.select2').select2({ width: '100%' }); 
	
	$('.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;
	});
	$('#indicator_simpanBtn').click(function() {
		var indicator_list = [[]];
		indicator_list.push(['nama_pemilik', 'Nama Pemilik', 'tb']);
		indicator_list.push(['kpbaru_pemilik', 'No. KP Baru Pemilik', 'tb']);
		
		if (checkInput(indicator_list))
		{
			$(this).prop("disabled",true);
			var param = $('#indicator_form').serialize();
			$.ajax({
				type: "POST", 
				url: "<?=$script_base?>-tambah_insert.php", 
				data: param+ '&id=' + "<?=$myID?>",

				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.parent.reloadTable();
							$("#closeBtn").click();
						});
					}
					else
					{
						$(this).prop("disabled",false);		
						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>