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

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

$indicator_Query = "SELECT t_pengguna.*
				  FROM t_pengguna
				  WHERE id_pengguna = ?";
$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 Pengguna</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="pengguna_form" id="pengguna_form">
		<div class="form-body">
			<div class="row">
				<?php
					$panduanKataLaluan = '- Tidak kurang dari 8 karakter<br>- Gabungan alphabet, nombor dan simbol<br>- Simbol yang dibenarkan adalah !@#$*_ sahaja';
					createTextBox('Nama', 'nama', 'Nama', $indicator_List['nama'], 3, $readonly, 7, 12, '', 'text', 'Sila masukkan nama pengguna');
					if($myID > 0)
					{
						createLabels('No Kad Pengenalan', 'kp', $indicator_List['kpbaru'], '', 3, 7, 0, 12);
					}
					createTextBox('No Tel Pejabat/HP', 'no_tel', 'No Tel Pejabat/HP', $indicator_List['no_tel'], 3, $readonly, 7, 12, '', 'numonly');
					createTextBox('Email', 'emel', 'Email', $indicator_List['emel'], 3, $readonly, 7, 12, '', 'email');
					createDropDown($PDO, 'Jabatan', 'i_jabatan', 'id_jabatan', $indicator_List['id_jabatan'], 'Sila Pilih Jabatan', 7, 3, 12, $readonly);
					if($my_details->user_level == 1 || $my_details->user_level == 3)
					{
						createDropDown($PDO, 'Peranan', 't_peranan', 'id_peranan', $indicator_List['id_peranan'], 'Pilih Peranan', 7, 3, 12, $readonly);
					}
					else if($my_details->user_level == 2)
					{						
						$mySQL = "SELECT id_peranan AS id, desk_peranan as name FROM t_peranan WHERE id_peranan NOT IN (1,3) ORDER BY susunan";										
						createDropDown_sql($PDO, 'Peranan', $mySQL, 'id_peranan', $indicator_List['id_peranan'], 'Pilih Peranan', 7, 3, 12, $readonly,'');
					}
					else
					{						
						$mySQL = "SELECT id_peranan AS id, desk_peranan as name FROM t_peranan WHERE id_peranan NOT IN (1,2,3) ORDER BY susunan";										
						createDropDown_sql($PDO, 'Peranan', $mySQL, 'id_peranan', $indicator_List['id_peranan'], 'Pilih Peranan', 7, 3, 12, $readonly,'');
					}
					//createDropDown($PDO, 'Parlimen', 'i_parlimen', 'id_parlimen', $indicator_List['id_parlimen'], 'Sila Pilih Parlimen', 7, 3, 12, $readonly);
					//createDropDown($PDO, 'DUN', 'i_dun', 'id_dun', $indicator_List['id_dun'], 'Sila Pilih DUN', 7, 3, 12, $readonly);
					createDropDown($PDO, 'Daerah', 'i_daerah', 'id_daerah', $indicator_List['id_daerah'], 'Sila Pilih Daerah', 7, 3, 12, $readonly);
					if($myID == 0)
					{
						createTextBox('Login ID (No KP Baru)', 'pengguna_login', 'Login ID (No KP Baru)', $indicator_List['login'], 3, $readonly, 7, 12, '', 'numonly', 'Sila masukkan Login ID pengguna');
						createTextBox('Katalaluan', 'pengguna_pwd', 'Katalaluan', '', 3, $readonly, 7, 12, '', 'password', $panduanKataLaluan);
						createTextBox('Katalaluan (Ulang)', 'pengguna_pwd2', 'Katalaluan (Ulang)', '', 3, $readonly, 7, 12, '', 'password');
					}
					
					if($my_details->user_level == 1 || $my_details->user_level == 3)
					{
						$flag_aktif = 1;
						if($myID > 0)
						{
							$flag_aktif = $indicator_List['aktif'];						
						}
						createDropDown($PDO, 'Status Aktif', 'i_aktif', 'aktif', $flag_aktif, 'Pilih Status Aktif', 7, 3, 12, $readonly);
					}
					createTextArea('Catatan', 'catatan', 'Catatan', $indicator_List['catatan'], 0, 3, 7, 12, '', 4);
				?>
			</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="pengguna_simpanBtn">Simpan</button>';
		}
	?>
</div>

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

	$('.select2').select2({ width: '100%' }); 
	
	$('#pengguna_simpanBtn').click(function() {
		var pengguna_list = [[]];
		pengguna_list.push(['nama', 'Nama', 'nm']);
		pengguna_list.push(['emel', 'Email', 'em']);
		pengguna_list.push(['pengguna_login', 'Login ID', 'tb']);
		if("<?=$myID?>" == 0)
		{
			pengguna_list.push(['pengguna_pwd', 'Kata Laluan', 'pwd']);
		}
		pengguna_list.push(['id_jabatan', 'Jabatan', 'dd']);
		pengguna_list.push(['id_peranan', 'Peranan', 'dd']);
		if($('#id_jabatan').val() == 2 || $('#id_jabatan').val() == 5)
		{
			pengguna_list.push(['id_daerah', 'Daerah', 'dd']);			
		}
		else if($('#id_jabatan').val() == 3)
		{
			pengguna_list.push(['id_dun', 'DUN', 'dd']);			
		}
		else if($('#id_jabatan').val() == 4)
		{
			pengguna_list.push(['id_parlimen', 'Parlimen', 'dd']);			
		}
		
		if (checkInput(pengguna_list))
		{
			if($('#pengguna_pwd').val() == $('#pengguna_pwd2').val())
			{
				$(this).prop("disabled",true);
				var param = $('#pengguna_form').serialize();
				$.ajax({
					type: "POST", 
					url: "in_pengguna-tambah_insert.php", 
					data: param+ '&id=' + "<?=$myID?>"+ '&temp=' + "<?=$temp?>",

					success: function(res){
						if(res > 0)
						{
							swal.fire({
								text: "Berjaya Kemaskini Pengguna",
								icon: "success",
								buttonsStyling: false,
								confirmButtonText: "OK!",
								customClass: {
									confirmButton: "btn font-weight-bold btn-light-primary"
								}
							}).then(function() {
								window.parent.reloadTable();
								$("#closeBtn").click();
							});
						}
						else if (res == -888)
						{
							swal.fire({
								text: "Login id already exist in the system",
								icon: "error",
								buttonsStyling: false,
								confirmButtonText: "OK!",
								customClass: {
									confirmButton: "btn font-weight-bold btn-light-primary"
								}
							}).then(function() {
								$('#pengguna_login').focus();
								$(this).prop("disabled",false);									
								});							
						}
						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
			}
			else
			{
				swal.fire({
					text: "Katalaluan tidak sepadan",
					icon: "error",
					buttonsStyling: false,
					confirmButtonText: "OK!",
					customClass: {
						confirmButton: "btn font-weight-bold btn-light-primary"
					}
				}).then(function() {
					$('#pengguna_pwd2').focus();
				});
				return false;
			}
		}
	});		
});		
</script>
