var monitoringsurat=(function(){
	var hal=0;
	var jumlah=30;
	var idmonitoringsurat=0;
	var idjenissurat=0;
	var idkategori=0;
	var namalib="";
	var statusdisposisi=0;
	var idpegawai=0;
	var idstatus=0;
	var jumlahreq=0;
	var masatahun=0;
	var inout=0;
	var idtemplate=0;
	return{
		reqcb:function(jumlah){
			var requestcb = new reqcb({
			    jumlahReq: jumlah
			});
			return requestcb;
		},
		katch:function(){
			var idkategori=$("#sl_kategori").val();
			$("#boxjenissurat").hide();
			if(idkategori>0){
				$("#boxjenissurat").show();
				var loaderkategori=mload.loading({"el":"sl_jenissurat"});
				var ik_data = {ajax: 1,mode: "mod",cl: "monitoringsurat",
					ikdata: {0:"getjenissurat",
						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){
							loaderkategori.tutup();
							var htm="<option value='0'>Semua Jenis</option>";
							for(var i=0;i<e.length;i++){
								htm+="<option value='"+e[i]['id']+"'>"+e[i]['nama']+"</option>";
							}
							loaderkategori.pesan(htm);
							monitoringsurat.loade();
						}else{
							$("#boxjenissurat").hide();
							loaderkategori.pesan("<option value='0'>Semua Jenis</option>");
							monitoringsurat.loade();
						}
					}
				});
			}else{
				monitoringsurat.loade();
			}
		},
		
		caricanggih:function(){
			var loadercari=mload.loader({"keterangan":"Proses ...."});
			var ik_data = {ajax: 1,mode: "mod",cl: "monitoringsurat",
				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){
				monitoringsurat.loade();
			}
		},
		loade:function(){
			$("#hal_ke").val(0);
			monitoringsurat.load();
		},
		load:function(){
			var nohal=$("#hal_ke").val()
			jumlah=$("#jumlah_hal").val();
			hal=(nohal-1)*jumlah;
			var cr=$("#cari").val();
			
			var idkategori=$("#sl_kategori").val();
			var idjenis=$("#sl_jenissurat").val();
			
			$("#grid_monitoringsurat").html("");
			var loaderdata=mload.loading({"el":"loader_datamonitoringsurat"});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "monitoringsurat",
				ikdata: {0:"getTabel",1:hal,2:jumlah,3:cr,
					idkategori:idkategori,
					idjenis:idjenis}
			};
			
			$.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_monitoringsurat",
							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":"tanggal_surat","judul":"Tanggal","tipe":"data","lebar":"90px","format":"tanggal","align":"center"},
									{"col":"jenissurat","judul":"Detail Surat","tipe":"tpl","tpl":"btdetailsurat"},
									{"col":"namadisposisi","judul":"Status Disposisi","tipe":"data","lebar":"190px"},
									{"align":"center","judul":"Catatan","tipe":"tpl","tpl":"btcatatan","lebar":"70px"},
									{"align":"center","judul":"Status Surat","tipe":"tpl","tpl":"btstatus","lebar":"150px"},
									{"align":"center","judul":"","tipe":"tpl","tpl":"btedit","lebar":"70px"},
									
								],
							mod:"mod",
							module:"monitoringsurat"
						};
						tabel.multisub(datatb);
					}else{
						loaderdata.pesan("BELUM ADA DATA");
					}
				}
			});
		},
		lihat:function(a,b,c,d){
			document.location=lokal+"member/mod/monitoringsurat?id="+a+"&scrmenu="+modmenu.getmenuscr();
		},
		kembali:function(){
			document.location=lokal+"member/mod/monitoringsurat?scrmenu="+modmenu.getmenuscr();
		},
		edit:function(a){
			idmonitoringsurat=a['id'];
			idpegawai=a['id'];
			idstatus=a['idstatus'];
			statusdisposisi=a['statusdisposisi'];
			namalib=a['namalib'];
			idkategori=a['idkategori'];
			idjenissurat=a['idjenissurat'];
			inout=a['inout'];
			idtemplate=a['idtemplate'];
			
			$("#btsubmitdisposisi").hide();
			$("#bttutupdisposisi").hide();
			$("#btpreviewsurat").hide();
			if(idtemplate>0) $("#btpreviewsurat").show();
			
			var obj=new Object;
			var cbf=monitoringsurat.reqcb(3);
			
			cbf.addCallbackToQueue(true, function() {
	            monitoringsurat.getdatasurat(idmonitoringsurat);
	        });
	        
	        cbf.addCallbackToQueue(true, function() {
	            monitoringsurat.cekdisposisi(idmonitoringsurat);
	        });
	        
			cbf.addCallbackToQueue(true, function() {
	            monitoringsurat.getcatatansurat(idmonitoringsurat);
	        });
		},
		getdatasurat:function(a){
			$("#boxdatasurat").html("");
			$("#boxdatasurat").show();
			var fg="";
			if(idkategori==1){
				var fg="getdatasuratcuti";
				if(idjenissurat==11){
					fg="getdatasurattugas";
				}
			}else if(idkategori==2){
				if(inout==1) {
					fg="getdatasuratmasuk";
					if(idjenissurat==20){
						fg="getdatasuratpkl";
					}
				}else if(inout==2){
					fg="getdatasuratkeluar";
				}
			}
			
			var loadercekdatasurat=mload.loading({"el":"boxdatasurat"});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "monitoringsurat",
				ikdata: {0:fg,idsurat:a}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				dataType: "json",
				async:true,
				data: ik_data,
				success: function(e){
					if(e.hasOwnProperty("status")){
						loadercekdatasurat.tutup();
						if(e['status']==1) {
							loadercekdatasurat.tutup();
							$("#boxdatasurat").html(e['data']);
						}else{
							loadercekdatasurat.pesan(e['pesan']);
						}
					}else{
						loadercekdatasurat.pesan("Terjadi kesalahan!");
					}
				}
			});
		},
		cekdisposisi:function(a){
			$("#btsubmitdisposisi").hide();
			var loadercekdisposisi=mload.loading({"el":"loader_monitoringsurat"});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "monitoringsurat",
				ikdata: {0:"cekstatusdisposisi",idsurat:a}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				dataType: "json",
				async:true,
				data: ik_data,
				success: function(e){
					if(e.hasOwnProperty("status")){
						loadercekdisposisi.tutup();
					}
				}
			});
		},
		getcatatansurat:function(a){
			$("#grid_catatan").html("");
			var loaderdatacatatan=mload.loading({"el":"grid_catatan"});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "monitoringsurat",
				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 datactt=e;
						var tabeldata={
							box:"grid_catatan",
							data:datactt,
							lebarmin:"500px",
							trklik:{"data":"id","fungsi":"barisklik"},
							title:"kategori Menu",
							tbclass:"table_grid",
							isi:[
									{"align":"center","judul":"Tanggal","tipe":"data","col":"tanggal","lebar":"140px","format":"tanggal"},
									{"judul":"Nama Pegawai","tipe":"data","col":"namapegawai","lebar":"250px"},
									{"col":"catatan","judul":"Catatan","tipe":"data"},
								],
							mod:"mod",
							module:"monitoringsurat"
						};
						tabel.multisub(tabeldata);
						
					}else{
						loaderdatacatatan.pesan("Tidak ada catatan");
					}
				}
			});
		},
		getmasakerja:function(a){
			$("#masakerja").hide();
			var loadermasakerja=mload.loading({"el":"loader_masakerja"});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "monitoringsurat",
				ikdata: {0:"getmasakerja",idpegawai:a}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				dataType: "json",
				async:true,
				data: ik_data,
				success: function(e){
					if(e.hasOwnProperty("tahun")){
						loadermasakerja.tutup();
						$("#masakerja").show();
						var hasil="";
						if(e['tahun']>0) hasil+=e['tahun']+ " Tahun ";
						if(e['bulan']>0) hasil+=e['bulan']+ " Bulan ";
						if(e['hari']>0) hasil+=e['hari']+ " Hari ";
						
						$("#masakerja").html(hasil);
						masatahun=parseInt(e['tahun']);
					}else{
						loadermasakerja.pesan("Terjadi kesalahan");
					}
				}
			});
		},
		
		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=\"monitoringsurat.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=\"monitoringsurat.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");
                }
            };
            
            monitoringsurat.loaddatactt(b,c);
		},
		loaddatactte:function(b,c){
			var idtb=b+"_"+c;
			$("#hal_ke_"+idtb).val(0);
			monitoringsurat.loaddatactt(b,c);
		},
		loaddatactt:function(b,c){
			var idtb=b+"_"+c;
			var idsurat=tabel.gettabel(b).getdata(c,"id");
			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: "monitoringsurat",
				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:"monitoringsurat"
							};
							tabel.multisub(tabeldata);
						}else{
							loaderdatasub.pesan("TIDAK ADA DATA");
						}
					}
				}
			});
		},
		lihatdokumen:function(a){
			var iddok=a;
			if(iddok<=0) return;
			var myWindow = window.open(lokal+"service.php?pos=monitoringsurat&mode=mod&fl=download&id="+iddok, "", "left=100,top=50,width=800, height=600");
		},
		cetaksurat:function(){
			var jenissurat=$("#frmjenissurat").html();
			//var namapegawai=$("#frmjenissurat").html();
			var judulsurat="Cetak "+jenissurat;
			//if(namapegawai!="") judulsurat+=" "+namapegawai; 
			var obj={
				"judul":judulsurat,
				"lib":namalib,
				"data":{"idsurat":idmonitoringsurat,"prev":1}
			};
			cetak.cetaksample(obj);
		}
	};
})();