
jQuery(function($) {
	
	initRupI18nPromise.then(function () {
		initFilter();
		initTable();
	});
	
	const initFilter = () => {
		
		var properties={
				feedback:$("#maestronivsoporte_detail_feedback"),
				liveCheckingErrors: false,
				showFieldErrorAsDefault: true,
				showErrorsInFeedback: true,
				showFieldErrorsInFeedback: true,
			  rules:{
			      "descEs58":{required:true},
			      "descEu58":{required:true}
			 }
		};
		$("#maestronivsoporte_detail_form").rup_validate(properties);
		
		let maestronivsoporte_dialog = $("#maestronivsoporte_detail_div");
		let maestronivsoporte_dialog_feedback = $("#maestronivsoporte_detail_feedback");
		
		try {
			maestronivsoporte_dialog.rup_dialog("destroy");
		} catch (error) {}
		
		maestronivsoporte_dialog = $("#maestronivsoporte_detail_div");
		maestronivsoporte_dialog_feedback = $("#maestronivsoporte_detail_feedback");
		
		maestronivsoporte_dialog_feedback.rup_feedback (
			{
			  type: "ok",
			  block: false,
			}
		);	
		
		maestronivsoporte_dialog.rup_dialog( {
			type: $.rup.dialog.DIV,
			autoOpen: false,
			modal: true,
			closeOnEscape: true,
			resizable: true,
			//width:600,
			//height:600,
			dialogClass: 'custom_small_dialog',
			title: $.rup.i18n.app.botonera.add,
			buttons : [ 
                {
                    text:$.rup.i18n.app.botonera.aceptar,
                    id:"maestronivsoporte_aceptar_btn",
                    class: 'btn-material btn-material-primary-high-emphasis',
                    click:  () => {
                    	if ($("#maestronivsoporte_detail_form").valid()){
                    		const maestronivsoporte = $("#maestronivsoporte").rup_table("getSelectedRows");

                    		let data =  $('#maestronivsoporte_detail_form').rup_form("formToJson");
                    		
                    		let url = $('#maestronivsoporte_detail_form').attr("action") + "/";
                    		url += data["idNivelSoporte58"] != null && data["idNivelSoporte58"] != "" ? "edit" : "add";
                    		let type = data["idNivelSoporte58"] != null && data["idNivelSoporte58"] != "" ? "PUT" : "POST";
                    		
                        	$.rup_ajax({
        			            url: url,
                        	    type: type,
        			            contentType: "application/json",
        			            data: JSON.stringify(data),             	
        			            success: function (data){    			            	
        			            	$("#maestronivsoporte").rup_table("reload");
        			            	maestronivsoporte_dialog.rup_dialog("close");
        			            	Ad74aUtils.desBloquearInterfaz();
        			            	$("#rup_feedback_maestronivsoporte").rup_feedback('set', $.rup.i18n.app.feedback.operacionOK, "ok"); 
        			            },
        			            beforeSend: function(){
        			            	Ad74aUtils.cerrarFeedbacks();
        			            	Ad74aUtils.bloquearInterfaz();
        						},
        			            error:function(data){
        			            	Ad74aUtils.desBloquearInterfaz();
        			            	maestronivsoporte_dialog_feedback.rup_feedback('set', data.responseText, "error");
        			            }
        			        });
                    	}
 					}
                 },
				{
					text : $.rup.i18n.app.botonera.cancelar,
					click : () => {
						maestronivsoporte_dialog.rup_dialog("close");
					},
					btnType : $.rup.dialog.LINK
				}
			],
			open: function(){
				const maestronivsoporte = $("#maestronivsoporte").rup_table("getSelectedRows");  
			},
			close: function(){
				$("#maestronivsoporte_detail_form").rup_form("clearForm");
				$("#maestronivsoporte_detail_form").rup_validate("resetElements");
				maestronivsoporte_dialog_feedback.rup_feedback("close");
			}		    
		});
	}
	
	const initTable = () => {
		
		let maestronivsoporte_dialog = $("#maestronivsoporte_detail_div");
		let maestronivsoporte_dialog_feedback = $("#maestronivsoporte_detail_feedback");
		
		const getColumnsModel = function(){
			const getColumnModel = function (name, index, editable, hidden, orderable) {
				return {
					name: name,
					index: index,
					editable: editable,
					hidden: hidden,
					orderable: orderable
				}
			};
			let columnsModel = [];
			columnsModel.push(getColumnModel("idNivelSoporte58", "idNivelSoporte58", false, true, false));
			columnsModel.push(getColumnModel("descEs58", "descEs58", true, false));
			columnsModel.push(getColumnModel("descEu58", "descEu58", true, false));
			columnsModel.push(getColumnModel("fAlta58", "fAlta58", false, true));
			columnsModel.push(getColumnModel("usuAlta58", "usuAlta58", false, true));
			columnsModel.push(getColumnModel("fModif58", "fModif58", false, true));
			columnsModel.push(getColumnModel("usuModif58", "usuModif58", false, true));
			return columnsModel;
		};

		const getColumnsDefs = function () {
			return [ 
				
			];
		};
		
		const getCustomButtons = function () {
			let customButons = [];	
			
			const FN_editarBorrar = {};
			FN_editarBorrar.test  = function(){
				const selectedRows  = $("#maestronivsoporte").rup_table("getSelectedRows");	
				// 1 seleccionado
				if ( selectedRows.length == undefined) {
					return true;
				}
				return false;
			};
			const anadirButton = {
					text: function () {
						return $.rup.i18n.app.botonera.add;
					},
					id: 'anadirmaestronivsoporte', // Campo obligatorio si se quiere usar desde el contextMenu
					className: 'btn-material-primary-high-emphasis table_toolbar_btnView',
					icon: "mdi-plus",
					displayRegex: /^\d+$/,			
					insideContextMenu: true, 
					//type: 'view',
					action: function (e, dt) {
						maestronivsoporte_dialog.rup_dialog("open");
						const divTitleDialog = $("div[aria-describedby='maestronivsoporte_detail_div'] .ui-dialog-title");
						divTitleDialog.html('<i>'+$.rup.i18n.base.rup_table.toolbar.add+'</i>');
						$("#idNivelSoporte58_maestronivsoporte_detail_table").val("");
						maestronivsoporte_dialog_feedback.rup_feedback("close");
					}				
			};	
			customButons.push(anadirButton);
			
			const editButton = {
					text: function () {
						return $.rup.i18n.app.botonera.edit;
					},
					id: 'editarmaestronivsoporte', // Campo obligatorio si se quiere usar desde el contextMenu
					className: 'btn-material-primary-high-emphasis table_toolbar_btnView',
					icon: "mdi-playlist-edit",
					displayRegex: FN_editarBorrar,			
					insideContextMenu: true, 
					//type: 'view',
					action: function (e, dt) {
						$.rup_ajax({
				              url: $.rup.CTX_PATH +"maestronivsoporte/"+ $('#maestronivsoporte').rup_table('getSelectedRows')['idNivelSoporte58'],
				              type: "GET",            
				              dataType: 'json',
				              contentType: 'application/json',
				              beforeSend: function() {
				            	  Ad74aUtils.bloquearInterfaz();
				              },
				              complete: function() {
				            	  Ad74aUtils.desBloquearInterfaz();
				              },
				              success: function (data){
				              	 maestronivsoporte_dialog.rup_dialog("open");
				              	 const divTitleDialog = $("div[aria-describedby='maestronivsoporte_detail_div'] .ui-dialog-title");
								 divTitleDialog.html('<i>'+$.rup.i18n.base.rup_table.toolbar.edit+'</i>');
				              	 $.rup_utils.populateForm($.rup_utils.jsontoarray(data), "#maestronivsoporte_detail_form");	
				              },
				              error: function(error){ 
				                  alert("error")
				              }
				        });
					}		
			};
			customButons.push(editButton);
			
			const eliminarButton = {
					text: function () {
						return $.rup.i18n.app.botonera.eliminar;
					},
					id: 'eliminarmaestronivsoporte', // Campo obligatorio si se quiere usar desde el contextMenu
					className: 'btn-material-primary-high-emphasis table_toolbar_btnView',
					icon: "mdi-delete",
					displayRegex: FN_editarBorrar,			
					insideContextMenu: true, 
					//type: 'view',
					action: function (e, dt) {
						Ad74aUtils.openConfirmDialog(
							'dialogConfirm', 
							$.rup.i18n.app.dialog.title.borrarMaestronivsoporte, 
							$.rup.i18n.app.dialog.label.borrarMaestronivsoporte,  
							[
					        	{
					                text: $.rup.i18n.app.botonera.cancelar,
					                click: function () { 
					                    $('#dialogConfirm').rup_dialog('close'); 
					                },
					                btnType: $.rup.dialog.LINK
					            },
						        {
						            text: $.rup.i18n.app.botonera.aceptar,
						            id: "btnEliminarmaestronivsoporte",
						            class: 'btn-material btn-material-primary-high-emphasis',
						            click: function () { 
						            	$.rup_ajax({
								              url: $.rup.CTX_PATH +"maestronivsoporte/"+ $('#maestronivsoporte').rup_table('getSelectedRows')['idNivelSoporte58'],
								              type: "DELETE",            
								              dataType: 'json',
								              contentType: 'application/json',
								              beforeSend: function() {
								            	  Ad74aUtils.bloquearInterfaz();
								              },
								              complete: function() {
								            	  Ad74aUtils.desBloquearInterfaz();
								            	  $('#dialogConfirm').rup_dialog('close');
								              },
								              success: function (data){
								              	  $('#rup_feedback_maestronivsoporte').rup_feedback("option", "delay", null);
								              	  $('#rup_feedback_maestronivsoporte').rup_feedback("set", $.rup.i18n.app.feedback.eliminadoOk, "ok");
								              	  $('#maestronivsoporte').rup_table("reload");
								              },
								              error: function(error){ 
								                  alert("error")
								              }
								        });
						            }
						        }
					        ]
						);
					}		
			};
			customButons.push(eliminarButton);
			
			return customButons;
		};

		$("#maestronivsoporte").rup_table({
			colReorder: {
				fixedColumnsLeft: 1
			},
			colModel: getColumnsModel(),
			columnDefs: getColumnsDefs(),
			primaryKey: "idNivelSoporte58",
	     	filter:{
	  	  		id:"maestronivsoporte_filter_form",
	  	  		filterToolbar:"maestronivsoporte_filter_toolbar",
	  	  		collapsableLayerId:"maestronivsoporte_filter_fieldset",
	  	  		showHidden: true
	     	},
	     	createTooltipBody: false,// No muestra el tooltip automático de las columnas de la tabla
	   		select: {
	       		activate: true,
	       		deleteDoubleClick:true
	   		},
			buttons: {
				activate: true,
				blackListButtons: ['reportsButton', 'addButton', 'editButton', 'cloneButton', 'deleteButton', 'copyButton'],
				myButtons: getCustomButtons()
			},
			columnOrderArrows: {
			    showOnlyActive: false,
			    display: 'inline'
			},
			order: [[1, "asc"]]
		});
		
	}
	
});
