<?php

	//HARAP PERHATIAN - SETTING PHP DAN MYSQL HARUS DI RUBAH
	//edit my.ini  => max_allowed_packet = 916M
	//edit php.ini => upload_max_filesize = 900M

	//baca allfunc.js ini header.php
	include("header.php"); 

	//baca security
	$nameprog =basename($_SERVER['PHP_SELF']); 
	include("ceklogin.php");

	//simpan alamat yg akan di baca supaya saat login bisa redirect ke halaman ini
	$_SESSION['actual_link'] = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
	//$_SESSION['actual_link']=end(explode('/',$_SERVER['REQUEST_URI']));
	$fkodekantorx=$_SESSION['f_kodekantor'];

	//20150527, Anggota Direksi bisa akses smua tapi hanya boleh lihat
	//baca security
	//- variable yg bisa di pakai : $open_hakx:true-disabled bisa diganti $_SESSION['f_open']:true-false, $add_hakx, $edit_hakx, $del_hakx, $print_hakx, $bcadd_hakx, $bcdel_hakx
	//if ((!isset($_SESSION['f_user'])  && !isset($_SESSION['f_pass'])) || $open_hakx=='disabled') {
	//if ( !isset($_SESSION['f_user']) || !isset($_SESSION['f_pass']) || !$_SESSION['f_open'] ){
	if ( !isset($_SESSION['f_user']) || !isset($_SESSION['f_pass'])){
		echo "
		<script>
		$.messager.alert('Warning','<font color=red>GAGAL, Anda tidak berhak akses</font>','info',
			function () {
				//var i = 1;
				//alert(i);
				window.location = 'index.php';
				}
		);   
		</script>
		";
		die();
	}

	//baca session, Source => http://www.infragistics.com/community/blogs/taz_abdeali/archive/2012/02/08/netadvantage-for-jquery-with-php-101.aspx
	$fuserx		=isset($_SESSION['f_user'])			?$_SESSION['f_user']	:""; 
	$fpassx		=isset($_SESSION['f_pass'])			?$_SESSION['f_pass']	:"";

	//cek posting
	$ffullnamex				=isset($_POST['fullnamex'])					?$_POST['fullnamex']				:"";
	$foldpasswordx		=isset($_POST['oldpasswordx'])			?$_POST['oldpasswordx']			:"";
	$fpasswordx				=isset($_POST['passwordx'])					?$_POST['passwordx']				:"";
	$fconfirmpassword	=isset($_POST['confirmpasswordx'])	?$_POST['confirmpasswordx']	:"";
	$femailx					=isset($_POST['emailx'])						?$_POST['emailx']						:"";
	$fkodekantorx			=isset($_POST['kodekantorx'])				?$_POST['kodekantorx']			:"";
	$simpanx					=isset($_POST['simpan'])						?$_POST['simpan']						:false;
	$kuncix						=isset($_POST['kunci'])							?$_POST['kunci']						:"";

	//parameter utk search 
	$wherex="1=1";
	$wherex .= !empty($fuserx)	? " and user='$fuserx'"			: "";
	$wherex .= !empty($fpassx)	? " and password='$fpassx'" : "";

	//initial
	$today = date("d-m-Y"); //simpan tanggal hari ini

	//aktifkan session
	if(!isset($_SESSION)){session_start();}
	//baca isi session
	
	//jika tombol tidak simpan di klik
	//$simpanx=true;
	if (!$simpanx){
		//ambil info dari table
		$qstring = "select * from apl_user where $wherex";
		$rs = mysqli_query($conn_login,$qstring);//query the database for entries containing the term
		//simpan info variable yang terkirim dari page, post, session sebelumnya
		//$wherex=$qstring;
		//include("LogCekVar.php");
		//die();
		if ($rs){
			while ($row = mysqli_fetch_array($rs,MYSQL_ASSOC))//loop through the retrieved values
			{
				$ffullnamex		=$row["FULLNAME"];
				$femailx			=$row["EMAIL"];
				$fpasswordx		=$row["PASSWORD"];
				$fkodekantorx	=$row["KODE_KANTOR"];
			}		
		}
	}else{
		//$_SESSION['f_pakai']			= "OK";
		//$_SESSION['f_level']			= $row ['LEVEL'];
		//$_SESSION['f_kodekantor']	= $row ['KODE_KANTOR'];
		//$_SESSION['f_keterangan'] = $row ['KETERANGAN'];
		//$_SESSION['f_fullname']		= $row ['FULLNAME'];
		//$_SESSION['f_masuk']			= date('Y-m-d H:i:s');
		//include("LogCekVar.php");
		//echo json_encode(array ('isimsg'=>"test")); 
		die();
	}

	//ambil nilai variable php ke javascript
	echo "
	<script>
			kode_kantor='$fkodekantorx';

			//supaya combobox readonly
			tfcombo1=false;

			//cek supaya combobox disable
			temp='$bcadd_hakx';
			if (temp=='disabled'){
				tfcombo1status='disable';
			}else{
				tfcombo1status='enable';
			}
	</script>";
?>

<!DOCTYPE html> 
<!-- tulisan 'DOCTYPE html' ==> INI HARUS HARUS ADA DAN LETAKNYA PALING ATAS (case sensitive)
sumber: 
http://users.tpg.com.au/j_birch/plugins/superfish/download/ 
en.wikipedia.org/wiki/Document_type_declaration
-->
<html>
<head>
  <?php $judulweb="Data pegawai";?>
  <title><?php echo $judulweb;?></title>
</head>		

<body>	
<div style="width:380px;height:370px;"></div>
<!-- FORM regEdit -->
<div id="regiseditAdd_dlg" class="easyui-dialog" title="Change Profile User" style="top:150px;left:200px;width:380px;height:340px;padding:10px 20px" closed="false" buttons="#regiseditAdd_dlg-buttons" data-options="iconCls:'icon-save',resizable:true,modal:false">
	<div class="ftitle">Data profile user - Information</div>
	<form id="frm_signup" name="frm_signup" method="post" action="<?php echo basename(__FILE__); ?>">
		<input type="hidden" name="form_name" value="signupform">
		<div class="fitem">
		<label>User Name:</label>
		<input id="usernamex" name="usernamex" class="easyui-validatebox" style="width:180px;line-height:16px;border:1px solid #ccc" data-options="required:false" maxlength="99" value="<?php echo $fuserx;?>">
		</div>
		<div class="fitem">
		<label>Full Name:</label>
		<input id="fullnamex" name="fullnamex" class="easyui-validatebox" style="width:180px;line-height:16px;border:1px solid #ccc" data-options="required:false" maxlength="99" value="<?php echo $ffullnamex;?>">
		</div>
		<div class="fitem">
		<label>Password lama:</label>
		<input id="oldpasswordx" name="oldpasswordx" class="easyui-passwordbox" style="width:180px;line-height:16px;border:1px solid #ccc" data-options="required:false" maxlength="99" >
		</div>
		<div class="fitem">
		<label>Password:</label>
		<input id="passwordx" name="passwordx" class="easyui-passwordbox" style="width:180px;line-height:16px;border:1px solid #ccc" data-options="required:false" maxlength="99" >
		</div>
		<div class="fitem">
		<label>Konfirmasi Password:</label>
		<input id="confirmpasswordx" name="confirmpasswordx" class="easyui-passwordbox" style="width:180px;line-height:16px;border:1px solid #ccc" data-options="required:false" maxlength="99" >
		</div>
		<div class="fitem">
		<label>E-mail:</label>
		<input id="emailx" name="emailx" class="easyui-validatebox" style="width:180px;line-height:16px;border:1px solid #ccc" data-options="required:false" maxlength="99" value="<?php echo $femailx;?>">
		</div>
		<div class="fitem">
		<label>Kantor Cabang:</label>
    <input id="kodekantorx" name="kodekantorx"  style="width:180px;line-height:16px;border:1px solid #ccc" data-options="required:false" maxlength="3" <?php echo $bcadd_hakx; ?> >
		</div>
	</form>
</div>
<!-- div id="progressFile" class="easyui-progressbar" style="width:100%;"></div -->
<div id="regiseditAdd_dlg-buttons">
	<!-- a id="bupload" href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveUpload()" style="width:90px">Save</a -->
	<a id="bpegawai" href="#" class="easyui-linkbutton" iconCls="icon-save" plain="false" onClick="doSave()">Save</a>
	<!-- a id="btn-upload" href="javascript:void(0)">Upload</a -->
	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="doClose()" style="width:90px">Cancel</a>
</div>
<!-- END FORM UTK ADD/EDIT DATA PEGAWAI-->

<script type="text/javascript"> 
	//utk open form upload
	function doAdd(){
		//kosongkan semua isi form
		$('#formAdd_pegawai').form('clear');
		//initial
		var x = new Date();
		//open dialog
		$('#regiseditAdd_dlg').dialog('open').dialog('setTitle','Add Data Pegawai');
		//mengganti tombol form 
		$('#bpegawai').linkbutton({text:'Add',iconCls: 'icon-save'});

		$('#addpegawai_tglmasuk').datebox('setValue', myformattertgl(x));
		//$('#addpegawai_tglkeluar').datebox('setValue', ' ');
		$( "#addpegawai_status").prop("checked", true);

		//setcombobox
		$('#addpegawai_kodekantor').combobox('setValue', kode_kantor);
		
		kuncix='ambil_id';
		$.ajax({
			type: "POST", //$(this).attr('method'), 
			url:"pegawaiFunc.php",//$(this).attr('action'),
			data: "kunci="+kuncix,//+"&ss="+dd, //"kunci=John&submit=Boston",
			dataType: "json",  //hanya di pasang jika data php menggunakan json
			success: function(datax) {	
				if (datax.berhasil){
					$('#addpegawai_id').val(datax.isimsg);
					//set focus combobox
					//$('#addpegawai_kodekantor').next().find('input').focus();
					//set focus standar
					//$('#addpegawai_nama').focus();
					//set focus standar
					$('#addpegawai_kartu').focus();
				}else{
					//alert(datax.isimsg);
					$.messager.alert('Status','Error.. Gagal ambil nilai terakhir ID pegawai','error'); //error,question,info,warning
				}				
			},
			error: function(){
				alert("errrr gagal kirim");
			},
			complete: function() {
				//alert("complete");
			}
		});
	}
	//utk close form upload
	function doClose(){
		//$('#addtoner_nofaktur').validatebox({required: false});
		//$( "#addtoner_nofaktur").validatebox('isValid');//utk cek apakah sdh valid blm
		$('#regiseditAdd_dlg').dialog('close');
	}
	function doEdit(){
		//initial
		//var x = new Date();

		var rows = $('#pegawai').datagrid('getSelections');
		if (rows.length>=1){
			//open dialog
			$('#regiseditAdd_dlg').dialog('open').dialog('setTitle','Edit Data Pegawai');
			//mengganti tombol form 
			$('#bpegawai').linkbutton({text:'Edit',iconCls: 'icon-save'});
		}else{
			$.messager.alert('Status',"<font color=red><b>Error, Harap pilih salah satu baru tekan EDIT lagi</b></font>");
		}
	}	
	//utk save
	function doSave(){
		//cek apakah login sudah expired
		//doCeklogin();

		//enable dan simpan value dari text combobox
		$('#kodekantorx').combobox('enable');
		$('#kodekantorx').combobox('setValue',$('#kodekantorx').combobox('getText'));

		//mulai simpan
		urlx="regisEditFunc.php";
		$.ajax({
			type:"POST", //$('#frm_salestarget').attr("method"),
			url:urlx,//$('#frm_salestarget').attr("action"),
			//async: true,
			//contoh penambahan variable manual => "KUNCI="+$('#kunci').val()+"&"+$("form").serialize(),
			//data:"kuncibc="+kunciku+"&nofaktur="+nofakturx+"&tglfaktur="+tglfakturx+"&kdcust="+kdcustx+"&ketbrg="+ketbrgx.replace(/&nbsp;/g, ' '),
			data:"simpan=true&"+$("#frm_signup").serialize(),
			dataType:"json",
			success: function(data) {
				if (data.berhasil){
					window.location.href = "menuutama.php";
				}
				$.messager.show({
					title:'Message',
					msg:data.isimsg,
					timeout:3000,
					height:'auto',
					showType:'slide'
				});
			},
			error : function() {
				//alert("Error... Please Cek URL - Data gagal di Proses!!!.");
				$.messager.show({
					title:'ERROR URL',
					msg:"Cek Ulang URL: <b>"+urlx+"</b>",
					timeout:3000,
					height:'auto',
					showType:'slide'
				});
			},
			complete: function() {
				//alert("Complete Proses!!!.");						
			}							
		});		

		//alert("Pesan terkirim.. silahkan tunggu!");
		//Alert("Proses Update...!");

		//menormalkan standar awal combobox
		$("#kodekantorx").combobox(tfcombo1status);
	}
	//utk delete data
	function doDelete(){
		//cek login
		//doCeklogin();

		var rows = $('#pegawai').datagrid('getSelections');
		if (rows.length>=1){
			//alert('test');
			//membuat list data grid ke array
			var msg= [];
			var ss = [];
			for(var i=0; i<rows.length; i++){
				var row = rows[i];
				//msg.push('<span>'+row.BARCODE+":"+row.NAMA+":"+myformattertgl(new Date(row.TGMASUK))+'</span>');
				msg.push('<span>'+row.id+":"+row.nama+":"+row.tgl_masuk+'</span>');
				ss.push({
					id				:row.id,
					nama			:row.nama,
					tgl_masuk	:row.tgl_masuk
				});
				//dd=JSON.stringify(ss);
			}
			//msg.push("?");
			//memunculkan warning delete
			$.messager.confirm('Confirm','<font color="red"><b>Are you sure delete :</b></font><br>'+msg.join('<br/>'),function(r){
				if (r){
					var dd=JSON.stringify(ss);
					var urlx="pegawaiFunc.php";
					var kunci="delpegawai";
					$.ajax({
						type:"POST", //$('#frm_salestarget').attr("method"),
						url:urlx, //$('#frm_salestarget').attr("action"),
						async: false,
						//contoh penambahan variable manual => "KUNCI="+$('#kunci').val()+"&"+$("form").serialize(),
						//data:"kuncibc="+kunciku+"&nofaktur="+nofakturx+"&tglfaktur="+tglfakturx+"&kdcust="+kdcustx+"&ketbrg="+ketbrgx.replace(/&nbsp;/g, ' '),
						data:"kunci="+kunci+"&ids="+dd, //$("#formAdd_toner").serialize(),
						dataType:"json",
						success: function(data) {
							//$.messager.show({
							//	title:'Message',
							//	msg:data.isimsg,
							//	timeout:3000,
							//	height:'auto',
							//	showType:'slide'
							//});
							$.messager.alert('Data sudah di hapus',data.isimsg);
							if (data.berhasil){
								$("#pegawai").datagrid("reload");
							}
						},
						error : function() {
							//alert("Error... Please Cek URL - Data gagal di Proses!!!.");
							$.messager.show({
								title:'ERROR URL',
								msg:"Cek Ulang URL: <b>"+urlx+"</b>",
								timeout:3000,
								height:'auto',
								showType:'slide'
							});
						},
						complete: function() {
							//alert("Complete Proses!!!.");						
						}					
					});									

					//$("#toner").datagrid("reload");
					//$.messager.alert('Data sudah di hapus', msg.join('<br/>'));
				}
			});
		}else{
			$.messager.alert("Err..","select pegawai dulu ya");
		}
	}
</script> 

<script type="text/javascript">  
	$(function() {  
	});

	//mengisi combobox kodekantorx
	$('#kodekantorx').combobox({
		data:
			[{
					"id":"Solo",
					"text":"001",
			},{
					"id":"Klaten",
					"text":"002"
			},{
					"id":"Sragen",
					"text":"003"
			},{
					"id":"Boyolali",
					"text":"004"
			},{
					"id":"Magelang",
					"text":"005"
			}]
		,
		valueField:'id',
		textField:'text',
		editable:tfcombo1,
		formatter: FormatKdsup
	});
	$('#kodekantorx').combobox('setValue',kode_kantor);
	//$('#kodekantorx').combobox('select',kode_kantor);
</script>

<style type="text/css">  
		#fm{  
				margin:0;  
				padding:10px 30px;  
		}  
		.ftitle{  
				font-size:14px;  
				font-weight:bold;  
				color:#666;  
				padding:5px 0;  
				margin-bottom:10px;  
				border-bottom:1px solid #ccc;  
		}  
		.fitem{  
				margin-bottom:5px;  
		}  
		.fitem label{  
				display:inline-block;  
				width:100px;  
		}  
		.fitem2 label{  
				display:inline-block;  
				width:180px;  
		}  
		/*== membuat animasi cursor ==*/
		input {
				cursor: pointer; /*default =>https://developer.mozilla.org/id/docs/Web/CSS/cursor */
		}
</style>  

<!-- FOOTER -->
<?php include_once("footer.php"); ?>
</body>

</html>

