var managkonten=(function(){
    var hal=0;
    var jumlah=30;
    var idkonten=0;
    var idgambar=0;
    return{
        
        caricanggih:function(){
            mload.sh();
            var ik_data = {ajax: 1,mode: "mod",cl: "managkonten",
                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){
                managkonten.loade();
            }
        },
        loade:function(){
            $("#hal_ke").val(0);
            managkonten.load();
        },
        load:function(){
            var nohal=$("#hal_ke").val()
            jumlah=$("#jumlah_hal").val();
            var kategori=$("#sl_kategori").val();
            
            hal=(nohal-1)*jumlah;
            var cr=$("#cari").val();
            
            $("#grid_managkonten").html("");
            var loaderdatakonten=mload.loading({"el":"loader_datakonten"});
            var ik_data = {
                ajax: 1,
                mode: "mod",
                cl: "managkonten",
                ikdata: {0:"getTabel",1:hal,2:jumlah,3:cr,kategori:kategori}
            };
            
            $.ajax({
                url: lokal+"man.ajax.php",
                type: "POST",
                dataType: "json",
                async:true,
                data: ik_data,
                success: function(e){
                    
                    loaderdatakonten.tutup();
                    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_managkonten",
                            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 Konten","tipe":"tpl","tpl":"btdetailkonten"},
                                    {"align":"center","judul":"Utama","tipe":"tpl","tpl":"btutama","lebar":"50px"},
                                    {"align":"center","judul":"Status","tipe":"tpl","tpl":"btstatus","lebar":"50px"},
                                    {"align":"center","judul":"","tipe":"tpl","tpl":"btedit","lebar":"60px"},
                                    
                                ],
                            mod:"mod",
                            module:"managkonten"
                        };
                        tabel.multisub(datatb);
                    }else{
                    	loaderdatakonten.pesan("BELUM ADA DATA KONTEN");
                    }
                }
            });
        },
        buat:function(a){
            document.location=lokal+"member/mod/managkonten?id="+a+"&scrmenu="+modmenu.getmenuscr();
        },
        kembali:function(){
            document.location=lokal+"member/mod/managkonten?scrmenu="+modmenu.getmenuscr();
        },
        edit:function(a){
            idkonten=a['id'];
            $("#sl_kategori").val(a['idkategori']);
            $("#tanggal").datepicker({
                inline: true,
                dateFormat: 'dd/mm/yy'
            });
            idgambar=a['idgambar'];
            
            var namaawal=a['nama_awal'];
            var filelama=a['nama_baru'];
            var folder=a['folder'];
            var prev="tidak";
            
            if(idgambar>0){
                prev="ya";
            }
            $("#box_upload_icon").html("");
            upload.tambahupload("box_upload_icon",managkonten.objupload(namaawal,prev,filelama,folder));
            
            var ted=tinymce.init({
                selector: "textarea#keterangan",
                theme: "modern",
                height: 400,
                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 "
            });
            
        },
        simpan:function(){
            $("#loader_simpan_konten").html("");
            var nm=$("#judul").val();
            var tg=$("#tanggal").val();
            var jm=$("#jam").val();
            var ket=tinyMCE.get('keterangan').getContent();
            var idkategori=$("#sl_kategori").val();
            
            var err="";
            
            if(nm=="") err+="Judul konten belum diisi.</br>";
            if(tg=="") err+="Tanggal konten belum diisi.</br>";
            if(jm=="") err+="Jam konten belum diisi.</br>";
            if(ket=="") err+="Isi konten belum diisi.</br>";
            if(idgambar<=0) err+="Gambar banner harap diisi.</br>";
            var err="";
            
            if(err!=""){
                $("#loader_simpan_konten").html(err);
                return;
            }
            
            var fg="inputkonten";
            if(idkonten>0){
                fg="editkonten";
            }
            var loadersimpan=mload.loader({"keterangan":"Proses simpan data.."});
            var ik_data = {
                ajax: 1,
                mode: "mod",
                cl: "managkonten",
                ikdata: {0:fg,judul:nm,tanggal:tg,jam:jm,keterangan:ket,idkategori:idkategori,
                    id:idkonten,idgambar:idgambar}
            };
            
            $.ajax({
                url: lokal+"man.ajax.php",
                type: "POST",
                dataType: "json",
                async:true,
                data: ik_data,
                success: function(e){
                    
                    if(e==1){
                       managkonten.kembali();
                    }else{
                    	loadersimpan.error([{"pesan":"Gagal simpan konten informasi ...."}]);
                    }
                }
            });
        },
        hapus:function(a){
           	if(a<=0) return;
			var boxyesno=null;
			var databox={
				judul:"Yakin akan menghapus data konten informasi ini ?? ",
				proses:function(){
					var loaderhapus=mload.loader({"keterangan":"Proses hapus data.."});
					boxyesno.tutup();
					var ik_data = {
						ajax: 1,
						mode: "mod",
						cl: "managkonten",
						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"}]);
								managkonten.load();
							}else{
								loaderhapus.error([{"pesan":"Gagal hapus data"}]);
							}
						}
					});
				}
			};
			boxyesno=boxwin.yesno(databox);
			boxyesno.buka();
        },
        setstatus:function(a,b,c){
            var st=1;
            if(b==1){
                st=0;
            }
            
            var loaderstatus=mload.loader({"keterangan":"Update status .."});
            var ik_data = {ajax: 1,mode: "mod",cl: "managkonten",
                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){
                            c.className="mini_ok";
                        }else{
                            c.className="mini_dis";
                        }
                        c.onclick=function(){
                            managkonten.setstatus(a,st,c);
                        };
                    }else{
                    	loaderstatus.error([{"pesan":"Gagal update status"}]);
                    }
                }
            });
        },
        setutama:function(a,b,c,d){
            var st=1;
            if(tabel.gettabel(b).getdata(c,"utama")==1){
                st=0;
            }
            
            var loaderutama=mload.loader({"keterangan":"Update status utama .."});
            var ik_data = {ajax: 1,mode: "mod",cl: "managkonten",
                ikdata: {0:"setUtama",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){
                    	loaderutama.tutup();
                        if(st==1){
                            d.className="mini_ok";
                        }else{
                            d.className="mini_dis";
                        }
                        tabel.gettabel(b).setdata(c,"utama",st);
                    }else{
                    	loaderutama.error([{"pesan":"Gagagl update status utama"}]);
                    }
                }
            });
        },
        lihatdokumen:function(a){
			var iddok=a;
			if(iddok<=0) return;
			var myWindow = window.open(lokal+"service.php?pos=managkonten&mode=mod&fl=download&id="+iddok, "", "left=100,top=50,width=800, height=600");
		},
        objupload:function(a,b,c,d){
            var nama=a || "PILIH FILE UNTUK UPLOAD BANNER BERITA";
            var prev=b || "tidak";
            var filelama=c || "";
            var folder=d || "";
            
            var obp=new Object;
            obp.proses=function(dataupload){
                idgambar=dataupload.id;
                nama=dataupload.nama;
                filelama=dataupload.namabaru;
                folder=dataupload.folder;
                
                $("#box_upload_icon").html("");
                upload.tambahupload("box_upload_icon",managkonten.objupload(nama,"ya",filelama,folder));
            };
            
            obp.hapus=function(a){
                
            };
            obp.lihat=function(a){
                managkonten.lihatdokumen(idgambar);
            };
            
            obp.pilihfile=function(a){
                idgambar=0;
            };
            
            var dtpost={"jenis":2};
            var obj={"tautan":lokal+"service.php?pos=managkonten&mode=mod&fl=upload",
                "status":lokal+"service.php?pos=managkonten&mode=mod&fl=progres","tutup":"tidak","finish":obp,
                "data":dtpost,"namafile":nama,"preview":prev};
            return obj;
        }
    };
})();