var usermanajemen=(function(){
	var hal=0;
	var jumlah=30;
	var iduser=0;
	var ubahpass=0;
	var idpegawai=0;
	return{
		caricanggih:function(){
			mload.sh();
			var ik_data = {ajax: 1,mode: "mod",cl: "usermanajemen",
				ikdata: {0:"datacari01",1:1}
			};
			
			$.ajax({url: lokal+"man.ajax.php",type: "POST",dataType: "json",async:true,data: ik_data,
				success: function(e){
					mload.hd();
					if(e!=false){
						var boxcari;
						var obj=new Object;
						obj.proses=function(a){
							$("#cari").val(a);
							boxcari.tutup();
							document.getElementById("btcari").click();
						};
						var datacari={"data":e,"proses":obj}
						boxcari=caricanggih.buka(datacari);
					}
				}
			});
		},
		enter:function(e){
			if(e.which == 13 || e.keyCode == 13){
				usermanajemen.loade();
			}
		},
		loade:function(){
			$("#hal_ke").val(0);
			usermanajemen.load();
		},
		load:function(){
			var nohal=$("#hal_ke").val()
			jumlah=$("#jumlah_hal").val();
			hal=(nohal-1)*jumlah;
			var cr=$("#cari").val();
			
			$("#grid_usermanajemen").html("");
			var loaderdatauser=mload.loading({"el":"loader_datauser"});
			
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "usermanajemen",
				ikdata: {0:"getTabel",1:hal,2:jumlah,3:cr}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				dataType: "json",
				async:true,
				data: ik_data,
				success: function(e){
					loaderdatauser.tutup();
					//alert(JSON.stringify(e));
					if(e!=false && e.length>0){
						
						if(e.length>0 && e[0]['halaman']>0){
							var htm;
							for(var i=0;i<e[0]['halaman'];i++){
								htm+="<option value='"+(i+1)+"'>"+(i+1)+"</option>";
							}
							$("#hal_ke").html(htm);
							
						}
						$("#hal_ke").val(nohal);
						
						var datatb={
							box:"grid_usermanajemen",
							data:e,
							lebarmin:"500px",
							trklik:{"data":"id","fungsi":"barisklik"},
							title:"kategori Menu",
							isi:[
									{"col":"","judul":"No","tipe":"urut","align":"center","lebar":"50px","halaman":hal,"jumlah":jumlah},
									{"col":"namalevel","judul":"Level","tipe":"data","lebar":"120px"},
									{"col":"nama","judul":"Detail User","tipe":"tpl","tpl":"btdetailuser"},
									{"align":"center","judul":"Status","tipe":"tpl","tpl":"btstatus","lebar":"60px"},
									{"align":"center","judul":"Edit","tipe":"tpl","tpl":"btedit","lebar":"70px"}
									
								],
							mod:"mod",
							module:"usermanajemen"
						};
						tabel.multisub(datatb);
					}else{
						loaderdatauser.pesan("BELUM ADA USER LOGIN");
					}
				}
			});
		},
		
		buat:function(a){
			var scrmenu=modmenu.getmenuscr();
			document.location=lokal+"member/mod/usermanajemen?id="+a+"&scrmenu="+scrmenu;
		},
		kembali:function(){
			var scrmenu=modmenu.getmenuscr();
			document.location=lokal+"member/mod/usermanajemen?scrmenu="+scrmenu;
		},
		simpan:function(){
			$("#loader_simpan_user").html("");
			var nm=$("#nama").val();
			var level=$("#sl_level").val();
			var pegawai=kudus.getattrselect("sl_level","pegawai");
			
			var err="";
			if(nm=="") err+="Nama User belum diisi.</br>";			
			if(level=="" || level<0) err+="Level salah.</br>";
			if(pegawai==1 && idpegawai<=0) {
				err+="Pegawai belum dipilih salah.</br>";
			}
			if(iduser==0){
				var p1=$("#pass1").val();
				var p2=$("#pass2").val();
				var us=$("#username").val();
				if(p1==""){
					 err+="Password belum diisi.<br/>";
				}else{
					if(p1!=p2){
						 err+="Password tidak sama.<br/>";
					}
				}
				if(us=="") err+="Username belum diisi.<br/>";
			}else{
				if(ubahpass==1){
					var p1=$("#pass1").val();
					var p2=$("#pass2").val();
					var us=$("#username").val();
					if(p1==""){
						 err+="Password belum diisi.<br/>";
					}else{
						if(p1!=p2){
							 err+="Password tidak sama.<br/>";
						}
					}
					if(us=="") err+="Username belum diisi.<br/>";
				}
			}
			if(err!=""){
				$("#loader_simpan_user").html(err);
				return;
			}
			if(pegawai!=1) idpegawai=0;
			
			var fg="simpanusermanajemen";
			if(iduser>0){
				fg="editusermanajemen";
			}
			var loadersimpan=mload.loader({"keterangan":"Proses simpan data ..."});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "usermanajemen",
				ikdata: {0:fg,nama:nm,user:us,
					level:level,pass:p1,
					idpegawai:idpegawai,
					id:iduser,ubahpass:ubahpass}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				dataType: "json",
				async:true,
				data: ik_data,
				success: function(e){
					
					if(e!=false && e.hasOwnProperty("status")){
						if(e['status']==1){
							usermanajemen.kembali();
						}else{
							loadersimpan.error(e['pesan']);
						}
						
					}else{
						loadersimpan.error([{"pesan":"Gagal proses simpan data..."}]);
					}
				}
			});
		},
		hapus:function(a){
			if(a<=0) return;
			var boxyesno=null;
			var databox={
				judul:"Yakin akan menghapus data user login ini ?? ",
				proses:function(){
					var loaderhapus=mload.loader({"keterangan":"Proses hapus data.."});
					boxyesno.tutup();
					var ik_data = {
						ajax: 1,
						mode: "mod",
						cl: "usermanajemen",
						ikdata: {0:"hapusData",id:a}
					};
					
					$.ajax({
						url: lokal+"man.ajax.php",
						type: "POST",
						dataType: "json",
						async:true,
						data: ik_data,
						success: function(e){
							if(e==1){
								loaderhapus.error([{"pesan":"Berhasil"}]);
								usermanajemen.load();
							}else{
								loaderhapus.error([{"pesan":"Gagal hapus data"}]);
							}
						}
					});
				}
			};
			boxyesno=boxwin.yesno(databox);
			boxyesno.buka();
		},
		status:function(a,b,c,d){
			var st=1;
			if(tabel.gettabel(b).getdata(c,"status")==1){
				st=0;
			}
			
			var loaderstatus=mload.loader({"keterangan":"Proses update status .."});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "usermanajemen",
				ikdata: {0:"status","status":st,"id":a}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				dataType: "json",
				async:true,
				data: ik_data,
				success: function(e){
					
					//alert(JSON.stringify(e));
					if(e==1){
						loaderstatus.tutup();
						d.className="mini_dis";
						if(st==1) d.className="mini_ok";
						tabel.gettabel(b).setdata(c,"status",st);
					}else if(e==2){
						loaderstatus.error([{"pesan":"Tidak di perbolehkan none aktifkan akun diri sendiri!!!"}]);
					}else{
						loaderstatus.error([{"pesan":"Gagal update status ..."}]);
					}
				}
			});
		},
		
		loadlevel:function(a){
			iduser=a['id'];
			idpegawai=a['idpegawai'];
			
			$("#sl_level").val(a['level']);
			var lvl=a;
			ubahpass=0;
			$("#btbatalsimpan").hide();
			if(iduser<=0) {
				$("#btbatalsimpan").show();
				$("#username").focus();
			}
			
			$("#boxpass1").hide();
			$("#boxpass2").hide();
			$("#btubahpass").hide();
			$("#btbatalubahpass").hide();
			usermanajemen.levelch();
			
			if(iduser>0){
				$("#btubahpass").show();
				document.getElementById("btubahpass").onclick=function(){
					ubahpass=1;
					$("#btubahpass").hide();
					$("#boxpass1").show();
					$("#boxpass2").show();
					$("#btbatalubahpass").show();
				};
				
				document.getElementById("btbatalubahpass").onclick=function(){
					$("#btubahpass").show();
					$("#boxpass1").hide();
					$("#boxpass2").hide();
					$("#btbatalubahpass").hide();
					ubahpass=0;
				};
			}else if(iduser==0){
				$("#boxpass1").show();
				$("#boxpass2").show();
				$("#btbatalubahpass").hide();
				$("#btubahpass").hide();
			}
			
		},
		batalsimpan:function(){
			$("#username").val("");
			$("#nama").val("");
			$("#pass1").val("");
			$("#pass2").val("");
			
			$("#username").focus();
			$("#namapegawai").val("");
			$("#nip").val("");
			idpegawai=0;
		},
		levelch:function(){
			$("#boxpegawai").hide();
			var pegawai=kudus.getattrselect("sl_level","pegawai");
			if(pegawai==1) $("#boxpegawai").show();
		},
		pilihpegawai:function(){
			var obj=new Object;
			obj['judul']="Pilih Data Pegawai";
			obj['modepilih']="tunggal";
			obj.pilih=function(a){
				var datapegawai=a;
				var cari=false;
				$("#namapegawai").val(datapegawai['nama']);
				$("#nip").val(datapegawai['nip']);
				idpegawai=datapegawai['id'];
			};
			
			pilihpegawai.bukapegw(obj);
		},
	};
})();
