var epengajuancuti=(function(){
	var hal=0;
	var jumlah=30;
	var idepengajuancuti=0;
	var idpegawai=0;
	var idsurat=0;
	var masatahun=0;
	return{
		caricanggih:function(){
			var loadercari=mload.loader({"keterangan":"Proses ...."});
			var ik_data = {ajax: 1,mode: "mod",cl: "epengajuancuti",
				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){
				epengajuancuti.loade();
			}
		},
		loade:function(){
			$("#hal_ke").val(0);
			epengajuancuti.load();
		},
		load:function(){
			var nohal=$("#hal_ke").val()
			jumlah=$("#jumlah_hal").val();
			hal=(nohal-1)*jumlah;
			var cr=$("#cari").val();
			
			$("#grid_epengajuancuti").html("");
			var loaderdata=mload.loading({"el":"loader_dataepengajuancuti"});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "epengajuancuti",
				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_epengajuancuti",
							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":"nama","judul":"Detail Pegawai","tipe":"tpl","tpl":"btdetailpegawai"},
									{"col":"nomorsurat","judul":"Nomor Surat","tipe":"data","lebar":"160px"},
									{"col":"jeniscuti","judul":"Jenis Cuti","tipe":"data","lebar":"100px"},
									{"align":"center","judul":"Lama Cuti","tipe":"tpl","tpl":"btlamacuti","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:"epengajuancuti"
						};
						tabel.multisub(datatb);
					}else{
						loaderdata.pesan("BELUM ADA DATA");
					}
				}
			});
		},
		buat:function(a){
			document.location=lokal+"member/mod/epengajuancuti?id="+a+"&scrmenu="+modmenu.getmenuscr();
		},
		kembali:function(){
			document.location=lokal+"member/mod/epengajuancuti?scrmenu="+modmenu.getmenuscr();
		},
		edit:function(a){
			idepengajuancuti=a['id'];
			idsurat=a['idsurat'];
			$("#sl_jeniscuti").val(a['idjeniscuti']);
			$("#sl_kodewaktu").val(a['idkodewaktu']);
			
			$("#tanggalmulai").datepicker({
                inline: true,
                dateFormat: 'dd/mm/yy'
            });
            
            $("#tanggalselesai").datepicker({
                inline: true,
                dateFormat: 'dd/mm/yy'
            });
            
			idpegawai=a['idpegawai'];
			if(idpegawai>0 && idepengajuancuti<=0){
				epengajuancuti.getmasakerja(idpegawai);
			}
			
			$("#btcetaksurat").hide();
			if(a['statusdisposisi']==1 && a['status']==28){
				$("#btcetaksurat").show();
				document.getElementById("btcetaksurat").onclick=function(){
					var namapegawai=$("#nama").val();
					var jenissurat=a['jenissurat'];
					var namalib=a['lib'];
					
					var judulsurat="Cetak "+jenissurat;
					if(namapegawai!="") judulsurat+=" "+namapegawai; 
					var obj={
						"judul":judulsurat,
						"lib":namalib,
						"data":{"idsurat":idsurat}
					};
					cetak.cetaksample(obj);
				};
			}
			epengajuancuti.getcatatansurat(idsurat);
		},
		getcatatansurat:function(a){
			$("#grid_catatan").html("");
			var loaderdatacatatan=mload.loading({"el":"grid_catatan"});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "epengajuancuti",
				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:[
									{"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:"epengajuancuti"
						};
						tabel.multisub(tabeldata);
						
					}else{
						loaderdatacatatan.pesan("Tidak ada catatan");
					}
				}
			});
		},
		pilihpegawai:function(){
			var obj=new Object;
			obj['judul']="Pilih Data Pegawai";
			obj['modepilih']="tunggal";
			obj.pilih=function(a){
				var datapegawai=a;
				var cari=false;
				$("#nama").val(datapegawai['nama']);
				$("#nip").val(datapegawai['nip']);
				$("#jeniskelamin").val(datapegawai['jeniskelamin']);
				$("#jabatan").val(datapegawai['jabatanstruk']);
				$("#golongan").val(datapegawai['golongan']);
				idpegawai=datapegawai['id'];
				
				epengajuancuti.getmasakerja(idpegawai);
			};
			
			pilihpegawai.bukapegw(obj);
		},
		getmasakerja:function(a){
			$("#masakerja").hide();
			var loadermasakerja=mload.loading({"el":"loader_masakerja"});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "epengajuancuti",
				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").val(hasil);
						masatahun=parseInt(e['tahun']);
					}else{
						loadermasakerja.pesan("Terjadi kesalahan");
					}
				}
			});
		},
		simpan:function(a){
			var submitdata=a || 0;
			
			$("#loader_epengajuancuti").html("");
			var idjeniscuti=$("#sl_jeniscuti").val();
			var keperluan=$("#keperluan").val();
			var waktu=$("#waktu").val();
			var kodewaktu=$("#sl_kodewaktu").val();
			var alamatcuti=$("#alamatcuti").val();
			var telpcuti=$("#telpcuti").val();
			var tanggalmulai=$("#tanggalmulai").val();
			var tanggalselesai=$("#tanggalselesai").val();
			
			var err="";
			if(keperluan=="") err+="Keperluan cuti belum diisi.</br>";
			if(waktu=="" || parseInt(waktu)<=0) err+="Waktu lamanya cuti belum diisi.</br>";
			if(tanggalmulai=="") err+="Tanggal mulai cuti  cuti belum diisi.</br>";
			if(tanggalselesai=="") err+="Tanggal selesai cuti belum diisi.</br>";
			if(alamatcuti=="") err+="Alamat waktu cuti belum diisi.</br>";
			if(telpcuti=="") err+="No telephone waktu cuti belum diisi.</br>";
			if(idpegawai<=0) err+="Pegawai yang mengajukan waktu cuti belum diisi.</br>";
			
			if(err!=""){
				$("#loader_epengajuancuti").html(err);
				return;
			}
			var fg="inputepengajuancuti";
			if(idepengajuancuti>0){
				fg="editepengajuancuti";
			}
			
			var loadersimpan=mload.loader({"keterangan":"Proses simpan data"});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "epengajuancuti",
				ikdata: {0:fg,idpegawai:idpegawai,
					idjeniscuti:idjeniscuti,
					keperluan:keperluan,
					waktu:waktu,
					tanggalmulai:tanggalmulai,
					tanggalselesai:tanggalselesai,
					kodewaktu:kodewaktu,
					alamatcuti:alamatcuti,
					telpcuti:telpcuti,
					submitdata:submitdata,
					id:idepengajuancuti}
			};
			
			$.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){
							epengajuancuti.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: "epengajuancuti",
						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"}]);
									epengajuancuti.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=\"epengajuancuti.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=\"epengajuancuti.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");
                }
            };
            
            epengajuancuti.loaddatactt(b,c);
		},
		loaddatactte:function(b,c){
			var idtb=b+"_"+c;
			$("#hal_ke_"+idtb).val(0);
			epengajuancuti.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: "epengajuancuti",
				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:"epengajuancuti"
							};
							tabel.multisub(tabeldata);
						}else{
							loaderdatasub.pesan("TIDAK ADA DATA");
						}
					}
				}
			});
		},
		initcetak:function(a,b,c,d){
			var status=tabel.gettabel(b).getdata(c,"status");
			var statusdisposisi=tabel.gettabel(b).getdata(c,"statusdisposisi");
			if(status!=28 || statusdisposisi!=1) return;
			var jenissurat=tabel.gettabel(b).getdata(c,"jenissurat");
			var namapegawai=tabel.gettabel(b).getdata(c,"namapegawai");
			var lib=tabel.gettabel(b).getdata(c,"lib");
			var idsurat=tabel.gettabel(b).getdata(c,"idsurat");
			
			var datajs={
				"jenissurat":jenissurat,
				"namapegawai":namapegawai,
				"lib":lib,
				"idsurat":idsurat
			};
			
			epengajuancuti.cetaksurat(datajs);
		},
		cetaksurat:function(a){
			var jenissurat=a['jenissurat'];
			var namapegawai=a['namapegawai'];
			var namalib=a['lib'];
			
			var judulsurat="Cetak "+jenissurat;
			if(namapegawai!="") judulsurat+=" "+namapegawai; 
			var obj={
				"judul":judulsurat,
				"lib":namalib,
				"data":{"idsurat":a['idsurat']}
			};
			cetak.cetaksample(obj);
		}
	};
})();