var pilihstatus=(function(){
	var boxstatus=null;
	var module="";
	return{
		init:function(a){
			module=a['module'];
		},
		bukastus:function(a){
			var judulbox=a['judul'] || "Pilih Data Status";
			var modepilih=a['modepilih'] || "tunggal";
			
			var ps={"box":"box_status",
					"judul":judulbox,tinggi:"auto","lebar":"700px"};
		
			boxstatus=boxwin.load(ps);
			boxstatus.obj=a;
			boxstatus.modepilih=modepilih;
			boxstatus.arrpilih=[];
			boxstatus.buka();
			$("#box_loader_stus").html("");
			
			$("#btprosesmulti").hide();
			if(modepilih=="multi") $("#btprosesmulti").show();
			pilihstatus.loadestus();
		},
		caristus:function(e){
			if(e.which == 13 || e.keyCode == 13){
				$("#cari_stus").val($("#namastus").val());
				pilihstatus.loadestus();
			}
		},
		loadestus:function(){
			$("#hal_ke_stus").val(0);
			pilihstatus.loaddatastus($("#hal_ke_stus").val(),$("#jumlah_hal_stus").val());
		},
		loadstus:function(){
			pilihstatus.loaddatastus($("#hal_ke_stus").val(),$("#jumlah_hal_stus").val());
		},
		enterstus:function(e){
			if(e.which == 13 || e.keyCode == 13){
				pilihstatus.loadestus();
			}
		},
		loaddatastus:function(h,jm){
			var idkategori=$("#sl_kat_stus").val();
			$("#grid_data_stus").html("");
			var cr=$("#cari_stus").val();
			var posisiawal=(h-1)*jm;
			
			$("#hal_ke_stus").html("<option value=\"1\">1</option>");
			
			var obj={"idbox":"box_loader_stus"};
			var boxloader=mload.init(obj);
			boxloader.load();
			
			var ik_data = {ajax: 1,mode: "plg",
				cl: "pilihstatus",
				ikdata: {0:"getdatastatus",
					idkategori:idkategori,
					module:module,
					mode:boxstatus.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_stus").html(htm);
						}
						
						$("#hal_ke_stus").val(h);
						var tplpilih="btpilihtunggal";
						if (boxstatus.modepilih=="multi"){
							tplpilih="btpilihmulti";
						}
						var datatb={
							box:"grid_data_stus",
							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":"kategori","judul":"Kategori","tipe":"data","lebar":"130px","align":"left"},
									{"col":"nama","judul":"Nama Status","tipe":"tpl","tpl":"btdetaildata"},
									{"align":"center","judul":"","tipe":"tpl","tpl":tplpilih,"lebar":"60px"},
									
								],
							mod:"mod",
							module:"pilihstatus"
						};
						tabel.multisub(datatb);
						
					}else{
						$("#grid_data_stus").html("Tidak ada data!!")
					}
				}
			});
		},
		pilihstatus:function(a,b,c){
			boxstatus.obj.pilih(tabel.gettabel(b).getdatabaris(c));
			pilihstatus.bataldatastus();
		},
		pilihstatusmulti:function(a,b,c){
			var idtb=b+"_"+c;
            var attst=document.getElementById("btpilihmulti_"+idtb).getAttribute("stpilih");
            
            var st=0;
            if(attst==0){
                st=1;
                boxstatus.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,boxstatus.arrpilih,"id");
                if(index > -1)  {
                    boxstatus.arrpilih.splice(index, 1);
                }
                
                document.getElementById("btpilihmulti_"+idtb).className="bt_tidak";
                document.getElementById("btpilihmulti_"+idtb).setAttribute("stpilih",0);
            }
            
		},
		bataldatastus:function(){
			if(boxstatus!=null) boxstatus.tutup();
			boxstatus=null;
		},
		prosesstatus:function(){
			$("#box_loader_stus").html("");
			if(boxstatus.arrpilih.length<=0){
				$("#box_loader_stus").html("Belum ada status yang dipilih!");
				return;
			}
			var datastatus=boxstatus.arrpilih;
			
			boxstatus.obj.pilih(datastatus);
			pilihstatus.bataldatastus();
		}
	};
})();
