var masterpropinsi=(function(){
	var hal=0;
	var jumlah=30;
	var idpropinsi=0;
	var boxlokasiarea=null;
	return{
		caricanggih:function(){
			mload.sh();
			var ik_data = {ajax: 1,mode: "mod",cl: "masterpropinsi",
				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){
				masterpropinsi.loade();
			}
		},
		loade:function(){
			$("#hal_ke").val(0);
			masterpropinsi.load();
		},
		load:function(){
			var nohal=$("#hal_ke").val()
			jumlah=$("#jumlah_hal").val();
			hal=(nohal-1)*jumlah;
			var cr=$("#cari").val();
			
			$("#grid_masterpropinsi").html("");
			var loaderdata=mload.loading({"el":"loader_propinsi"});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "masterpropinsi",
				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_masterpropinsi",
							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":"kode","judul":"Kode Propinsi","tipe":"data","lebar":"100px"},
									{"col":"propinsi","judul":"Nama Propinsi","tipe":"data"},
									//{"align":"center","judul":"Lokasi","tipe":"tpl","tpl":"btlokasi","lebar":"220px"},
									{"align":"center","judul":"Status","tipe":"tpl","tpl":"btstatus","lebar":"60px"},
									{"align":"center","judul":"","tipe":"tpl","tpl":"btedit","lebar":"60px"},
								],
							mod:"mod",
							module:"masterpropinsi"
						};
						tabel.multisub(datatb);
					}else{
						loaderdata.pesan("BELUM ADA DATA PROPINSI");
					}
				}
			});
		},
		buat:function(a){
			document.location=lokal+"member/mod/masterpropinsi?id="+a+"&scrmenu="+modmenu.getmenuscr();
		},
		kembali:function(){
			document.location=lokal+"member/mod/masterpropinsi?scrmenu="+modmenu.getmenuscr();
		},
		edit:function(a){
			idpropinsi=a;
		},
		simpan:function(){
			$("#error_ppropinsi").html("");
			var nm=$("#nama").val();
			var kd=$("#kode").val();
			var lokasi=$("#lokasi").html();
			
			var err="";
			if(nm=="") err+="Nama Propinsi belum diisi.</br>";
			if(kd=="") err+="Kode Propinsi belum diisi.</br>";
			if(err!=""){
				$("#error_ppropinsi").html(err);
				return;
			}
			var fg="inputpropinsi";
			if(idpropinsi>0){
				fg="editpropinsi";
			}
			var loadersimpan=mload.loader({"keterangan":"Proses simpan data ..."});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "masterpropinsi",
				ikdata: {0:fg,nama:nm,id:idpropinsi,kode:kd,lokasi:lokasi}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				dataType: "json",
				async:true,
				data: ik_data,
				success: function(e){
					
					if(e==1){
						masterpropinsi.kembali();
					}else{
						loadersimpan.error([{"pesan":"Gagal simpan data propinsi ..."}]);
					}
				}
			});
		},
		hapus:function(a){
			if(a<=0) return;
			var ps={"pesan":"Yakin akan menghapus data propinsi ini ??",
					"title":"Peringatan Hapus data"};
				
			var boxyesno=boxpesan.yesno(ps);
			boxyesno.proses.onclick=function(){
				
				boxyesno.tutup();
				var loaderhapus=mload.loader({"keterangan":"Proses hapus data.."});
				var ik_data = {
					ajax: 1,
					mode: "mod",
					cl: "masterpropinsi",
					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){
							loaderhapus.error([{"pesan":"Berhasil hapus data"}]);
							masterpropinsi.load();
						}else{
							loaderhapus.error([{"pesan":"Gagal hapus data"}]);
						}
					}
				});
			}
		},
		setstatus:function(a,b,c,d){
			var st=1;
			if(tabel.gettabel(b).getdata(c,"status")==1){
				st=0;
			}
			
			var loaderstatus=mload.loader({"keterangan":"Update status ..."});
			var ik_data = {ajax: 1,mode: "mod",cl: "masterpropinsi",
				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.."}]);
					}
				}
			});
		},
		pilihlokasi:function(){
			var strlokasi=$("#lokasi").html();
			var lokasi="-6.209095839597512,106.85921458124994";
			if(strlokasi!="" && strlokasi.split(",").length==2){
				lokasi=strlokasi.split(",")[0]+","+strlokasi.split(",")[1];
			}
			
			var ps={"box":"box_winpeta",
						"title":"Input Koordinat","tinggimax":"fit","tinggi":kudus.tinggifit()-200+"px","edit":true,"mode":"buat","zoom":8,"lokasi":lokasi};
				
			boxlokasiarea=boxpesan.peta(ps);
			boxlokasiarea.proses.onclick=function(){
				$("#lokasi").html(boxlokasiarea.tanda);
				boxlokasiarea.tutup();
			};
		},
		setlokasi:function(a,b,c,d){
			var strlokasi=tabel.gettabel(b).getdata(c,"lokasipeta");
			var lokasi="-6.209095839597512,106.85921458124994";
			if(strlokasi!="" && strlokasi.split(",").length==2){
				lokasi=strlokasi.split(",")[0]+","+strlokasi.split(",")[1];
			}
			
			var ps={"box":"box_winpeta",
						"title":"Input Koordinat","tinggimax":"fit","tinggi":kudus.tinggifit()-200+"px","edit":true,"mode":"buat","zoom":8,"lokasi":lokasi};
				
			boxlokasiarea=boxpesan.peta(ps);
			boxlokasiarea.proses.onclick=function(){
				mload.sh();
				var ik_data = {ajax: 1,mode: "mod",cl: "masterpropinsi",
					ikdata: {0:"updateLokasi",lokasi:boxlokasiarea.tanda,id:a}
				};
				
				$.ajax({
					url: lokal+"man.ajax.php",type: "POST",dataType: "json",async:true,data: ik_data,
					success: function(e){
						
						mload.hd();
						//alert(JSON.stringify(e));
						if(e==1){
							masterpropinsi.load();
							boxlokasiarea.tutup();
						}
					}
				});
				
			};
		}
	};
})();