var mastercatatan=(function(){
	var hal=0;
	var jumlah=30;
	var idmastercatatan=0;
	return{
		caricanggih:function(){
			var loadercari=mload.loader({"keterangan":"Proses ...."});
			var ik_data = {ajax: 1,mode: "mod",cl: "mastercatatan",
				ikdata: {0:"datacari01",1:1}
			};
			
			$.ajax({url: lokal+"man.ajax.php",type: "POST",dataType: "json",async:true,data: ik_data,
				success: function(e){
					loadercari.tutup();
					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){
				mastercatatan.loade();
			}
		},
		loade:function(){
			$("#hal_ke").val(0);
			mastercatatan.load();
		},
		load:function(){
			var nohal=$("#hal_ke").val()
			jumlah=$("#jumlah_hal").val();
			hal=(nohal-1)*jumlah;
			var cr=$("#cari").val();
			
			$("#grid_mastercatatan").html("");
			var loaderdatagolongan=mload.loading({"el":"loader_data_golongan"});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "mastercatatan",
				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){
					loaderdatagolongan.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_mastercatatan",
							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":"kategori","judul":"Kategori","tipe":"data","lebar":"150px"},
									{"col":"nama","judul":"Nama Catatan","tipe":"data"},
									{"align":"center","judul":"Status","tipe":"tpl","tpl":"btstatus","lebar":"60px"},
									{"align":"center","judul":"","tipe":"tpl","tpl":"btedit","lebar":"60px"},
									
								],
							mod:"mod",
							module:"mastercatatan"
						};
						tabel.multisub(datatb);
					}else{
						loaderdatagolongan.pesan("BELUM ADA DATA");
					}
				}
			});
		},
		buat:function(a){
			document.location=lokal+"member/mod/mastercatatan?id="+a+"&scrmenu="+modmenu.getmenuscr();
		},
		kembali:function(){
			document.location=lokal+"member/mod/mastercatatan?scrmenu="+modmenu.getmenuscr();
		},
		edit:function(a){
			idmastercatatan=a['id'];
			$("#sl_kategori").val(a['idkategori']);
			var ted=tinymce.init({
                selector: "textarea#catatan",
                theme: "modern",
                height: 200,
                relative_urls: false,
                remove_script_host: false,
                menubar: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: "undo redo | styleselect | bold italic fontselect fontsizeselect | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent"
            });
		},
		simpan:function(){
			$("#loader_simpan_err").html("");
			var nm=$("#nama").val();
			var idkategori=$("#sl_kategori").val();
			var catatan=tinyMCE.get('catatan').getContent();
			var err="";
			if(nm=="") err+="Nama Catatan belum diisi.</br>";
			if(catatan=="") err+="Catatan belum diisi.</br>";
			
			if(err!=""){
				$("#loader_simpan_err").html(err);
				return;
			}
			var fg="inputmastercatatan";
			if(idmastercatatan>0){
				fg="editmastercatatan";
			}
			var loadersimpandata=mload.loader({"keterangan":"Proses simpan data.."});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "mastercatatan",
				ikdata: {0:fg,
					nama:nm,
					idkategori:idkategori,
					catatan:catatan,
					id:idmastercatatan}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				dataType: "json",
				async:true,
				data: ik_data,
				success: function(e){
					if(e==1){
						mastercatatan.kembali();
					}else{
						loadersimpandata.error([{"pesan":"Gagal simpan data"}]);
					}
				}
			});
		},
		hapus:function(a){
			if(a<=0) return;
			var ps={"pesan":"Yakin akan menghapus data catatan ini ??",
					"title":"Peringatan Hapus data"};
				
			var boxyesno=boxpesan.yesno(ps);
			boxyesno.proses.onclick=function(){
				
				boxyesno.tutup();
				var loadersimpandata=mload.loader({"keterangan":"Proses hapus data.."});
				var ik_data = {
					ajax: 1,
					mode: "mod",
					cl: "mastercatatan",
					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){
							loadersimpandata.error([{"pesan":"Berhasil"}]);
							mastercatatan.load();
						}else{
							loadersimpandata.error([{"pesan":"Gagal hapus data"}]);
						}
					}
				});
			}
		},
		setstatus:function(a,b,c,d){
            var st=0;
            if(tabel.gettabel(b).getdata(c,"status")==0){
                st=1;
            }
            
            mload.loader();
            var ik_data = {
                ajax: 1,
                mode: "mod",
                cl: "mastercatatan",
                ikdata: {0:"setstatus",id:a,status:st}
            };
            
            $.ajax({
                url: lokal+"man.ajax.php",
                type: "POST",
                dataType: "json",
                async:true,
                data: ik_data,
                success: function(e){
                    
                    if(e==1){
                        
                        d.className="mini_dis";
                        if(st==1){
                            d.className="mini_ok";
                        }
                        tabel.gettabel(b).setdata(c,"status",st);
                        mload.tutup();
                    }else{
                        mload.error([{"pesan":"Status error"}]);
                    }
                }
            });
        },

	};
})();