var managdownload=(function(){
	var hal=0;
	var jumlah=30;
	var iddownload=0;
	var iddokumen=0;
	return{
		caricanggih:function(){
			mload.sh();
			var ik_data = {ajax: 1,mode: "mod",cl: "managdownload",
				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){
				managdownload.loade();
			}
		},
		loade:function(){
			$("#hal_ke").val(0);
			managdownload.load();
		},
		load:function(){
			var nohal=$("#hal_ke").val()
			jumlah=$("#jumlah_hal").val();
			hal=(nohal-1)*jumlah;
			var cr=$("#cari").val();
			
			$("#grid_managdownload").html("");
			var loaderdata=mload.loading({"el":"loader_datadownload"});
			
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "managdownload",
				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_managdownload",
							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 File","tipe":"tpl","tpl":"btdetaildownload"},
									{"align":"center","judul":"Status","tipe":"tpl","tpl":"btstatusfile","lebar":"60px"},
									{"align":"center","judul":"Hapus","tipe":"tpl","tpl":"btedit","lebar":"80px"}
								],
							mod:"mod",
							module:"managdownload"
						};
						tabel.multisub(datatb);
					}else{
						loaderdata.pesan("BELUM ADA DATA DOWNLOAD");
					}
				}
			});
		},
		buat:function(a){
			document.location=lokal+"member/mod/managdownload?id="+a+"&scrmenu="+modmenu.getmenuscr();
		},
		kembali:function(){
			document.location=lokal+"member/mod/managdownload?scrmenu="+modmenu.getmenuscr();
		},
		edit:function(a){
			iddownload=a['id'];
			iddokumen=a['iddokumen'];
			$("#sl_kategori").val(a['idkategori']);
			$("#box_upload_img").html("");
			
			var namafile=a['nama_awal'];
			var prev="tidak";
			if(iddokumen>0) prev="ya";
			upload.tambahupload("box_upload_img",managdownload.objupload(namafile,prev));
		},
		simpan:function(){
			var nm=$("#nama").val();
			var ket=$("#keterangan").val();
			var kat=$("#sl_kategori").val();
			var err="";
			if(nm=="") err+="Nama Download belum diisi.</br>";
			if(iddokumen<=0) err+="File upload belum dipilih/belum selesai";
			
			if(err!=""){
				var ps={"title":"Peringatan","pesan":err};
				boxpesan.peringatan(ps);
				return;
			}
			var fg="inputdownload";
			if(iddownload>0){
				fg="editdownload";
			}
			var loadersimpandata=mload.loader({"keterangan":"Proses simpan data.."});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "managdownload",
				ikdata: {0:fg,nama:nm,keterangan:ket,iddokumen:iddokumen,
					idkategori:kat,id:iddownload}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				dataType: "json",
				async:true,
				data: ik_data,
				success: function(e){
					if(e==1){
						managdownload.kembali();
					}else{
						loadersimpandata.error([{"pesan":"Gagal simpan data"}]);
					}
				}
			});
		},
		hapusdokumen:function(a){
			if(a<=0) return;
			var boxyesno=null;
			var databox={
				judul:"Yakin akan menghapus data file download ini ?? ",
				proses:function(){
					var loaderhapus=mload.loader({"keterangan":"Proses hapus data.."});
					boxyesno.tutup();
					var ik_data = {
						ajax: 1,
						mode: "mod",
						cl: "managdownload",
						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==1){
								loaderhapus.error([{"pesan":"Berhasil"}]);
								managdownload.load();
							}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":"Update status.."});
			var ik_data = {ajax: 1,mode: "mod",cl: "managdownload",
				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){
					
					loaderstatus.tutup();
					//alert(JSON.stringify(e));
					if(e==1){
						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"}]);
					}
				}
			});
		},
		lihatdokumen:function(a){
			var iddok=a;
			if(iddok<=0) return;
			var myWindow = window.open(lokal+"service.php?pos=managdownload&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 FILE";
			var prev=b || "tidak";
			
			var obp=new Object;
			obp.proses=function(dataupload){
				iddokumen=dataupload.id;
				nama=dataupload.nama;
				
				$("#box_upload_img").html("");
				upload.tambahupload("box_upload_img",managdownload.objupload(nama,"ya"));
				
			};
			
			obp.hapus=function(a){
				
			};
			obp.lihat=function(a){
				managdownload.lihatdokumen(iddokumen);
			};
			
			obp.pilihfile=function(a){
				iddokumen=0;
			};
			
			var dtpost={"jenis":10};
			var obj={"tautan":lokal+"service.php?pos=managdownload&mode=mod&fl=upload",
				"status":lokal+"service.php?pos=managdownload&mode=mod&fl=progres","tutup":"tidak","finish":obp,
				"data":dtpost,"namafile":nama,"preview":prev};
			return obj;
		},
		
	};
})();