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

$script_base = 'in_bank';
$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_bank A
				  WHERE A.id_bank = ?";
$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 Bank</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 Bank', 'desk_bank', 'Nama Bank', $indicator_List['desk_bank'], 3, $readonly, 7, 12, '', 'text');
					createTextBox('Kod Bank', 'kod_bank', 'Kod Bank', $indicator_List['kod_bank'], 3, $readonly, 7, 12, '', 'text');
					createTextBox('Bil Digit Minimum Akaun', 'digit_bank_min', 'Bil Digit Minimum Akaun', $indicator_List['digit_bank_min'], 3, $readonly, 7, 12, '', 'numonly');
					createTextBox('Bil Digit Maksimum Akaun', 'digit_bank', 'Bil Digit Maksimum Akaun', $indicator_List['digit_bank'], 3, $readonly, 7, 12, '', 'numonly');
					createTextBox('Kod Rentas', 'kod_rentas', 'Kod Rentas', $indicator_List['kod_rentas'], 3, $readonly, 7, 12, '', 'text');
					createTextBox('Alamat', 'alamat1', 'Alamat', $indicator_List['alamat1'], 3, $readonly, 7, 12, '', 'text');
					createTextBox('', 'alamat2', 'Alamat', $indicator_List['alamat2'], 3, $readonly, 7, 12, '', 'text');
					createTextBox('Susunan', 'susunan', 'Susunan', $indicator_List['susunan'], 3, $readonly, 7, 12, '', 'text');
					
					if($myID > 0)
					{
						$myArray = [];
						$myArray[] = array(1, 'YA');
						createDropDown_Array('Aktif', $myArray, 'aktif', $indicator_List['aktif'], 'TIDAK', 7, 3, 12, $readonly);
					}
				?>
			</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(['desk_bank', 'Nama Bank', 'tb']);
		indicator_list.push(['kod_bank', 'Kod Bank', 'tb']);
		indicator_list.push(['digit_bank_min', 'Bil Digit Minimum Akaun', 'tb']);
		indicator_list.push(['digit_bank', 'Bil Digit Maksimum Akaun', '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>