var masterjabatan=(function(){
	var hal=0;
	var jumlah=30;
	var idmasterjabatan=0;
	var boxsubjabatan=null;
	return{
		caricanggih:function(){
			mload.sh();
			var ik_data = {ajax: 1,mode: "mod",cl: "masterjabatan",
				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){
				masterjabatan.loade();
			}
		},
		loade:function(){
			$("#hal_ke").val(0);
			masterjabatan.load();
		},
		load:function(){
			var nohal=$("#hal_ke").val()
			jumlah=$("#jumlah_hal").val();
			hal=(nohal-1)*jumlah;
			var cr=$("#cari").val();
			
			$("#grid_masterjabatan").html("");
			var loaderdata=mload.loading({"el":"loader_datamasterjabatan"});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "masterjabatan",
				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_masterjabatan",
							data:e,
							lebarmin:"500px",
							trklik:{"data":"id","fungsi":"barisklik"},
							title:"kategori Menu",
							isi:[
									{"col":"id","judul":"ID","tipe":"data","align":"center","lebar":"50px"},
									{"col":"nama","judul":"Nama Jabatan","tipe":"data"},
									{"align":"center","judul":"Sub","tipe":"tpl","tpl":"btjumlahsub","lebar":"60px"},
									{"align":"center","judul":"Status","tipe":"tpl","tpl":"btstatus","lebar":"60px"},
									{"align":"center","judul":"","tipe":"tpl","tpl":"btedit","lebar":"60px"},
									
								],
							mod:"mod",
							module:"masterjabatan"
						};
						tabel.multisub(datatb);
					}else{
						loaderdata.pesan("BELUM ADA JABATAN");
					}
				}
			});
		},
		buat:function(a){
			document.location=lokal+"member/mod/masterjabatan?id="+a+"&scrmenu="+modmenu.getmenuscr();
		},
		kembali:function(){
			document.location=lokal+"member/mod/masterjabatan?scrmenu="+modmenu.getmenuscr();
		},
		edit:function(a){
			idmasterjabatan=a['id'];
		},
		simpan:function(){
			$("#loader_masterjabatan").html("");
			var nm=$("#nama").val();
			var iddata=$("#iddata").val();
			
			var err="";
			if(nm=="") err+="Nama Jabatan belum diisi.</br>";
			if(parseInt(iddata)<=0) err+="ID Jabatan belum diisi.</br>";
			
			if(err!=""){
				$("#loader_masterjabatan").html(err);
				return;
			}
			var fg="inputmasterjabatan";
			if(idmasterjabatan>0){
				fg="editmasterjabatan";
			}
			
			var loadersimpan=mload.loader({"keterangan":"Proses simpan data"});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "masterjabatan",
				ikdata: {0:fg,nama:nm,
					iddata:iddata,
					id:idmasterjabatan}
			};
			
			$.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){
							masterjabatan.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 jabatan ini ?? ",
				proses:function(){
					var loaderhapus=mload.loader({"keterangan":"Proses hapus data.."});
					boxyesno.tutup();
					var ik_data = {
						ajax: 1,
						mode: "mod",
						cl: "masterjabatan",
						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.tutup();
									masterjabatan.load();
								}else{
									loaderhapus.error(e['pesan']);
								}
							}else{
								loaderhapus.error([{"pesan":"Gagal hapus data"}]);
							}
						}
					});
				}
			};
			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: "masterjabatan",
				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([{"pesan":"Gagal update status"}]);
					}
				}
			});
		},
		bukasub:function(a,b,c,d){
			var namaparr=(tabel.gettabel(b).getdata(c,"nama")).toUpperCase();
			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>SUB JABATAN \""+namaparr+"\"</b></div>";
            htm+="<select id=\"jumlah_hal_"+idtb+"\" class=\"slkecil\" onchange=\"masterjabatan.loadloadtrxe("+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=\"masterjabatan.loadtrx("+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+="<span class='bthijau medium' id='bt_input_"+idtb+"'>INPUT SUB JABATAN</span>";
            
            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");
                }
            };
            
            document.getElementById("bt_input_"+idtb).onclick=function(){
                var ps={"box":"box_subjabatan",
					"judul":"Input Sub "+namaparr,tinggi:"auto","lebar":"700"};
		
				boxsubjabatan=boxwin.load(ps);
				boxsubjabatan.id=0;
				boxsubjabatan.kode=tabel.gettabel(b).getdata(c,"kode");
				boxsubjabatan.idparrent=tabel.gettabel(b).getdata(c,"id");
				boxsubjabatan.tabel=b;
				boxsubjabatan.baris=c;
				boxsubjabatan.el=d;
				boxsubjabatan.buka();
				
				$("#loader_sub").html("");
				$("#idsub").val("");
				$("#namasub").val("");
            };
            
			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: "masterjabatan",
				ikdata: {0:"getsubjabatan",
					halaman:hal,jumlah:jumlah,
					idparrent:a}
			};
			
			$.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:[
										{"col":"id","judul":"ID","tipe":"data","align":"center","lebar":"50px"},
										{"col":"nama","judul":"Nama Jabatan","tipe":"data"},
										{"align":"center","judul":"Sub","tipe":"tpl","tpl":"btjumlahsub","lebar":"60px"},
										{"align":"center","judul":"Status","tipe":"tpl","tpl":"btstatus","lebar":"60px"},
										{"align":"center","judul":"","tipe":"tpl","tpl":"bteditsub","lebar":"60px"},
									],
								mod:"mod",
								module:"masterjabatan"
							};
							tabel.multisub(tabeldata);
						}else{
							loaderdatasub.pesan("TIDAK ADA DATA");
						}
					}
				}
			});
		},
		editsub:function(a,b,c,d){
			var tbpar=tabel.gettabel(b).parrent;
			var el=document.getElementById("btjumlahsub_"+tbpar.tabel+"_"+tbpar.baris);
			var namaparr=tabel.gettabel(tbpar.tabel).getdata(tbpar.baris,"nama");
			
			var ps={"box":"box_subjabatan",
				"judul":"Edit Sub "+namaparr,tinggi:"auto","lebar":"700"};
			
			boxsubjabatan=boxwin.load(ps);
			boxsubjabatan.id=a;
			boxsubjabatan.kode=tabel.gettabel(tbpar.tabel).getdata(tbpar.baris,"kode");
			boxsubjabatan.idparrent=tabel.gettabel(b).getdata(c,"idparrent");
			boxsubjabatan.tabel=tbpar.tabel;
			boxsubjabatan.baris=tbpar.baris;
			boxsubjabatan.el=el;
			boxsubjabatan.buka();
			
			$("#loader_sub").html("");
			$("#idsub").val(tabel.gettabel(b).getdata(c,"id"));
			$("#namasub").val(tabel.gettabel(b).getdata(c,"nama"));
		},
		batalsub:function(){
			if(boxsubjabatan!=null) boxsubjabatan.tutup();
			boxsubjabatan=null;
		},
		inputsub:function(){
			var idtb=boxsubjabatan.tabel+"_"+boxsubjabatan.baris;
			$("#loader_sub_"+idtb).html("");
			var idsub=$("#idsub").val();
			var namasub=$("#namasub").val();
			var err="";
			if(idsub=="") err+="ID sub jabatan belum diisi!<br/>";
			if(namasub=="") err+="Nama sub jabatan belum diisi!<br/>";
			if(err!="") {
				$("#loader_sub").html(err);
				return;
			}
			
			var loadersimpansub=mload.loader({"keterangan":"Proses simpan data ..."});
			var fg="inputdatasub";
			if(boxsubjabatan.id>0) fg="updatedatasub";
			
			var ik_data = {ajax: 1,mode: "mod",cl: "masterjabatan",
				ikdata: {0:fg,
					kode:boxsubjabatan.kode,
					idsub:idsub,
					namasub:namasub,
					idparrent:boxsubjabatan.idparrent,
					id:boxsubjabatan.id}
			};
			
			$.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){
							loadersimpansub.tutup();
							if(boxsubjabatan.id<=0){
								var jumlahsub=parseInt(tabel.gettabel(boxsubjabatan.tabel).getdata(boxsubjabatan.baris,"jumlah_sub"))+1;
								tabel.gettabel(boxsubjabatan.tabel).setdata(boxsubjabatan.baris,"jumlah_sub",jumlahsub);
								boxsubjabatan.el.innerHTML=jumlahsub;
							}
							var idparrent=boxsubjabatan.idparrent;
							masterjabatan.bukasub(idparrent,boxsubjabatan.tabel,boxsubjabatan.baris,boxsubjabatan.el);
							masterjabatan.batalsub();
						}else{
							loadersimpansub.error(e['pesan']);
						}
					}else{
						loadersimpansub.error([{"pesan":"Terjadi kesalahan"}]);
					}
				}
			});
		},
		hapussub:function(a,b,c,d){
			if(a<=0) return;
			var boxyesno=null;
			var databox={
				judul:"Yakin akan menghapus data alat ini ?? ",
				proses:function(){
					var loaderhapussub=mload.loader({"keterangan":"Proses hapus data sub..."});
					boxyesno.tutup();
					
					var ik_data = {
						ajax: 1,
						mode: "mod",
						cl: "masterjabatan",
						ikdata: {0:"hapusdatasub",
							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){
									var tbpar=tabel.gettabel(b).parrent;
									var el=document.getElementById("btjumlahsub_"+tbpar.tabel+"_"+tbpar.baris);
									var jumlahsub=parseInt(tabel.gettabel(tbpar.tabel).getdata(tbpar.baris,"jumlah_sub"))-1;
									tabel.gettabel(tbpar.tabel).setdata(tbpar.baris,"jumlah_sub",jumlahsub);
									el.innerHTML=jumlahsub;
									var idparrent=tabel.gettabel(b).getdata(c,"idparrent");
									
									masterjabatan.bukasub(idparrent,tbpar.tabel,tbpar.baris,el);
									loaderhapussub.tutup();
								}else{
									loaderhapussub.error(e['pesan']);
								}
								
								
							}else{
								loaderhapussub.error([{"pesan":"Gagal hapus data"}]);
							}
						}
					});
				}
			};
			boxyesno=boxwin.yesno(databox);
			boxyesno.buka();
		}
	};
})();