var pilihpegawai=(function(){
	var boxpegawai=null;
	var module="";
	return{
		init:function(a){
			module=a['module'];
		},
		bukapegw:function(a){
			var judulbox=a['judul'] || "Pilih Data Pegawai";
			var modepilih=a['modepilih'] || "tunggal";
			
			var ps={"box":"box_pegawai",
					"judul":judulbox,tinggi:"auto","lebar":"700px"};
		
			boxpegawai=boxwin.load(ps);
			boxpegawai.obj=a;
			boxpegawai.modepilih=modepilih;
			boxpegawai.arrpilih=[];
			boxpegawai.buka();
			$("#box_loader_pegw").html("");
			
			$("#btprosesmulti").hide();
			if(modepilih=="multi") $("#btprosesmulti").show();
			pilihpegawai.loadepegw();
		},
		caripegw:function(e){
			if(e.which == 13 || e.keyCode == 13){
				$("#cari_pegw").val($("#namapegw").val());
				pilihpegawai.loadepegw();
			}
		},
		loadepegw:function(){
			$("#hal_ke_pegw").val(0);
			pilihpegawai.loaddatapegw($("#hal_ke_pegw").val(),$("#jumlah_hal_pegw").val());
		},
		loadpegw:function(){
			pilihpegawai.loaddatapegw($("#hal_ke_pegw").val(),$("#jumlah_hal_pegw").val());
		},
		enterpegw:function(e){
			if(e.which == 13 || e.keyCode == 13){
				pilihpegawai.loadepegw();
			}
		},
		loaddatapegw:function(h,jm){
			$("#grid_data_pegw").html("");
			var cr=$("#cari_pegw").val();
			var posisiawal=(h-1)*jm;
			
			$("#hal_ke_pegw").html("<option value=\"1\">1</option>");
			
			var obj={"idbox":"box_loader_pegw"};
			var boxloader=mload.init(obj);
			boxloader.load();
			
			var ik_data = {ajax: 1,mode: "plg",
				cl: "pilihpegawai",
				ikdata: {0:"getdatapegawai",
					module:module,
					mode:boxpegawai.mode,
					halaman:h,jumlah:jm,cari:cr}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",type: "POST",
				dataType: "json",async:true,data: ik_data,
				success: function(e){
					
					boxloader.tutup();
					if(e.hasOwnProperty("data") && e['data'].length>0){
						var htm="<option value=\"1\">1</option>";
						var jumlahhalaman=e['totalhalaman'];
						if(jumlahhalaman>1){
							for(var i=0;i<jumlahhalaman;i++){
								htm+="<option value='"+(i+1)+"'>"+(i+1)+"</option>";
							}
							$("#hal_ke_pegw").html(htm);
						}
						
						$("#hal_ke_pegw").val(h);
						var tplpilih="btpilihtunggal";
						if (boxpegawai.modepilih=="multi"){
							tplpilih="btpilihmulti";
						}
						var datatb={
							box:"grid_data_pegw",
							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":jm},	
									{"col":"nama","judul":"Detail Pegawai","tipe":"tpl","tpl":"btdetailpegawai"},
									{"col":"golongan","judul":"Gol.","tipe":"data","col":"golongan","lebar":"60px","align":"center"},
									{"align":"center","judul":"Status","tipe":"tpl","tpl":"btstatus","lebar":"120px"},
									{"align":"center","judul":"","tipe":"tpl","tpl":tplpilih,"lebar":"60px"},
									
								],
							mod:"mod",
							module:"pilihpegawai"
						};
						tabel.multisub(datatb);
						
					}else{
						$("#grid_data_pegw").html("Tidak ada data!!")
					}
				}
			});
		},
		pilihpegawai:function(a,b,c){
			boxpegawai.obj.pilih(tabel.gettabel(b).getdatabaris(c));
			pilihpegawai.bataldatapegw();
		},
		pilihpegawaimulti:function(a,b,c){
			var idtb=b+"_"+c;
            var attst=document.getElementById("btpilihmulti_"+idtb).getAttribute("stpilih");
            
            var st=0;
            if(attst==0){
                st=1;
                boxpegawai.arrpilih.push(tabel.gettabel(b).getdatabaris(c));
                
                document.getElementById("btpilihmulti_"+idtb).className="bt_ya";
                document.getElementById("btpilihmulti_"+idtb).setAttribute("stpilih",1);
            }else{
                var index = kudus.getindexarrkey(a,boxpegawai.arrpilih,"id");
                if(index > -1)  {
                    boxpegawai.arrpilih.splice(index, 1);
                }
                
                document.getElementById("btpilihmulti_"+idtb).className="bt_tidak";
                document.getElementById("btpilihmulti_"+idtb).setAttribute("stpilih",0);
            }
            
		},
		bataldatapegw:function(){
			if(boxpegawai!=null) boxpegawai.tutup();
			boxpegawai=null;
		},
		prosespegawai:function(){
			$("#box_loader_pegw").html("");
			if(boxpegawai.arrpilih.length<=0){
				$("#box_loader_pegw").html("Belum ada pegawai yang dipilih!");
				return;
			}
			var datapegawai=boxpegawai.arrpilih;
			
			boxpegawai.obj.pilih(datapegawai);
			pilihpegawai.bataldatapegw();
		}
	};
})();
