var skgolongan=(function(){
	var hal=0;
	var jumlah=30;
	var idskgolongan=0;
	var idpegawai=0;
	var iddokumen=0;
	var editstatus=1;
	var disabled="";
	var jumlahreq=0;
	var datadokumen=[];
	var boxuploaddokumen=null;
	return{
		reqcb:function(jumlah){
			var requestcb = new reqcb({
			    jumlahReq: jumlah
			});
			return requestcb;
		},
		caricanggih:function(){
			mload.sh();
			var ik_data = {ajax: 1,mode: "mod",cl: "skgolongan",
				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){
				skgolongan.loade();
			}
		},
		loade:function(){
			$("#hal_ke").val(0);
			skgolongan.load();
		},
		load:function(){
			var nohal=$("#hal_ke").val()
			jumlah=$("#jumlah_hal").val();
			hal=(nohal-1)*jumlah;
			var cr=$("#cari").val();
			
			$("#grid_skgolongan").html("");
			var loaderdata=mload.loading({"el":"loader_dataskgolongan"});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "skgolongan",
				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_skgolongan",
							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_sk","judul":"Tanggal SK","tipe":"data","lebar":"100px","align":"center","format":"tanggal"},
									{"col":"nama","judul":"Detail Pegawai","tipe":"tpl","tpl":"btdetail"},
									{"col":"nomor","judul":"Nomor SK","tipe":"data","lebar":"130px","align":"center"},
									{"col":"golongan","judul":"Gol.","tipe":"data","lebar":"60px","align":"center"},
									{"align":"center","judul":"Dok. SK","tipe":"tpl","tpl":"btjumlahdokumensk","lebar":"60px"},
									{"align":"center","judul":"Status","tipe":"tpl","tpl":"btstatus","lebar":"120px"},
									{"align":"center","judul":"","tipe":"tpl","tpl":"btedit","lebar":"60px"},
									
								],
							mod:"mod",
							module:"skgolongan"
						};
						tabel.multisub(datatb);
					}else{
						loaderdata.pesan("BELUM ADA");
					}
				}
			});
		},
		buat:function(a){
			document.location=lokal+"member/mod/skgolongan?id="+a+"&scrmenu="+modmenu.getmenuscr();
		},
		kembali:function(){
			document.location=lokal+"member/mod/skgolongan?scrmenu="+modmenu.getmenuscr();
		},
		edit:function(a){
			idskgolongan=a['id'];
			idpegawai=a['idpegawai'];
			iddokumen=a['iddokumen'];
			editstatus=a['editstatus'];
			disabled="tidak";
			if(a['editstatus']==0) disabled="ya";
			
			if(editstatus==1){
				$("#sl_status").val(a['status']);
			}
			var obj=new Object;
			var cbf=skgolongan.reqcb(1);
			
			cbf.addCallbackToQueue(true, function() {
	            skgolongan.getdokumen();
	        });
		},
		getdokumen:function(){
			datadokumen=[];
			var loaderdokumen=mload.loading({"el":"grid_dokumensk"});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "skgolongan",
				ikdata: {0:"getdatadokumen",
					id:idskgolongan}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				dataType: "json",
				async:true,
				data: ik_data,
				success: function(e){
					if(e!=false && e.length>0){
						loaderdokumen.tutup();
						for(var i=0;i<e.length;i++){
							datadokumen.push(
								{
									"id":e[i]['id'],
									"nama":e[i]['nama'],
									"idskgol":e[i]['idskgol'],
									"iddokumen":e[i]['iddokumen'],
									"editdata":editstatus
								}
							);
						}
						skgolongan.tabeldokumen();
					}else{
						loaderdokumen.pesan("Tidak ada data!");
					}
				}
			});
		},
		uploaddokumen:function(){
			var ps={"box":"boxwin_uploaddokumen",
				"judul":"Upload Dokumen SK ",tinggi:"auto","lebar":"700"};
	
			boxuploaddokumen=boxwin.load(ps);
			boxuploaddokumen.iddokumen=0;
			boxuploaddokumen.buka();
			
			$("#box_uploaddokumen").html("");
			$("#loader_uploaddokumen").html("");
			$("#namadokumen").val("");
			upload.tambahupload("box_uploaddokumen",skgolongan.objupload("PILIH FILE UNTUK UPLOAD DOKUMEN SK","tidak"));
		},
		batalupload:function(){
			if(boxuploaddokumen!=null) boxuploaddokumen.tutup();
			boxuploaddokumen=null;
		},
		simpanupload:function(){
			if(boxuploaddokumen==null || boxuploaddokumen.iddokumen<=0){
				$("#loader_uploaddokumen").html("Dokumen belum diupload!");
				return;
			}
			$("#loader_uploaddokumen").html("");
			var nama=$("#namadokumen").val();
			var err="";
			if(nama=="") err+="Nama dokumen wajib diisi!<br/>";
			if(err!=""){
				$("#loader_uploaddokumen").html(err);
				return;
			}
			if(idskgolongan>0){
				var loadersimpandokumen=mload.loading({"el":"loader_uploaddokumen"});
				var ik_data = {
					ajax: 1,
					mode: "mod",
					cl: "skgolongan",
					ikdata: {0:"simpandokumen",
						nama:nama,
						iddokumen:boxuploaddokumen.iddokumen,
						id:idskgolongan}
				};
				
				$.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){
								loadersimpandokumen.tutup();
								datadokumen.push(
									{
										"id":e['data']['id'],
										"nama":nama,
										"idskgol":idskgolongan,
										"iddokumen":boxuploaddokumen.iddokumen,
										"editdata":editstatus
									}
								);
								skgolongan.tabeldokumen();
								skgolongan.batalupload();
							}else{
								loadersimpandokumen.pesan(e['pesan']);
							}
							
						}else{
							loadersimpandokumen.pesan("Terjadi kesalahan!");
						}
					}
				});
			}else{
				datadokumen.push(
					{
						"id":0,
						"nama":nama,
						"idskgol":idskgolongan,
						"iddokumen":boxuploaddokumen.iddokumen,
						"editdata":editstatus
					}
				);
				skgolongan.tabeldokumen();
				skgolongan.batalupload();
			}
			
		},
		tabeldokumen:function(){
			$("#grid_dokumensk").html("");
			if(datadokumen.length>0){
				var datatb={
					box:"grid_dokumensk",
					data:datadokumen,
					lebarmin:"500px",
					trklik:{"data":"id","fungsi":"barisklik"},
					title:"Data Dokumen",
					isi:[
							{"col":"","judul":"No","tipe":"urut","align":"center","lebar":"50px","halaman":0},
							{"col":"nama","judul":"Nama Dokumen","tipe":"data"},
							{"align":"center","judul":"Dok.","tipe":"tpl","tpl":"btdokumensk","lebar":"60px"},
							{"align":"center","judul":"","tipe":"tpl","tpl":"bteditdokumen","lebar":"60px"},
						],
					mod:"mod",
					module:"skgolongan"
				};
				tabel.multisub(datatb);
			}else{
				$("#grid_dokumensk").html("Tidak ada data Dokumen!");
			}
		},
		simpan:function(){
			$("#loader_skgolongan").html("");
			var tanggalsk=$("#tanggalsk").val();
			var nomor=$("#nosk").val();
			var idgolongan=$("#sl_golongan").val();
			var keterangan=$("#keterangan").val();
			var status=$("#sl_status").val();
			
			var err="";
			if(tanggalsk=="") err+="Tanggal SK belum diisi.</br>";
			if(nomor=="") err+="Nomor SK belum diisi.</br>";
			if(idpegawai=="" || idpegawai<=0) err+="Pegawai belum dipilih belum diisi.</br>";
			
			if(idskgolongan>0) {
				datadokumen=[];
			}else{
				if(datadokumen.length<=0) err+="Dokumen SK belum diupload.</br>";
			}
			
			if(err!=""){
				$("#loader_skgolongan").html(err);
				return;
			}
			var fg="inputskgolongan";
			if(idskgolongan>0){
				fg="editskgolongan";
			}
			
			var loadersimpan=mload.loader({"keterangan":"Proses simpan data"});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "skgolongan",
				ikdata: {0:fg,tanggalsk:tanggalsk,
					idpegawai:idpegawai,
					nomor:nomor,idgolongan:idgolongan,
					status:status,
					keterangan:keterangan,
					datadokumen:datadokumen,
					id:idskgolongan}
			};
			
			$.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){
							skgolongan.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 SK golongan pegawai ini ?? ",
				proses:function(){
					var loaderhapus=mload.loader({"keterangan":"Proses hapus data.."});
					boxyesno.tutup();
					var ik_data = {
						ajax: 1,
						mode: "mod",
						cl: "skgolongan",
						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"}]);
									skgolongan.load();
								}else{
									loaderhapus.error(e['pesan']);
								}
								
							}else{
								loaderhapus.error([{"pesan":"Gagal hapus data"}]);
							}
						}
					});
				}
			};
			boxyesno=boxwin.yesno(databox);
			boxyesno.buka();
		},
		bukadokumensk: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>DOKUMEN SK </b></div>";
            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");
                }
            };
            
            var loadersub=mload.loading({"el":"loader_sub_"+idtb});
            var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "skgolongan",
				ikdata: {0:"getdatadokumen",id: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){
						loadersub.tutup();
						var datatb={
							box:"grid_sub_"+idtb,
							data:e,
							lebarmin:"500px",
							trklik:{"data":"id","fungsi":"barisklik"},
							title:"Data Dokumen",
							isi:[
									{"col":"","judul":"No","tipe":"urut","align":"center","lebar":"50px","halaman":0},
									{"col":"nama","judul":"Nama Dokumen","tipe":"data"},
									{"align":"center","judul":"Dok.","tipe":"tpl","tpl":"btdokumensk","lebar":"60px"},
								],
							mod:"mod",
							module:"skgolongan"
						};
						tabel.multisub(datatb);
						
					}else{
						loadersub.pesan("Tidak ada dokumen");
					}
				}
			});
		},
		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']);
				idpegawai=datapegawai['id'];
			};
			
			pilihpegawai.bukapegw(obj);
		},
		hapusdokumen:function(a,b,c){
			var boxyesno=null;
			var databox={
				judul:"Yakin akan menghapus data dokumen SK golongan pegawai ini ?? ",
				proses:function(){
					boxyesno.tutup();
					if(idskgolongan>0){
						var loaderhapusdok=mload.loader({"keterangan":"Proses hapus dokumen sk"});
			            var ik_data = {
							ajax: 1,
							mode: "mod",
							cl: "skgolongan",
							ikdata: {0:"hapusdokumensk",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){
										loaderhapusdok.tutup();
										skgolongan.getdokumen();
									}else{
										loaderhapusdok.error(e['pesan']);
									}
									
								}else{
									loaderhapusdok.error([{"pesan":"Terjadi kesalahan"}]);
								}
							}
						});
					}else{
						datadokumen.splice(c,1);
						skgolongan.tabeldokumen();
					}
				}
			};
			boxyesno=boxwin.yesno(databox);
			boxyesno.buka();
			
		},
		bukagambar:function(a,b,c,d){
			var idgambar=tabel.gettabel(b).getdata(c,"idgambar");
			if(idgambar>0){
				skgolongan.lihatdokumen(idgambar);
			}
		},
		lihatdokumen:function(a){
			var iddok=a;
			if(iddok<=0) return;
			var myWindow = window.open(lokal+"service.php?pos=skgolongan&mode=mod&fl=download&id="+iddok, "", "left=100,top=50,width=800, height=600");
		},
		objupload:function(a,b){
			var nama=a || "PILIH FILE UNTUK UPLOAD DOKUMEN SK";
			var prev=b || "tidak";
			
			var obp=new Object;
			obp.proses=function(dataupload){
				boxuploaddokumen.iddokumen=dataupload.id;
				var filelama=dataupload.nama
				$("#box_uploaddokumen").html("");
				upload.tambahupload("box_uploaddokumen",skgolongan.objupload(filelama,"ya"));
			};
			
			obp.hapus=function(a){
				
			};
			obp.lihat=function(a){
				skgolongan.lihatdokumen(boxuploaddokumen.iddokumen);
			};
			
			obp.pilihfile=function(a){
				boxuploaddokumen.iddokumen=0;
			};
			
			var dtpost={"jenis":12};
			var obj={"tautan":lokal+"service.php?pos=skgolongan&mode=mod&fl=upload",
				"status":lokal+"service.php?pos=skgolongan&mode=mod&fl=progres",
				"tutup":"tidak","finish":obp,disupload:disabled,
				"data":dtpost,"namafile":nama,"preview":prev};
			return obj;
		},
	};
})();