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

$script_base = 'in_dun';
$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 i_dun A
				  WHERE A.id_dun = ?";
$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">Daftar / Kemaskini Maklumat Kadun</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 Kadun', 'desk_dun', 'Nama Kadun', $indicator_List['desk_dun'], 3, 1, 7, 12, '', 'text');
					createTextBox('Nama Adun', 'nama_adun', 'Nama Adun', $indicator_List['nama_adun'], 3, $readonly, 7, 12, '', 'text');
					createTextBox('No. Telefon Adun', 'no_teladun', 'No. Telefon Adun', $indicator_List['no_teladun'], 3, $readonly, 7, 12, '', 'text');
					createTextBox('No. Telefon HP Adun', 'no_telhpadun', 'No. Telefon HP Adun', $indicator_List['no_telhpadun'], 3, $readonly, 7, 12, '', 'text');
					createTextBox('Emel', 'emel', 'Emel', $indicator_List['emel'], 3, $readonly, 7, 12, '', 'text');
					createTextArea('Alamat Pusat Khidmat', 'alamat_kadun', 'Alamat Pusat Khidmat', $indicator_List['alamat_kadun'], $readonly, 3, 7, 12, 'text', 5);
					createTextArea('Alamat Bank', 'alamat_bank', 'Alamat Bank', $indicator_List['alamat_bank'], $readonly, 3, 7, 12, 'text', 5);
				?>
			</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%' }); 
	
	$('#indicator_simpanBtn').click(function() {
		var indicator_list = [[]];
		indicator_list.push(['nama_adun', 'Nama Adun', 'tb']);
		indicator_list.push(['no_teladun', 'No. Telefon Adun', '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
					{
						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>