var mastertemplatecetak=(function(){
	var hal=0;
	var jumlah=30;
	var idmastertemplatecetak=0;
	return{
		caricanggih:function(){
			var loadercari=mload.loader({"keterangan":"Proses data.."});
			var ik_data = {ajax: 1,mode: "mod",cl: "mastertemplatecetak",
				ikdata: {0:"datacari01",1:1}
			};
			
			$.ajax({url: lokal+"man.ajax.php",type: "POST",dataType: "json",async:true,data: ik_data,
				success: function(e){
					if(e!=false){
						loadercari.tutup();
						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);
					}else{
						loadercari.error([{"pesan":"Tidak ada data"}])
					}
				}
			});
		},
		enter:function(e){
			if(e.which == 13 || e.keyCode == 13){
				mastertemplatecetak.loade();
			}
		},
		loade:function(){
			$("#hal_ke").val(0);
			mastertemplatecetak.load();
		},
		load:function(){
			var nohal=$("#hal_ke").val()
			jumlah=$("#jumlah_hal").val();
			hal=(nohal-1)*jumlah;
			var cr=$("#cari").val();
			var idkategori=$("#sl_kategori_template").val();
			
			$("#grid_mastertemplatecetak").html("");
			var loaderdata=mload.loading({"el":"grid_mastertemplatecetak"});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "mastertemplatecetak",
				ikdata: {0:"getTabel",1:hal,2:jumlah,3:cr,idkategori:idkategori}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				dataType: "json",
				async:true,
				data: ik_data,
				success: function(e){
					
					if(e!=false && e.length>0){
						loaderdata.tutup();
						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_mastertemplatecetak",
							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":"40px"},
									{"col":"","judul":"Detail Template","tipe":"tpl","tpl":"btdetailtpl"},
									{"align":"center","judul":"Status","tipe":"tpl","tpl":"btstatus","lebar":"60px"},
									{"align":"center","judul":"","tipe":"tpl","tpl":"btedit","lebar":"60px"},
									
								],
							mod:"mod",
							module:"mastertemplatecetak"
						};
						tabel.multisub(datatb);
					}else{
						loaderdata.pesan("TIDAK ADA DATA");
					}
				}
			});
		},
		buat:function(a){
			document.location=lokal+"member/mod/mastertemplatecetak?id="+a+"&scrmenu="+modmenu.getmenuscr();
		},
		kembali:function(){
			document.location=lokal+"member/mod/mastertemplatecetak?scrmenu="+modmenu.getmenuscr();
		},
		edit:function(a){
			idmastertemplatecetak=a['id'];
			$("#sl_kategori").val(a['idkategori']);
			
		},
		simpan:function(){
			$("#loader_mastertemplatecetak").html("");
			var nm=$("#nama").val();
			var kode=$("#kode").val();
			var template=$("#template").val();
			var idkategori=$("#sl_kategori").val();
			
			var err="";
			if(nm=="") err+="Nama Template belum diisi.</br>";
			if(kode=="") err+="Kode template belum diisi.</br>";
			if(template=="") err+="Template belum diisi.</br>";
			
			if(err!=""){
				$("#loader_mastertemplatecetak").html(err);
				return;
			}
			var fg="inputmastertemplatecetak";
			if(idmastertemplatecetak>0){
				fg="editmastertemplatecetak";
			}
			var loadersimpan=mload.loading({"el":"loader_mastertemplatecetak"});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "mastertemplatecetak",
				ikdata: {0:fg,nama:nm,
					kode:kode,
					template:template,
					idkategori:idkategori,
					id:idmastertemplatecetak}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				dataType: "json",
				async:true,
				data: ik_data,
				success: function(e){
					
					if(e==1){
						loadersimpan.tutup();
						alert("simpan template berhasil!");
					}else{
						loadersimpan.error([{"pesan":"Terjadi kesalahan data"}]);
					}
				}
			});
		},
		hapus:function(a){
			if(a<=0) return;
			var boxyesno=null;
			var databox={
				judul:"Yakin akan menghapus data template ini ?? ",
				proses:function(){
					var loaderhapus=mload.loader({"keterangan":"Proses hapus data!"});
					boxyesno.tutup();
					var ik_data = {
						ajax: 1,
						mode: "mod",
						cl: "mastertemplatecetak",
						ikdata: {0:"hapusData",id:a}
					};
					
					$.ajax({
						url: lokal+"man.ajax.php",
						type: "POST",
						dataType: "json",
						async:true,
						data: ik_data,
						success: function(e){
							if(e==1){
								mastertemplatecetak.kembali();
							}else{
								loaderhapus.error([{"pesan":"Terjadi kesalahan"}]);
							}
						}
					});
				}
			};
			boxyesno=boxwin.yesno(databox);
			boxyesno.buka();
			
		},
		setstatus:function(a,b,c,d){
			var st=1;
			if(tabel.gettabel(b).getdata(c,"status")==1){
				st=0;
			}
			
			var loaderstatus=mload.loader({"keterangan":"Proses update status.."});
			var ik_data = {ajax: 1,mode: "mod",cl: "mastertemplatecetak",
				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){
					if(e==1){
						loaderstatus.tutup();
						if(st==1){
							d.className="mini_ok";
						}else{
							d.className="mini_dis";
						}
						tabel.gettabel(b).setdata(c,"status",st)
					}else{
						loaderstatus.error({"keterangan":"Terjadi kesalahan"});
					}
				}
			});
		},
		
	};
})();