var suratbalasan=(function(){
	var hal=0;
	var jumlah=30;
	var idsuratbalasan=0;
	var idsurat=0;
	var datapegawai=[];
	var datamenimbang=[];
	var datadasar=[];
	var datacatatantugas=[];
	
	var editstatus=0;
	var statusdisposisi=0;
	var jumlahreq=0;
	return{
		reqcb:function(jumlah){
			var requestcb = new reqcb({
			    jumlahReq: jumlah
			});
			return requestcb;
		},
		caricanggih:function(){
			var loadercari=mload.loader({"keterangan":"Proses ...."});
			var ik_data = {ajax: 1,mode: "mod",cl: "suratbalasan",
				ikdata: {0:"datacari01",1:1}
			};
			
			$.ajax({url: lokal+"man.ajax.php",type: "POST",dataType: "json",async:true,data: ik_data,
				success: function(e){
					loadercari.tutup();
					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){
				suratbalasan.loade();
			}
		},
		loade:function(){
			$("#hal_ke").val(0);
			suratbalasan.load();
		},
		load:function(){
			var nohal=$("#hal_ke").val()
			jumlah=$("#jumlah_hal").val();
			hal=(nohal-1)*jumlah;
			var cr=$("#cari").val();
			
			$("#grid_suratbalasan").html("");
			var loaderdata=mload.loading({"el":"loader_datasuratbalasan"});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "suratbalasan",
				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){
					
					loaderdata.tutup();
					//alert(JSON.stringify(e));
					if(e!=false && e.length>0){
						
						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_suratbalasan",
							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":"jeniscuti","judul":"Jenis Cuti","tipe":"data","lebar":"100px"},
									//{"judul":"Hal Surat","tipe":"data","col":"halsurat","lebar":"80px"},
									{"judul":"Kepadat","tipe":"data","col":"kepada","lebar":"80px"},
									{"align":"center","judul":"Catatan","tipe":"tpl","tpl":"btcatatan","lebar":"60px"},
									{"align":"center","judul":"Cetak","tipe":"tpl","tpl":"btcetak","lebar":"40px"},
									{"align":"center","judul":"Status","tipe":"tpl","tpl":"btstatus","lebar":"210px"},
									{"align":"center","judul":"","tipe":"tpl","tpl":"btedit","lebar":"60px"},
									
								],
							mod:"mod",
							module:"suratbalasan"
						};
						tabel.multisub(datatb);
					}else{
						loaderdata.pesan("BELUM ADA DATA");
					}
				}
			});
		},
		buat:function(a){
			document.location=lokal+"member/mod/suratbalasan?id="+a+"&scrmenu="+modmenu.getmenuscr();
		},
		kembali:function(){
			document.location=lokal+"member/mod/suratbalasan?scrmenu="+modmenu.getmenuscr();
		},
		edit:function(a){
			idsuratbalasan=a['id'];
			idsurat=a['idsurat'];
			editstatus=a['editstatus'];
			statusdisposisi=a['statusdisposisi'];
			
            
            var obj=new Object;
			var cbf=suratbalasan.reqcb(5);
			
			cbf.addCallbackToQueue(true, function() {
	            suratbalasan.getcatatansurat(idsurat);
	        });
	        
	        cbf.addCallbackToQueue(true, function() {
	            suratbalasan.getdatamenimbang(idsurat);
	        });
	        
	        cbf.addCallbackToQueue(true, function() {
	            suratbalasan.getdatadasar(idsurat);
	        });
	        
	        cbf.addCallbackToQueue(true, function() {
	            suratbalasan.getdatactttugas(idsurat);
	        });
		},
		getcatatansurat:function(a){
			$("#grid_catatan").html("");
			var loaderdatacatatan=mload.loading({"el":"grid_catatan"});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "suratbalasan",
				ikdata: {0:"getcatatansurat",idsurat:a}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				dataType: "json",
				async:true,
				data: ik_data,
				success: function(e){
					if(e!=false && e.length>0){
						var tabeldata={
							box:"grid_catatan",
							data:e,
							lebarmin:"500px",
							trklik:{"data":"id","fungsi":"barisklik"},
							title:"kategori Menu",
							tbclass:"table_grid",
							isi:[
									{"col":"","judul":"No","tipe":"urut","align":"center","lebar":"50px","halaman":1},
									{"judul":"Nama Pegawai","tipe":"data","col":"namapegawai","lebar":"250px"},
									{"col":"catatan","judul":"Catatan","tipe":"data"},
									
								],
							mod:"mod",
							module:"suratbalasan"
						};
						tabel.multisub(tabeldata);
						
					}else{
						loaderdatacatatan.pesan("Tidak ada catatan");
					}
				}
			});
		},
		getdatacttbalasan:function(){
			datacatatantugas=[];
			var loaderdatactt=mload.loading({"el":"grid_catatanbalasan"});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "suratbalasan",
				ikdata: {0:"getdatacatatanbalasan",
					idsuratbalasan:idsuratbalasan}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				dataType: "json",
				async:true,
				data: ik_data,
				success: function(e){
					if(e!=false && e.length>0){
						loaderdatactt.tutup();
						datacatatanbalasan=e;
						suratbalasan.tabelcttbalasan();
					}else{
						loaderdatactt.pesan("TIDAK ADA DATA");
					}
				}
			});
		},
		pilihcttbalasan:function(){
			var obj=new Object;
			obj['judul']="Pilih Data Catatan";
			obj['modepilih']="multi";
			obj.pilih=function(a){
				var arrdatacttbalasan=a;
				var cari=false;
				for(var i=0;i<arrdatacttbalasan.length;i++){
					datacatatanbalasan.push({
							"catatan":arrdatacttbalasan[i]['catatan'],
							"edit":editstatus,
							"statusdisposisi":statusdisposisi
						}
					);
				}
				
				suratbalasan.tabelcttbalasan();
			};
			
			pilihcatatan.bukacttn(obj);
		},
		hapuscttbalasan:function(a,b,c,d){
			if(a<=0) return;
			var boxyesno=null;
			var databox={
				judul:"Yakin akan menghapus data catatan balasan pada surat balasan ini ?? ",
				proses:function(){
					var loaderhapus=mload.loader({"keterangan":"Proses hapus data.."});
					boxyesno.tutup();
					datacatatanbalasan.splice(c,1);
					loaderhapus.tutup();
					suratbalasan.tabelcttbalasan();
				}
			};
			boxyesno=boxwin.yesno(databox);
			boxyesno.buka();
		},
		tabelcttbalasan:function(){
			$("#grid_catatanbalasan").html("BELUM ADA DATA");
			if(datacatatanbalasan.length>0){
				var tabeldata={
					box:"grid_catatanbalasan",
					data:datacatatanbalasan,
					lebarmin:"500px",
					trklik:{"data":"id","fungsi":"barisklik"},
					title:"kategori Menu",
					tbclass:"table_grid",
					isi:[
							{"col":"","judul":"No","tipe":"urut","align":"center","lebar":"50px","halaman":0},
							{"judul":"Catatan","tipe":"data","col":"catatan"},
							{"judul":"","tipe":"tpl","tpl":"bteditcttbalasan","align":"center","lebar":"70px"},
							
						],
					mod:"mod",
					module:"suratbalasan"
				};
				tabel.multisub(tabeldata);
			}
		},
		simpan:function(a){
			var submitdata=a || 0;
			
			$("#loader_suratbalasan").html("");
			var halsurat=$("#halsurat").val();
			var sifat=$("#sifat").val();
			var lampiran=$("#lampiran").val();
			var kepada=$("#kepada").val();
			var ditempat=$("#ditempat").val();
			var keterangan=$("#keterangan").val();
			
			var err="";
			
			if(err!=""){
				$("#loader_suratbalasan").html(err);
				return;
			}
			var fg="inputsuratbalasan";
			if(idsuratbalasan>0){
				fg="editsuratbalasan";
			}
			
			var loadersimpan=mload.loader({"keterangan":"Proses simpan data"});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "suratbalasan",
				ikdata: {0:fg,sifat:sifat,halsurat:halsurat,lampiran:lampiran,
					kepada:kepada,ditepat:ditempat,
					keterangan:keterangan,
					datacatatanbalasan:datacatatanbalasan,
					submitdata:submitdata,
					id:idsuratbalasan}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				dataType: "json",
				async:true,
				data: ik_data,
				success: function(e){
					if(e.hasOwnProperty("status")){
						if(e['status']==1){
							suratbalasan.kembali();
						}else{
							loadersimpan.error(e['pesan']);
						}
					}else{
						loadersimpan.error([{"pesan":"Gagal proses simpan data"}]);
					}
				}
			});
		},
		hapus:function(a){
			if(a<=0) return;
			var boxyesno=null;
			var databox={
				judul:"Yakin akan menghapus data jenis cuti ini ?? ",
				proses:function(){
					var loaderhapus=mload.loader({"keterangan":"Proses hapus data.."});
					boxyesno.tutup();
					var ik_data = {
						ajax: 1,
						mode: "mod",
						cl: "suratbalasan",
						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!=false && e.hasOwnProperty("status")){
								if(e['status']==1){
									loaderhapus.error([{"pesan":"Berhasil"}]);
									suratbalasan.load();
								}else{
									loaderhapus.error(e['pesan']);
								}
								
							}else{
								loaderhapus.error([{"pesan":"Gagal hapus data"}]);
							}
						}
					});
				}
			};
			boxyesno=boxwin.yesno(databox);
			boxyesno.buka();
		},
		lihatcatatan: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>CATATAN </b></div>";
            htm+="<select id=\"jumlah_hal_"+idtb+"\" class=\"slkecil\" onchange=\"suratbalasan.loaddatacttpe("+b+","+c+")\">";
            htm+="<option value=\"30\">30</option>";
            htm+="<option value=\"50\">50</option>";
            htm+="<option value=\"100\">100</option>";
            htm+="</select>";
            htm+="&nbsp;&nbsp;Hal <select id=\"hal_ke_"+idtb+"\" class=\"slkecil\" onchange=\"suratbalasan.loaddatactt("+b+","+c+")\"><option value=\"1\">1</option></select>";
            htm+="<div style='margin-top:10px;margin-bottom:10px;'>";
            htm+="<div id='loader_sub_"+idtb+"'></div>";
            htm+="<div id='grid_sub_"+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+="</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");
                }
            };
            
            suratbalasan.loaddatactt(b,c);
		},
		loaddatactte:function(b,c){
			var idtb=b+"_"+c;
			$("#hal_ke_"+idtb).val(0);
			suratbalasan.loaddatactt(b,c);
		},
		loaddatactt:function(b,c){
			var idtb=b+"_"+c;
			var idsurat=tabel.gettabel(b).getdata(c,"idsurat");
			var loaderdatasub=mload.loading({"el":"loader_sub_"+idtb});
			var hal=$("#hal_ke_"+idtb).val();
			var jumlah=$("#jumlah_hal_"+idtb).val();
			var posisiawal=(hal-1)*jumlah;
			
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "suratbalasan",
				ikdata: {0:"getdataCatatan",
					halaman:hal,jumlah:jumlah,
					idsurat:idsurat}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				dataType: "json",
				async:true,
				data: ik_data,
				success: function(e){
					if(e!=false && e.hasOwnProperty("data")){
						loaderdatasub.tutup();
						var data=e['data'];
						var jumlahhalaman=e['totalhalaman'];
						if(data.length>0){
							var htm="";
							for(var i=0;i<jumlahhalaman;i++){
								htm+="<option value=\""+(i+1)+"\">"+(i+1)+"</option>";
							}
							$("#hal_ke_"+idtb).html(htm);
							$("#hal_ke_"+idtb).val(hal);
							
							var tabeldata={
								box:"grid_sub_"+idtb,
								data:data,
								lebarmin:"500px",
								trklik:{"data":"id","fungsi":"barisklik"},
								title:"kategori Menu",
								tbclass:"table_grid-1",
								tabelpar:b,
                            	barispar:c,
								isi:[
										{"align":"center","judul":"Tanggal","tipe":"data","col":"tanggal","lebar":"140px","format":"tanggal"},
										{"judul":"Nama Pegawai","tipe":"data","col":"namapegawai","lebar":"120px"},
										{"col":"catatan","judul":"Catatan","tipe":"data"},
									],
								mod:"mod",
								module:"suratbalasan"
							};
							tabel.multisub(tabeldata);
						}else{
							loaderdatasub.pesan("TIDAK ADA DATA");
						}
					}
				}
			});
		},
		initcetak:function(a,b,c,d){
			
		},
	};
})();