var pilihsurat=(function(){
	var boxpilihsurat=null;
	var module="";
	return{
		init:function(a){
			module=a['module'];
		},
		bukasrt:function(a){
			var judulbox=a['judul'] || "Pilih Data Status";
			var modepilih=a['modepilih'] || "tunggal";
			
			var ps={"box":"box_pilihsurat",
					"judul":judulbox,tinggi:"auto","lebar":"700px"};
		
			boxpilihsurat=boxwin.load(ps);
			boxpilihsurat.obj=a;
			boxpilihsurat.modepilih=modepilih;
			boxpilihsurat.arrpilih=[];
			boxpilihsurat.buka();
			$("#box_loader_srt").html("");
			
			$("#btprosesmulti").hide();
			if(modepilih=="multi") $("#btprosesmulti").show();
			pilihsurat.loadesrt();
		},
		carisrt:function(e){
			if(e.which == 13 || e.keyCode == 13){
				$("#cari_srt").val($("#namasrt").val());
				pilihsurat.loadesrt();
			}
		},
		loadesrt:function(){
			$("#hal_ke_srt").val(0);
			pilihsurat.loaddatasrt($("#hal_ke_srt").val(),$("#jumlah_hal_srt").val());
		},
		loadsrt:function(){
			pilihsurat.loaddatasrt($("#hal_ke_srt").val(),$("#jumlah_hal_srt").val());
		},
		entersrt:function(e){
			if(e.which == 13 || e.keyCode == 13){
				pilihsurat.loadesrt();
			}
		},
		loaddatasrt:function(h,jm){
			var idjenissurat=$("#sl_jenis_srt").val();
			$("#grid_data_srt").html("");
			var cr=$("#cari_srt").val();
			var posisiawal=(h-1)*jm;
			
			$("#hal_ke_srt").html("<option value=\"1\">1</option>");
			
			var obj={"idbox":"box_loader_srt"};
			var boxloader=mload.init(obj);
			boxloader.load();
			
			var ik_data = {ajax: 1,mode: "plg",
				cl: "pilihsurat",
				ikdata: {0:"getdatastatus",
					idjenissurat:idjenissurat,
					module:module,
					mode:boxpilihsurat.modepilih,
					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_srt").html(htm);
						}
						
						$("#hal_ke_srt").val(h);
						var tplpilih="btpilihtunggalsrt";
						if (boxpilihsurat.modepilih=="multi"){
							tplpilih="btpilihmultisrt";
						}
						var datatb={
							box:"grid_data_srt",
							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":"kategorisurat","judul":"Kategori","tipe":"data","lebar":"130px","align":"left"},
									{"col":"jenissurat","judul":"Jenis Surat","tipe":"data","lebar":"130px","align":"left"},
									{"col":"nomor_panjang","judul":"Nomor Surat","tipe":"data","lebar":"110px"},
									{"col":"keterangan","judul":"Keterangan","tipe":"data"},
									{"align":"center","judul":"","tipe":"tpl","tpl":tplpilih,"lebar":"60px"},
									
								],
							mod:"mod",
							module:"pilihsurat"
						};
						tabel.multisub(datatb);
						
					}else{
						$("#grid_data_srt").html("Tidak ada data!!")
					}
				}
			});
		},
		pilihsuratbl:function(a,b,c){
			boxpilihsurat.obj.pilih(tabel.gettabel(b).getdatabaris(c));
			pilihsurat.bataldatasrt();
		},
		pilihsuratmulti:function(a,b,c){
			var idtb=b+"_"+c;
            var attst=document.getElementById("btpilihmulti_"+idtb).getAttribute("stpilih");
            
            var st=0;
            if(attst==0){
                st=1;
                boxpilihsurat.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,boxpilihsurat.arrpilih,"id");
                if(index > -1)  {
                    boxpilihsurat.arrpilih.splice(index, 1);
                }
                
                document.getElementById("btpilihmulti_"+idtb).className="bt_tidak";
                document.getElementById("btpilihmulti_"+idtb).setAttribute("stpilih",0);
            }
            
		},
		bataldatasrt:function(){
			if(boxpilihsurat!=null) boxpilihsurat.tutup();
			boxpilihsurat=null;
		},
		prosesstatus:function(){
			$("#box_loader_srt").html("");
			if(boxpilihsurat.arrpilih.length<=0){
				$("#box_loader_srt").html("Belum ada status yang dipilih!");
				return;
			}
			var datastatus=boxpilihsurat.arrpilih;
			
			boxpilihsurat.obj.pilih(datastatus);
			pilihsurat.bataldatasrt();
		}
	};
})();
