var userapi=(function(){
	var hal=0;
	var jumlah=30;
	var iddatauserapi=0;
	var iduser=0;
	var level=0;
	var boxcariuser=null;
	return{
		caricanggih:function(){
			mload.sh();
			var ik_data = {ajax: 1,mode: "mod",cl: "userapi",
				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){
				userapi.loade();
			}
		},
		loade:function(){
			$("#hal_ke").val(0);
			userapi.load();
		},
		load:function(){
			var nohal=$("#hal_ke").val()
			jumlah=$("#jumlah_hal").val();
			hal=(nohal-1)*jumlah;
			var cr=$("#cari").val();
			
			$("#grid_userapi").html("");
			mload.sh();
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "userapi",
				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){
					
					mload.hd();
					//alert(JSON.stringify(e));
					if(e!=false){
						
						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_userapi",
							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":"namauser","judul":"Nama User","tipe":"data","lebar":"180px"},
									{"col":"ipaddress","judul":"IP Address","tipe":"data","lebar":"120px"},
									{"col":"token","judul":"Token","tipe":"data"},
									{"align":"center","judul":"Status","tipe":"tpl","tpl":"btstatus","lebar":"60px"},
									{"align":"center","judul":"","tipe":"tpl","tpl":"btedit","lebar":"60px"},
									
								],
							mod:"mod",
							module:"userapi"
						};
						tabel.multisub(datatb);
					}
				}
			});
		},
		buat:function(a){
			document.location=lokal+"member/mod/userapi?id="+a;
		},
		kembali:function(){
			document.location=lokal+"member/mod/userapi";
		},
		edit:function(a){
			iddatauserapi=a['id'];
			iduser=a['iduser'];
			level=a['level'];
			
			$("#boxnamauser").hide();
			if(level==30){
				$("#boxnamauser").show();
			}
		},
		cariuser:function(e){
			if(e.which == 13 || e.keyCode == 13){
				
				
				$("#grid_data_user").html("");
				
				var ps={"box":"box_cari_user",
					"judul":"Pilih Nama User",tinggi:"auto","lebar":"600"};
		
				boxcariuser=boxwin.load(ps);
				boxcariuser.buka();
				$("#cari_user").val($("#namauser").val());

				userapi.loadeuser();
			}
		},
		loadeuser:function(){
			$("#hal_ke_user").val(0);
			userapi.loaddatauser($("#hal_ke_user").val(),$("#jumlah_hal_user").val());
		},
		loaduser:function(){
			userapi.loaddatauser($("#hal_ke_user").val(),$("#jumlah_hal_user").val());
		},
		enteruser:function(e){
			if(e.which == 13 || e.keyCode == 13){
				userapi.loadeuser();
			}
		},
		loaddatauser:function(h,jm){
			$("#grid_data_user").html("");
			
			var cr=$("#cari_user").val();
			var posisiawal=(h-1)*jumlah;
			
			$("#hal_ke_user").html("<option value=\"1\">1</option>");
			
			mload.sh();
			var ik_data = {ajax: 1,mode: "mod",cl: "userapi",
				ikdata: {0:"getdatauser",halaman:h,jumlah:jm,cari:cr}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",type: "POST",dataType: "json",async:true,data: ik_data,
				success: function(e){
					
					mload.hd();
					//alert(JSON.stringify(e));
					if(e!=false && e.hasOwnProperty("data")){
						if(e['data'].length>0){
							var jumlahhalaman=e['totalhalaman'];
							if(jumlahhalaman>1){
								var htm;
								for(var i=0;i<jumlahhalaman;i++){
									htm+="<option value='"+(i+1)+"'>"+(i+1)+"</option>";
								}
								$("#hal_ke_user").html(htm);
								
							}
							$("#hal_ke_user").val(h);
							
							var datatb={
								box:"grid_data_user",
								data:e['data'],
								lebarmin:"500px",
								trklik:{"data":"id","fungsi":"barisklik"},
								title:"kategori Menu",
								isi:[
										{"col":"","judul":"No","tipe":"urut","align":"center","lebar":"50px","halaman":posisiawal,"jumlah":jumlah},
										{"col":"nama","judul":"Nama user","tipe":"data"},
										{"align":"center","judul":"","tipe":"tpl","tpl":"btpilihuser","lebar":"60px"}
									],
								mod:"mod",
								module:"userapi"
							};
							var tbuser=tabel.multisub(datatb);
						}else{
							$("#grid_data_user").html("Tidak ada data");
						}
						
					}else{
						$("#grid_data_user").html("Error");
					}
				}
			});
		},
		pilihdatauser:function(a,b,c,d){
			iduser=a;
			$("#namauser").val(tabel.gettabel(b).getdata(c,"nama"));
			userapi.batalpilihuser();
		},
		batalpilihuser:function(){
			if(boxcariuser!=null) boxcariuser.tutup();
			boxcariuser=null;
		},
		simpan:function(){
			$("#box_error_simpan").html("");
			var ipadd=$("#idaddress").val();
			var cb=$("#callback").val();
			
			var err="";
			if(ipadd=="") err+="IP Address belum diisi.</br>";
			if(cb=="") err+="Link callback belum diisi.</br>";
			if(level==30){
				if(iduser<=0) err+="Nama user belum dipilih.</br>";
			}
			
			if(err!=""){
				$("#box_error_simpan").html(err);
				return;
			}
			var fg="inputuserapi";
			if(iddatauserapi>0){
				fg="edituserapi";
			}
			mload.sh();
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "userapi",
				ikdata: {0:fg,iduser:iduser,ipaddress:ipadd,callback:cb,id:iddatauserapi}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				dataType: "json",
				async:true,
				data: ik_data,
				success: function(e){
					
					mload.hd();
					if(e==1){
						document.location=lokal+"member/mod/userapi";
					}
				}
			});
		},
		hapus:function(a){
			if(a<=0) return;
			var boxyesno=null;
			var databox={
				judul:"Yakin akan menghapus data api ini ini ?? ",
				proses:function(){
					mload.sh();
					var ik_data = {
						ajax: 1,
						mode: "mod",
						cl: "userapi",
						ikdata: {0:"hapusData",id:a}
					};
					
					$.ajax({
						url: lokal+"man.ajax.php",
						type: "POST",
						dataType: "json",
						async:true,
						data: ik_data,
						success: function(e){
							mload.hd();
							if(e==1){
								userapi.load();
								boxyesno.tutup();
								
							}
						}
					});
				}
			};
			boxyesno=boxwin.yesno(databox);
			boxyesno.buka();
			
		},
		setstatus:function(a,b,c,d){
			var status=tabel.gettabel(b).getdata(c,"status");
			var st=0;
			if(status==0){st=1;}
			
			mload.sh();
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "userapi",
				ikdata: {0:"setstatus",id:a,status:st}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				dataType: "json",
				async:true,
				data: ik_data,
				success: function(e){
					mload.hd();
					if(e==1){
						d.className='mini_dis';
						if(st==1){
							d.className='mini_ok';
						}
						tabel.gettabel(b).setdata(c,"status",st);
					}
				}
			});
		}
	};
})();