var nomorsurat=(function(){
	var mode=0;
	var idlogo=0;
	var datagambar={};
	var dataurutan=[];
	var boxjenisnomor=[];
	
	return{
		preload:function(a){
			if(a.hasOwnProperty("reset")){
				$("#sl_mode").val(a['reset']);
				
			}
			if(a.hasOwnProperty("dataurutan")){
				dataurutan=a['dataurutan'];
			}
			nomorsurat.tabeljenis();
		},
		tambahnomor:function(){
			var databox={box:"box_nomor_surat",judul:"Input Jenis Penomoran",tinggi:"auto",lebar:"500"};
			boxjenisnomor=boxwin.load(databox);
			boxjenisnomor.buka();
			boxjenisnomor.mode=0;
			boxjenisnomor.baris=0;
			boxjenisnomor.tabel=0;
			
			nomorsurat.jenisch();
		},
		inputnomor:function(){
			var jn=$("#sl_mode_surat").val();
			var str=$("#strcustom").val();
			if(jn!="custom") str="";
			if(boxjenisnomor.mode==0){
				dataurutan.push({"jenis":jn,"custom":str});
			}else{
				dataurutan[boxjenisnomor.baris]['jenis']=jn;
				dataurutan[boxjenisnomor.baris]['custom']=str;
			}
			nomorsurat.tabeljenis();
			nomorsurat.bataltambah();
		},
		editjenis:function(a,b){
			var databox={box:"box_nomor_surat",judul:"Input Jenis Penomoran",tinggi:"auto",lebar:"500"};
			boxjenisnomor=boxwin.load(databox);
			boxjenisnomor.buka();
			boxjenisnomor.mode=1;
			boxjenisnomor.tabel=a;
			boxjenisnomor.baris=b;
			$("#sl_mode_surat").val(tabel.gettabel(a).getdata(b,"jenis"));
			$("#strcustom").val(tabel.gettabel(a).getdata(b,"custom"));
			nomorsurat.jenisch();
		},
		hapusjenis:function(a,b){
			var boxyesno=null;
			var databox={
				judul:"Yakin akan menghapus data jenis penomoran ini!! ",
				proses:function(){
					dataurutan.splice(b,1);
					nomorsurat.tabeljenis();
					boxyesno.tutup();
				}
			};
			boxyesno=boxwin.yesno(databox);
			boxyesno.buka();
		},
		jenisch:function(){
			$("#groupcustom").hide();
			var jn=$("#sl_mode_surat").val();
			
			if(jn=="custom"){
				$("#groupcustom").show();
			}
		},
		tabeljenis:function(){
			$("#grid_urutan").html("");
			if(dataurutan.length>0){
				var datatb={
					box:"grid_urutan",
					data:dataurutan,
					lebarmin:"500px",
					trklik:{"data":"id","fungsi":"barisklik"},
					title:"kategori Menu",
					isi:[
							{"col":"","judul":"No","tipe":"urut","align":"center","lebar":"50px","halaman":0},
							{"col":"jenis","judul":"Jenis Nomor","tipe":"data"},
							{"col":"custom","judul":"Custom Text","tipe":"data","lebar":"160px"},
							{"align":"center","judul":"","tipe":"tpl","tpl":"bteditnomor","lebar":"60px"},
							
						],
					mod:"mod",
					module:"pendudukmasuk"
				};
				tabel.multisub(datatb);
			}else{
				$("#grid_urutan").html("BELUM ADA URUTAN NOMOR SURAT");
			}
			
		},
		
		simpan:function(){
			$("#err_simpan").html("");
			var batas=$("#pemisah").val();
			var reset=$("#sl_mode").val();
			
			var err="";
			if(batas=="") err+="Batas pemisah nomor surat belum diisi.</br>";
			if(dataurutan.length<=0) err+="Penomoran nomor surat belum diisi.</br>";
			
			if(err!=""){
				$("#err_simpan").html(err);
				return;
			}
			
			var datanomor={batas:batas,reset:reset,dataurutan:dataurutan};
			
			var loadersimpan=mload.loading({"el":"err_simpan"});
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "nomorsurat",
				ikdata: {0:"simpannomorsurat",datanomor:datanomor}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				dataType: "json",
				async:true,
				data: ik_data,
				success: function(e){
					
					if(e==1){
						loadersimpan.tutup();
						alert("Simpan penomoran surat berhasil");
					}else if(e==2){
						loadersimpan.pesan("Gagal simpan pengaturan");
					}
				}
			});
		},
		bataltambah:function(){
			if(boxjenisnomor!=null) boxjenisnomor.tutup();
			boxjenisnomor=null;
		}
	};
})();