var masterproduk=(function(){
	var hal=0;
	var jumlah=30;
	var idproduk=0;
	var datagambar=[];
	var boxupload=null;
	var jumlahreq=0;
	return{
		reqcb:function(jumlah){
			var requestcb = new reqcb({
			    jumlahReq: jumlah
			});
			return requestcb;
		},
		caricanggih:function(){
			mload.sh();
			var ik_data = {ajax: 1,mode: "mod",cl: "masterproduk",
				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){
				masterproduk.loade();
			}
		},
		loade:function(){
			$("#hal_ke").val(0);
			masterproduk.load();
		},
		load:function(){
			var nohal=$("#hal_ke").val()
			jumlah=$("#jumlah_hal").val();
			hal=(nohal-1)*jumlah;
			var cr=$("#cari").val();
			
			$("#grid_masterproduk").html("");
			var loaderdata=mload.loading({"el":"loader_daataproduk"});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "masterproduk",
				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_masterproduk",
							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},
									{"judul":"Detail Produk","tipe":"tpl","tpl":"btdetailproduk"},
									{"col":"harga","judul":"Harga","tipe":"data","align":"right","lebar":"130px","format":"desimal"},
									{"col":"point","judul":"Point","tipe":"data","align":"right","lebar":"90px","format":"desimal2"},
									{"align":"center","judul":"Status","tipe":"tpl","tpl":"btstatus","lebar":"60px"},
									{"align":"center","judul":"","tipe":"tpl","tpl":"btedit","lebar":"60px"},
									
								],
							mod:"mod",
							module:"masterproduk"
						};
						tabel.multisub(datatb);
					}else{
						loaderdata.pesan("BELUM ADA DATA PRODUK");
					}
				}
			});
		},
		buat:function(a){
			document.location=lokal+"member/mod/masterproduk?id="+a+"&scrmenu="+modmenu.getmenuscr();
		},
		kembali:function(){
			document.location=lokal+"member/mod/masterproduk?scrmenu="+modmenu.getmenuscr();
		},
		edit:function(a){
			idproduk=a['id'];
			datagambar=[];
			$("#grid_gambar").html("");
			
			var ted=tinymce.init({
			    selector: "textarea#keterangan",
			    theme: "modern",
			    menubar:false,
			    height: 200,
			    relative_urls: false,
				remove_script_host: false,
			    plugins: [
			         "advlist autolink link image lists charmap print preview hr anchor pagebreak spellchecker",
			         "searchreplace visualblocks visualchars code fullscreen insertdatetime media nonbreaking",
			         "save table contextmenu directionality emoticons template paste textcolor kfile"
			   	],
			
			    toolbar: "kfile undo redo | styleselect | bold italic fontselect fontsizeselect | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image "
			});
			
			var obj=new Object;
			var cbf=masterproduk.reqcb(1);
			
	        cbf.addCallbackToQueue(true, function() {
	            masterproduk.getgambarproduk(idproduk);
	        });
	        
		},
		getgambarproduk:function(a){
			$("#grid_gambar").html("");
			datagambar=[];
			
			var loadergambarproduk=mload.loading({"el":"grid_gambar"});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "masterproduk",
				ikdata: {0:"getGambarproduk",
					idproduk:a}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				dataType: "json",
				async:true,
				data: ik_data,
				success: function(e){
					loadergambarproduk.tutup();
					if(e!=false && e.length>0){
						datagambar=e;
						masterproduk.tabelgambar();
					}else{
						loadergambarproduk.pesan("BELUM ADA DATA GAMBAR PRODUK")
					}
				}
			});
		},
		tabelgambar:function(){
			$("#grid_gambar").html("");
			if(datagambar.length>0){
				var datatb={
					box:"grid_gambar",
					data:datagambar,
					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":"Nama","tipe":"data"},
							{"align":"center","judul":"Dok","tipe":"tpl","tpl":"btdokumen","lebar":"60px"},
							{"align":"center","judul":"","tipe":"tpl","tpl":"bteditgambar","lebar":"40px"},
						],
					mod:"mod",
					module:"masterproduk"
				};
				tabel.multisub(datatb);
			}else{
				$("#grid_gambar").html("BELUM ADA DATA GAMBAR PRODUK");
			}
		},
		inputgambarproduk:function(){
			var ps={"box":"box_upload",
				"judul":"Upload Gambar Produk",tinggi:"auto",lebar:"600"};
	
			boxupload=boxwin.load(ps);
			boxupload.id=0;
			boxupload.idgambar=0;
			boxupload.idproduk=idproduk;
			boxupload.namaawal="PILIH FILE GAMBAR";
			boxupload.namabaru="";
			boxupload.tabel=0;
			boxupload.baris=0;
			boxupload.mode=0;
			boxupload.buka();
			
			$("#boxtahap").hide();
			
			$("#box_upload_gambar").html("");
			$("#loader_simpan_upload").html("");
			$("#namagambar").val("");
			
			var namafile="PILIH FILE GAMBAR PRODUK";
			var namabaru="";
			
			upload.tambahupload("box_upload_gambar",masterproduk.objupload(namafile,"tidak"));
		},
		simpanupload:function(){
			$("#loader_simpan_upload").html("");
			var nama=$("#namagambar").val();
			
			var err="";
			if(nama=="") err+="Nama gambar belum diisi!<br/>";
			if(boxupload.idgambar<=0) err+="Gambar belum diupload!<br/>";
			if(err!=""){
				$("#loader_simpan_upload").html(err);
				return;
			}
			
			if(idproduk<=0){
				if(boxupload.mode==0){
					datagambar.push({
						"id":0,
						"idgambar":boxupload.idgambar,
						"nama":nama,
						"nama_awal":boxupload.namaawal,
						"nama_baru":boxupload.namabaru		
					});
				}else{
					datagambar[boxupload.baris]['idgambar']=boxupload.idgambar;
					datagambar[boxupload.baris]['nama']=nama;
					datagambar[boxupload.baris]['nama_awal']=boxupload.namaawal;
					datagambar[boxupload.baris]['nama_baru']=boxupload.namabaru;
				}
				masterproduk.batalupload();
				masterproduk.tabelgambar();
			}else{
				var loadersimpangambar=mload.loading({"el":"loader_simpan_upload"});
				var fg="inputGambarproduk";
				if(boxupload.mode==1){
					fg="editGambarproduk";
				}
				var ik_data = {
					ajax: 1,
					mode: "mod",
					cl: "masterproduk",
					ikdata: {0:fg,
						nama:nama,
						idgambar:boxupload.idgambar,
						idproduk:boxupload.idproduk,
						id:boxupload.id}
				};
				
				$.ajax({
					url: lokal+"man.ajax.php",
					type: "POST",
					dataType: "json",
					async:true,
					data: ik_data,
					success: function(e){
						loadersimpangambar.tutup();
						if(e==1){
							masterproduk.batalupload();
							masterproduk.getgambarproduk(idproduk);
						}else{
							loadersimpangambar.pesan("Gagal input gambar produk");
						}
					}
				});
			}
		},
		batalupload:function(){
			if(boxupload!=null) boxupload.tutup();
			boxupload=null;
		},
		hapusgambar:function(a,b,c,d){
			if(a<=0) return;
			var boxyesno=null;
			var databox={
				judul:"Yakin akan menghapus data gambar produk ini ?? ",
				proses:function(){
					boxyesno.tutup();
					if(idproduk<=0){
						datagambar.splice(c,1);
						masterproduk.tabelgambar();
					}else{
						var loaderhapus=mload.loader({"keterangan":"Proses hapus data.."});
						var ik_data = {
							ajax: 1,
							mode: "mod",
							cl: "masterproduk",
							ikdata: {0:"hapusDatagambar",
								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 "}]);
									masterproduk.getgambarproduk(idproduk);
								}else{
									loaderhapus.error([{"pesan":"Gagal "}]);
								}
							}
						});
					}
					
				}
			};
			boxyesno=boxwin.yesno(databox);
			boxyesno.buka();
		},
		simpan:function(){
			$("#loader_produk_err").html("");
			var nama=$("#nama").val();
			var kode=$("#kode").val();
			var keterangan=tinyMCE.get('keterangan').getContent();
			
			var strharga=$("#harga").val();
			var harga=kudus.buatdesimal(strharga);
			var strpoint=$("#point").val();
			var point=kudus.buatdesimal(strpoint);
			
			var err="";
			if(nama=="") err+="Nama produk belum diisi.</br>";
			if(kode=="") err+="Kode produk belum diisi.</br>";
			if(parseInt(harga)<0) err+="Harga produk belum diisi.</br>";
			if(parseFloat(point)<0) err+="Point produk belum diisi.</br>";
			
			if(err!=""){
				$("#loader_produk_err").html(err);
				return;
			}
			var arrgambar=[];
			if(idproduk<=0) arrgambar=datagambar;
			
			var fg="inputproduk";
			if(idproduk>0){
				fg="editproduk";
			}
			var loadersimpan=mload.loader({"keterangan":"Proses simpan data"});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "masterproduk",
				ikdata: {0:fg,nama:nama,
					kode:kode,
					harga:harga,
					point:point,
					keterangan:keterangan,
					datagambar:arrgambar,
					id:idproduk}
			};
			
			$.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){
							masterproduk.kembali();
						}else{
							loadersimpan.error(e['pesan']);
						}
					}else{
						loadersimpan.error([{"pesan":"Gagal simpan data produk"}]);
					}
				}
			});
		},
		hapus:function(a){
			if(a<=0) return;
			var boxyesno=null;
			var databox={
				judul:"Yakin akan menghapus data produk ini ?? ",
				proses:function(){
					boxyesno.tutup();
					var loaderhapus=mload.loader({"keterangan":"Proses hapus data.."});
					var ik_data = {
						ajax: 1,
						mode: "mod",
						cl: "masterproduk",
						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 "}]);
								masterproduk.load();
							}else{
								loaderhapus.error([{"pesan":"Gagal "}]);
							}
						}
					});
				}
			};
			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: "masterproduk",
				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();
					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"}]);
					}
				}
			});
		},
		bukagambar:function(a,b,c,d){
			var idgambar=tabel.gettabel(b).getdata(c,"idgambar");
			if(idgambar>0){
				masterproduk.lihatdokumen(idgambar);
			}
		},
		lihatdokumen:function(a){
			var iddok=a;
			if(iddok<=0) return;
			var myWindow = window.open(lokal+"service.php?pos=masterproduk&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 GAMBAR PRODUK";
			var prev=b || "tidak";
			
			var obp=new Object;
			obp.proses=function(dataupload){
				boxupload.idgambar=dataupload.id;
				boxupload.namaawal=dataupload.nama;
				boxupload.namabaru=dataupload.namabaru;
				filelama=dataupload.nama;
				
				$("#box_upload_gambar").html("");
				upload.tambahupload("box_upload_gambar",masterproduk.objupload(filelama,"ya"));
			};
			
			obp.hapus=function(a){
				
			};
			obp.lihat=function(a){
				masterproduk.lihatdokumen(boxupload.idgambar);
			};
			
			obp.pilihfile=function(a){
				boxupload.idgambar=0;
			};
			
			var dtpost={"jenis":11};
			var obj={"tautan":lokal+"service.php?pos=masterproduk&mode=mod&fl=upload",
				"status":lokal+"service.php?pos=masterproduk&mode=mod&fl=progres","tutup":"tidak","finish":obp,
				"data":dtpost,"namafile":nama,"preview":prev};
			return obj;
		},
		
	};
})();