var masterplugincetak=(function(){
	var hal=0;
	var jumlah=30;
	var idmasterplugincetak=0;
	var boxcaritemplate=null;
	return{
		caricanggih:function(){
			mload.sh();
			var ik_data = {ajax: 1,mode: "mod",cl: "masterplugincetak",
				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){
				masterplugincetak.loade();
			}
		},
		loade:function(){
			$("#hal_ke").val(0);
			masterplugincetak.load();
		},
		load:function(){
			var nohal=$("#hal_ke").val()
			jumlah=$("#jumlah_hal").val();
			hal=(nohal-1)*jumlah;
			var cr=$("#cari").val();
			
			$("#grid_masterplugincetak").html("");
			mload.sh();
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "masterplugincetak",
				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_masterplugincetak",
							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":"id","judul":"ID","tipe":"data","lebar":"60px"},
									{"col":"module","judul":"Module","tipe":"data","lebar":"160px"},
									{"col":"nama","judul":"Nama Plugin","tipe":"data"},
									{"align":"center","judul":"Template","tipe":"tpl","tpl":"btjumlahtpl","lebar":"70px"},
									{"align":"center","judul":"Status","tipe":"tpl","tpl":"btstatus","lebar":"60px"},
									
								],
							mod:"mod",
							module:"masterplugincetak"
						};
						tabel.multisub(datatb);
					}
				}
			});
		},
		bukatpl:function(a,b,c,d){
			var idtb=b+"_"+c;
            
            var htm="<div align='left' style='padding-top:10px;padding-bottom:10px;width:100%;'>";
            htm+="<div style='margin-top:10px;margin-bottom:10px;'><b>DAFTAR TEMPLATE</b></div>";
            htm+="<div id='grid_tpl_"+idtb+"'></div>";
            
            htm+="<div style='margin-top:10px;margin-bottom:10px;'>";
            htm+="<div align='left'>";
            htm+="<span class='btmerah medium' id='bt_hide_"+idtb+"'>TUTUP</span>&nbsp;&nbsp;";
            htm+="<span class='bthijau medium' id='bt_input_"+idtb+"'>INPUT TEMPLATE</span>&nbsp;&nbsp;";
            htm+="</div>";
            htm+="</div>";
            
            htm+="</div>";
            var obj=new Object;
            obj.html=htm;
            
            
            if(tabel.gettabel(b)!=null){
                
                tabel.gettabel(b).maxim(obj,c);
                var trparr=d.offsetParent.parentNode;
                var tdarr=trparr.childNodes;
                for(var n=0;n<tdarr.length;n++){
                    tdarr[n].removeAttribute("class");
                    tdarr[n].setAttribute("class","garis_kiri_td");
                }
                d.offsetParent.setAttribute("class","garis_kiri_td bghijau");
            };
            
            document.getElementById("bt_hide_"+idtb).onclick=function(){
                tabel.gettabel(b).minim(c);
                var trparr=d.offsetParent.parentNode;
                var tdarr=trparr.childNodes;
                for(var n=0;n<tdarr.length;n++){
                    tdarr[n].removeAttribute("class");
                    tdarr[n].setAttribute("class","garis_kiri_td");
                }
            };
            document.getElementById("bt_input_"+idtb).onclick=function(){
                var ps={"box":"box_cari_template",
						"judul":"Cari Template ","tinggimax":"fit","tinggi":"auto","lebar":"600px"};
				boxcaritemplate=boxwin.load(ps);
				boxcaritemplate.idplugin=a;
				boxcaritemplate.tabel=b;
				boxcaritemplate.baris=c;
				boxcaritemplate.el=d;
				boxcaritemplate.arrpilih=[];
				boxcaritemplate.buka();
				
				masterplugincetak.loadetemplate();
            };
            mload.sh();
            var ik_data = {ajax: 1,mode: "mod",cl: "masterplugincetak",
                ikdata: {0:"getdatatemplate",
                	idplugin:a}
            };
            
            $.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.length>0){
                       var datatb={
							box:"grid_tpl_"+idtb,
							data:e,
							lebarmin:"500px",
							tbclass:'table_grid-1',
							trklik:{"data":"id","fungsi":"barisklik"},
							title:"kategori Menu",
							isi:[
								{"col":"","judul":"No","tipe":"urut","align":"center","lebar":"50px","halaman":0},
								{"col":"id","judul":"ID Template","tipe":"data","lebar":"60px"},
								{"col":"nama","judul":"Nama Template","tipe":"data"},
								{"align":"center","judul":"Hapus","tipe":"tpl","tpl":"bthapustpl","lebar":"40px"},	
							],
							mod:"mod",
							module:"masterplugincetak"
						};
						tabel.multisub(datatb);
                    }else{
                    	$("#grid_tpl_"+idtb).html("TIDAK ADA DATA TEMPLATE");
                    }
                }
            });
		},
		loadetemplate:function(){
			$("#hal_ke_template").val(0);
			masterplugincetak.loaddatatemplate($("#hal_ke_template").val(),$("#jumlah_hal_template").val());
		},
		loadtemplate:function(){
			masterplugincetak.loaddatatemplate($("#hal_ke_template").val(),$("#jumlah_hal_template").val());
		},
		entertemplate:function(e){
			if(e.which == 13 || e.keyCode == 13){
				masterplugincetak.loadetemplate();
			}
		},
		loaddatatemplate:function(h,jm){
			
			$("#grid_data_template").html("");
			
			var cr=$("#cari_template").val();
			var posisiawal=(h-1)*jumlah;
			
			$("#hal_ke_template").html("<option value=\"1\">1</option>");
			
			mload.sh("loader_template");
			var ik_data = {ajax: 1,mode: "mod",cl: "masterplugincetak",
				ikdata: {0:"getdatatemplateplugin",halaman:h,jumlah:jm,cari:cr,
					idplugin:boxcaritemplate.idplugin}
			};
			
			$.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")){
						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_template").html(htm);
							
						}
						$("#hal_ke_template").val(h);
						
						
						if((e['data']).length>0){
							var datatb={
								box:"grid_data_template",
								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 Template","tipe":"data"},
									{"align":"center","judul":"Pilih","tipe":"tpl","tpl":"btpilihtemplate","lebar":"60px"},
								],
								mod:"mod",
								module:"masterplugincetak"
							};
							tabel.multisub(datatb);
						}else{
							$("#grid_data_template").html("Tidak ada data");
						}
						
					}
				}
			});
		},
		pilihdatatemplate:function(a,b,c,d){
			var idtb=b+"_"+c;
            var attst=document.getElementById("btpilihtemplate_"+idtb).getAttribute("stpilih");
            
            var st=0;
            if(attst==0){
                st=1;
                boxcaritemplate.arrpilih.push(a);
                
                document.getElementById("btpilihtemplate_"+idtb).className="bt_ya";
                document.getElementById("btpilihtemplate_"+idtb).setAttribute("stpilih",1);
            }else{
                var index = kudus.getindexarr(a,boxcaritemplate.arrpilih);
                if(index > -1)  {
                    boxcaritemplate.arrpilih.splice(index, 1);
                }
                
                document.getElementById("btpilihtemplate_"+idtb).className="bt_tidak";
                document.getElementById("btpilihtemplate_"+idtb).setAttribute("stpilih",0);
            }
		},
		prosespilihtemplate:function(){
			if(boxcaritemplate.length<0) return;
			
			mload.sh("loader_template");
            var ik_data = {ajax: 1,mode: "mod",cl: "masterplugincetak",
                ikdata: {0:"simpantemplatecetak",idplugin:boxcaritemplate.idplugin,
                	datatemplate:boxcaritemplate.arrpilih}
            };
            
            $.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==1){
                    	var jumlah=parseInt(tabel.gettabel(boxcaritemplate.tabel).getdata(boxcaritemplate.baris,"jumlah_template")) + boxcaritemplate.arrpilih.length;
                    	boxcaritemplate.el.innerHTML=jumlah;
                    	tabel.gettabel(boxcaritemplate.tabel).setdata(boxcaritemplate.baris,"jumlah_template",jumlah);
                    	
                    	masterplugincetak.bukatpl(boxcaritemplate.idplugin,boxcaritemplate.tabel,boxcaritemplate.baris,boxcaritemplate.el);
                        masterplugincetak.batalpilihtemplate();
                    }else if(e==2){
                        alert("Data layanan tidak ada");
                    }else{
                    	alert("Error");
                    }
                }
            });
		},
		batalpilihtemplate:function(){
			if(boxcaritemplate!=null) boxcaritemplate.tutup();
			boxcaritemplate=null;
		},
		hapustpl:function(a,b,c,d){
			if(a<=0) return;
			var ps={"pesan":"Yakin akan menghapus data template dari plugin ini ??",
					"title":"Peringatan Hapus data"};
				
			var boxyesno=boxpesan.yesno(ps);
			boxyesno.proses.onclick=function(){
				
				var tbpar=tabel.gettabel(b).parrent;
				var idplugin=tabel.gettabel(tbpar.tabel).getdata(tbpar.baris,"id");
				var iddata=tabel.gettabel(b).getdata(c,"id");
				mload.sh();
	            var ik_data = {ajax: 1,mode: "mod",cl: "masterplugincetak",
	                ikdata: {0:"hapustemplate",id:a}
	            };
	            
	            $.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==1){
	                    	boxyesno.tutup();
	                    	var jumlah=parseInt(tabel.gettabel(tbpar.tabel).getdata(tbpar.baris,"jumlah_template"))-1;
	                        
	                        var el=document.getElementById("btjumlahtpl_"+tbpar.tabel+"_"+tbpar.baris);
	                        el.innerHTML=jumlah;
	                        tabel.gettabel(tbpar.tabel).setdata(tbpar.baris,"jumlah_template",jumlah);
	                        masterplugincetak.bukatpl(idplugin,tbpar.tabel,tbpar.baris,el);
	                    }else{
	                    	alert("Error");
	                    }
	                }
	            });
			}
		},
		
		setstatus:function(a,b,c,d){
			var st=1;
			if(tabel.gettabel(b).getdata(c,"status")==1){
				st=0;
			}
			
			mload.sh();
			var ik_data = {ajax: 1,mode: "mod",cl: "masterplugincetak",
				ikdata: {0:"setStatus",status:st,id:a}
			};
			
			$.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==1){
						if(st==1){
							d.className="mini_ok";
						}else{
							d.className="mini_dis";
						}
						tabel.gettabel(b).setdata(c,"status",st)
					}
				}
			});
		},
		
	};
})();