var pilihtemplate=(function(){
	var boxtemplate=null;
	var module="";
	return{
		init:function(a){
			module=a['module'];
		},
		bukatemplt:function(a){
			var judulbox=a['judul'] || "Pilih Data Template";
			var modepilih=a['modepilih'] || "tunggal";
			
			var ps={"box":"box_template",
					"judul":judulbox,tinggi:"auto","lebar":"700px"};
		
			boxtemplate=boxwin.load(ps);
			boxtemplate.obj=a;
			boxtemplate.modepilih=modepilih;
			boxtemplate.arrpilih=[];
			boxtemplate.buka();
			$("#box_loader_templt").html("");
			
			$("#btprosesmulti").hide();
			if(modepilih=="multi") $("#btprosesmulti").show();
			pilihtemplate.loadetemplt();
		},
		caritemplt:function(e){
			if(e.which == 13 || e.keyCode == 13){
				$("#cari_templt").val($("#namatemplt").val());
				pilihtemplate.loadetemplt();
			}
		},
		loadetemplt:function(){
			$("#hal_ke_templt").val(0);
			pilihtemplate.loaddatatemplt($("#hal_ke_templt").val(),$("#jumlah_hal_templt").val());
		},
		loadtemplt:function(){
			pilihtemplate.loaddatatemplt($("#hal_ke_templt").val(),$("#jumlah_hal_templt").val());
		},
		entertemplt:function(e){
			if(e.which == 13 || e.keyCode == 13){
				pilihtemplate.loadetemplt();
			}
		},
		loaddatatemplt:function(h,jm){
			var idkategori=$("#sl_kat_templt").val();
			$("#grid_data_templt").html("");
			var cr=$("#cari_templt").val();
			var posisiawal=(h-1)*jm;
			
			$("#hal_ke_templt").html("<option value=\"1\">1</option>");
			
			var obj={"idbox":"box_loader_templt"};
			var boxloader=mload.init(obj);
			boxloader.load();
			
			var ik_data = {ajax: 1,mode: "plg",
				cl: "pilihtemplate",
				ikdata: {0:"getdatatemplate",
					idkategori:idkategori,
					module:module,
					mode:boxtemplate.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_templt").html(htm);
						}
						
						$("#hal_ke_templt").val(h);
						var tplpilih="btpilihtunggal";
						if (boxtemplate.modepilih=="multi"){
							tplpilih="btpilihmulti";
						}
						var datatb={
							box:"grid_data_templt",
							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 Template","tipe":"tpl","tpl":"btdetailpegawai"},
									{"align":"center","judul":"","tipe":"tpl","tpl":tplpilih,"lebar":"60px"},
									
								],
							mod:"mod",
							module:"pilihtemplate"
						};
						tabel.multisub(datatb);
						
					}else{
						$("#grid_data_templt").html("Tidak ada data!!")
					}
				}
			});
		},
		pilihtemplate:function(a,b,c){
			boxtemplate.obj.pilih(tabel.gettabel(b).getdatabaris(c));
			pilihtemplate.bataldatatemplt();
		},
		pilihtemplatemulti:function(a,b,c){
			var idtb=b+"_"+c;
            var attst=document.getElementById("btpilihmulti_"+idtb).getAttribute("stpilih");
            
            var st=0;
            if(attst==0){
                st=1;
                boxtemplate.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,boxtemplate.arrpilih,"id");
                if(index > -1)  {
                    boxtemplate.arrpilih.splice(index, 1);
                }
                
                document.getElementById("btpilihmulti_"+idtb).className="bt_tidak";
                document.getElementById("btpilihmulti_"+idtb).setAttribute("stpilih",0);
            }
            
		},
		bataldatatemplt:function(){
			if(boxtemplate!=null) boxtemplate.tutup();
			boxtemplate=null;
		},
		prosespegawai:function(){
			$("#box_loader_templt").html("");
			if(boxtemplate.arrpilih.length<=0){
				$("#box_loader_templt").html("Belum ada pegawai yang dipilih!");
				return;
			}
			var datatemplate=boxtemplate.arrpilih;
			
			boxtemplate.obj.pilih(datatemplate);
			pilihtemplate.bataldatatemplt();
		}
	};
})();
