 
jQuery(function($){
	window.initRupI18nPromise.then(function () {
		$.rup_date({
			yearRange: 'c-100:c+100',
	        from: "fecrecep50desde_filter_table",
	        to: "fecrecep50hasta_filter_table",
			datetimepicker:true,
			placeholderMask : true,
			mask: 'dd-MM-yyyy HH:mm:ss.S',
	        showSecond:true,
			showButtonPanel : true,
			showOtherMonths : true,
	        dateFormat: 'dd/mm/yy',
	        timeFormat: 'HH:mm:ss'
	    });
		
		Ad74aUtils.configCombo($("#mensaje50_filter_table"), {
			loadFromSelect: true
		});
		
		 function htmlEntities(str) {
				        return String(str).replaceAll('<', '&lt;').replaceAll('>', '&gt');
				    }
			function entitiesHtml(str) {
			    return String(str).replaceAll('&lt;', '<').replaceAll('&gt', '>').replaceAll('\t','');
			}
			
			function prettifyXml(sourceXml){
			    sourceXml = entitiesHtml(sourceXml);
			  var xmlDoc = new DOMParser().parseFromString(sourceXml, 'application/xml');
			  var xsltDoc = new DOMParser().parseFromString([           
			    '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">',
			    '<xsl:output omit-xml-declaration="yes" indent="yes"/>',
			    '    <xsl:template match="node()|@*">',
			    '     <xsl:copy>',
			    '      <xsl:apply-templates select="node()|@*"/>',
			    '   </xsl:copy>',
			    '</xsl:template>',
			    '</xsl:stylesheet>',
			  ].join('\n'), 'application/xml');
			  var xsltProcessor = new XSLTProcessor();    
			  xsltProcessor.importStylesheet(xsltDoc);
			  var resultDoc = xsltProcessor.transformToDocument(xmlDoc);
			  var resultXml = new XMLSerializer().serializeToString(resultDoc);
			  resultXml = htmlEntities(resultXml);
			  return resultXml;
			};
		
		$.verMensaje = function(idError){
	        $('#verMensajeContent').html("");
	        $.rup_ajax({
	              url: $.rup.CTX_PATH +"jmserror/"+idError,
	              type: "GET",            
	              dataType: 'json',
	              contentType: 'application/json',
	              success: function (errorJms){
	                    var xml = htmlEntities(errorJms.mensaje50Str);
	                    var xmlPretty = prettifyXml(xml);
	                    
	                    $('#verMensajeContent').html(xmlPretty);
	                    
	                    $('#verMensaje').rup_dialog("open");
	                    
	              },
	              error: function(error){ 
	                    console.log(error);     
	              }
	        });
		};
	
		
		//Dialog
		$('#verMensaje').rup_dialog({
		    type: $.rup.dialog.DIV,
		    autoOpen: false,
		    resizable: true,
		    /*minHeight:200,       
		    maxHeight:600,
		    minWidth:850,*/
		    height:600,
			dialogClass: 'custom_mid_dialog',
		    title: $.rup.i18n.app.errores.dialogoMsg,
		    modal: true,
		    buttons: [
		    {
		        text: $.rup.i18n.app.botonera.aceptar,
		        click: function () { 
		            $('#verMensaje').rup_dialog('close'); 
		
		        }
		        
		    }
		
		    ] 
		});	
		 
	
		let tableColModels = [			
				{ 	name: "id50",
					index: "id50",
					editable: true, 
					hidden: false
				},
				{ 	name: "error50",
					index: "error50",
					editable: true, 
					hidden: false
				},
				{ 	name: "errormsg50",
					index: "errormsg50",
					editable: true, 
					hidden: false
				},
				{ 	name: "tipomensaje50",
					index: "tipomensaje50",
					editable: true, 
					hidden: false
				},
				{ 	name: "fecrecep50",
					index: "fecrecep50",
					editable: true, 
					sortorder:"desc",
					hidden: false
				},
				{ 	name: "procedure50",
					index: "procedure50",
					editable: true, 
					hidden: false
				},
				
				{ 	name: "mensaje50Str",
					index: "mensaje50Str",
					editable: true, 
					hidden: false
				},
				{ 	name: "idjms50",
					index: "idjms50",
					editable: true, 
					hidden: true
				},
				
				
				{ 	name: "estado50",
					index: "estado50",
					editable: true, 
					hidden: true
				}
				];
		let columnDefs = [
			{
				"targets": 6, 
				"orderable":false,                  
			    "width":'4%',
			    "className": "text-center",
			    "render": function ( data, type, row, meta ) {
			             return "<a href='javascript:$.verMensaje(\"" + row.id50 + "\");'><i class='mdi mdi-xml' style='margin-right:5px;'></i></a>";               
				}
			}
		];
		
	
		$("#ErroresJMS").rup_table({
			colReorder: {
			 fixedColumnsLeft: 1
			}
			,colModel: tableColModels
			,columnDefs : columnDefs
			,primaryKey: "id50"
			,filter:{
	  	  		id:"ErroresJMS_filter_form",
	  	  		filterToolbar:"ErroresJMS_filter_toolbar",
	  	  		collapsableLayerId:"ErroresJMS_filter_fieldset"
	     	}
			,noEdit: true
	   		,select: {
	       		activate: true
	   		}
			,seeker: {
				activate: false
			}
			,buttons: {
				activate: true,
				blackListButtons: ['addButton', 'editButton', 'cloneButton', 'copyButton','deleteButton','pdfButton', 'odsButton', 'csvButton', 'reportsButton'],
				myButtons:[
					{
						text: function () {
							return $.rup.i18n.app.errores.btnReintentar;
						},
						id: 'reintentarErrores', // Campo obligatorio si se
													// quiere usar desde el
													// contextMenu
						className: 'btn-material-primary-high-emphasis table_toolbar_btnView',
						icon: "mdi-play",
						displayRegex: /^[1-9][0-9]*$/, // Se muestra siempre
														// que sea un numero
														// mayor a 0
						insideContextMenu: true, // Independientemente de
													// este valor, sera 'false'
													// si no tiene un id
													// definido
						type: 'view',
						action : function(e, dt, node, config) {			
							$.rup_ajax({
					              url: $.rup.CTX_PATH +"jmserror/relanzar/"+$('#ErroresJMS').rup_table('getSelectedRows')['id50'],
					              type: "POST",            
					              dataType: 'json',
					              contentType: 'application/json',
									beforeSend: function() {
										Ad74aUtils.bloquearInterfaz();
					                },
					                complete: function() {
					                	Ad74aUtils.desBloquearInterfaz();
					                },
					              success: function (){
					                   $('#rup_feedback_ErroresJMS').rup_feedback("option", "delay", null);
										$('#rup_feedback_ErroresJMS').rup_feedback("set", $.rup.i18n.app.errores.operExito, "ok");
										
										$('#ErroresJMS').rup_table("reload");
					                    
					              },
					              error: function(error){ 
									$('#rup_feedback_ErroresJMS').rup_feedback("option", "delay", null);
									$('#rup_feedback_ErroresJMS').rup_feedback("set", error.responseText/*$.rup.i18n.app.errores.errorReintentar*/, "error");
	
					              }
					        });				
						}
					}
				]
			}
			,order: [[4, "desc"]]
		});
	});
});