var masterkota=(function(){
	var hal=0;
	var jumlah=30;
	var idkota=0;
	var boxlokasiarea=null;
	return{
		caricanggih:function(){
			mload.sh();
			var ik_data = {ajax: 1,mode: "mod",cl: "masterkota",
				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){
				masterkota.loade();
			}
		},
		loade:function(){
			$("#hal_ke").val(0);
			masterkota.load();
		},
		load:function(){
			var nohal=$("#hal_ke").val()
			jumlah=$("#jumlah_hal").val();
			hal=(nohal-1)*jumlah;
			var cr=$("#cari").val();
			
			$("#grid_masterkota").html("");
			var loaderdata=mload.loading({"el":"loader_datakota"});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "masterkota",
				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_masterkota",
							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":"propinsi","judul":"Propinsi","tipe":"data","lebar":"200px"},
									{"col":"kode","judul":"Kode Kota","tipe":"data","lebar":"100px"},
									{"col":"kota","judul":"Nama Kota","tipe":"data"},
									{"align":"center","judul":"Lokasi","tipe":"tpl","tpl":"btlokasi","lebar":"220px"},
									{"align":"center","judul":"","tipe":"tpl","tpl":"btedit","lebar":"60px"},
									
								],
							mod:"mod",
							module:"masterkota"
						};
						tabel.multisub(datatb);
					}else{
						loaderdata.pesan("BELUM ADA DATA KOTA");
					}
				}
			});
		},
		buat:function(a){
			document.location=lokal+"member/mod/masterkota?id="+a+"&scrmenu="+modmenu.getmenuscr();
		},
		kembali:function(){
			document.location=lokal+"member/mod/masterkota?scrmenu="+modmenu.getmenuscr();
		},
		edit:function(a,b){
			idkota=a;
			$("#sl_propinsi").val(b);
		},
		simpan:function(){
			$("#loader_err_kota").html("");
			var nm=$("#nama").val();
			var idprop=$("#sl_propinsi").val();
			var kd=$("#kode").val();
			var lokasi=$("#lokasi").html();
			
			var err="";
			if(nm=="") err+="Nama kota belum diisi.</br>";
			if(kd=="") err+="Kode kota belum diisi.</br>";
			if(err!=""){
				$("#loader_err_kota").html(err);
				return;
			}
			var fg="inputkota";
			if(idkota>0){
				fg="editkota";
			}
			var loadersimpan=mload.loader({"keterangan":"Proses simpan data..."});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "masterkota",
				ikdata: {0:fg,nama:nm,idpropinsi:idprop,
					kode:kd,lokasi:lokasi,id:idkota}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				dataType: "json",
				async:true,
				data: ik_data,
				success: function(e){
					
					if(e==1){
						masterkota.kembali();
					}else{
						loadersimpan.error([{"pesan":"Gagal simpan data"}]);
					}
				}
			});
		},
		hapus:function(a){
			if(a<=0) return;
			var ps={"pesan":"Yakin akan menghapus data kota 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: "masterkota",
					ikdata: {0:"hapusData",1: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"}]);
							masterkota.load();
						}else{
							loaderhapus.error([{"pesan":"Gagal"}]);
						}
					}
				});
			}
		},
		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: "masterkota",
					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){
							masterkota.load();
							boxlokasiarea.tutup();
						}
					}
				});
				
			};
		}
	};
})();