
jQuery(function($) {
	
	initRupI18nPromise.then(function () {
		initFilter();
		initTable();
	});
	
	const initFilter = () => {
		
		var properties={
				feedback:$("#maestrotipoalarma_detail_feedback"),
				liveCheckingErrors: false,
				showFieldErrorAsDefault: true,
				showErrorsInFeedback: true,
				showFieldErrorsInFeedback: true,
			  rules:{
			      "descEs48":{required:true},
			      "descEu48":{required:true}
			 }
		};
		$("#maestrotipoalarma_detail_form").rup_validate(properties);
		
		let maestrotipoalarma_dialog = $("#maestrotipoalarma_detail_div");
		let maestrotipoalarma_dialog_feedback = $("#maestrotipoalarma_detail_feedback");
		
		try {
			maestrotipoalarma_dialog.rup_dialog("destroy");
		} catch (error) {}
		
		maestrotipoalarma_dialog = $("#maestrotipoalarma_detail_div");
		maestrotipoalarma_dialog_feedback = $("#maestrotipoalarma_detail_feedback");
		
		maestrotipoalarma_dialog_feedback.rup_feedback (
			{
			  type: "ok",
			  block: false,
			}
		);	
		
		maestrotipoalarma_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:"maestrotipoalarma_aceptar_btn",
                    class: 'btn-material btn-material-primary-high-emphasis',
                    click:  () => {
                    	if ($("#maestrotipoalarma_detail_form").valid()){
                    		const maestrotipoalarma = $("#maestrotipoalarma").rup_table("getSelectedRows");

                    		let data =  $('#maestrotipoalarma_detail_form').rup_form("formToJson");
                    		
                    		let url = $('#maestrotipoalarma_detail_form').attr("action") + "/";
                    		url += data["idTipoAlarma48"] != null && data["idTipoAlarma48"] != "" ? "edit" : "add";
                    		let type = data["idTipoAlarma48"] != null && data["idTipoAlarma48"] != "" ? "PUT" : "POST";
                    		
                        	$.rup_ajax({
        			            url: url,
                        	    type: type,
        			            contentType: "application/json",
        			            data: JSON.stringify(data),             	
        			            success: function (data){    			            	
        			            	$("#maestrotipoalarma").rup_table("reload");
        			            	maestrotipoalarma_dialog.rup_dialog("close");
        			            	Ad74aUtils.desBloquearInterfaz();
        			            	$("#rup_feedback_maestrotipoalarma").rup_feedback('set', $.rup.i18n.app.feedback.operacionOK, "ok"); 
        			            },
        			            beforeSend: function(){
        			            	Ad74aUtils.cerrarFeedbacks();
        			            	Ad74aUtils.bloquearInterfaz();
        						},
        			            error:function(data){
        			            	Ad74aUtils.desBloquearInterfaz();
        			            	maestrotipoalarma_dialog_feedback.rup_feedback('set', data.responseText, "error");
        			            }
        			        });
                    	}
 					}
                 },
				{
					text : $.rup.i18n.app.botonera.cancelar,
					click : () => {
						maestrotipoalarma_dialog.rup_dialog("close");
					},
					btnType : $.rup.dialog.LINK
				}
			],
			open: function(){
				const maestrotipoalarma = $("#maestrotipoalarma").rup_table("getSelectedRows");  
			},
			close: function(){
				$("#maestrotipoalarma_detail_form").rup_form("clearForm");
				$("#maestrotipoalarma_detail_form").rup_validate("resetElements");
				maestrotipoalarma_dialog_feedback.rup_feedback("close");
			}		    
		});
	}
	
	const initTable = () => {
		
		let maestrotipoalarma_dialog = $("#maestrotipoalarma_detail_div");
		let maestrotipoalarma_dialog_feedback = $("#maestrotipoalarma_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("idTipoAlarma48", "idTipoAlarma48", false, true, false));
			columnsModel.push(getColumnModel("descEs48", "descEs48", true, false));
			columnsModel.push(getColumnModel("descEu48", "descEu48", true, false));
			columnsModel.push(getColumnModel("fAlta48", "fAlta48", false, true));
			columnsModel.push(getColumnModel("usuAlta48", "usuAlta48", false, true));
			columnsModel.push(getColumnModel("fModif48", "fModif48", false, true));
			columnsModel.push(getColumnModel("usuModif48", "usuModif48", false, true));
			return columnsModel;
		};

		const getColumnsDefs = function () {
			return [ 
			];
		};
		
		const getCustomButtons = function () {
			let customButons = [];	
			
			const FN_editarBorrar = {};
			FN_editarBorrar.test  = function(){
				const selectedRows  = $("#maestrotipoalarma").rup_table("getSelectedRows");	
				// 1 seleccionado
				if ( selectedRows.length == undefined) {
					return true;
				}
				return false;
			};
			const anadirButton = {
					text: function () {
						return $.rup.i18n.app.botonera.add;
					},
					id: 'anadirmaestrotipoalarma', // 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) {
						maestrotipoalarma_dialog.rup_dialog("open");
						const divTitleDialog = $("div[aria-describedby='maestrotipoalarma_detail_div'] .ui-dialog-title");
						divTitleDialog.html('<i>'+$.rup.i18n.base.rup_table.toolbar.add+'</i>');
						$("#idTipoAlarma48_maestrotipoalarma_detail_table").val("");
						maestrotipoalarma_dialog_feedback.rup_feedback("close");
					}				
			};	
			customButons.push(anadirButton);
			
			const editButton = {
					text: function () {
						return $.rup.i18n.app.botonera.edit;
					},
					id: 'editarmaestrotipoalarma', // 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 +"maestrotipoalarma/"+ $('#maestrotipoalarma').rup_table('getSelectedRows')['idTipoAlarma48'],
				              type: "GET",            
				              dataType: 'json',
				              contentType: 'application/json',
				              beforeSend: function() {
				            	  Ad74aUtils.bloquearInterfaz();
				              },
				              complete: function() {
				            	  Ad74aUtils.desBloquearInterfaz();
				              },
				              success: function (data){
				              	 maestrotipoalarma_dialog.rup_dialog("open");
				              	 const divTitleDialog = $("div[aria-describedby='maestrotipoalarma_detail_div'] .ui-dialog-title");
								 divTitleDialog.html('<i>'+$.rup.i18n.base.rup_table.toolbar.edit+'</i>');
				              	 $.rup_utils.populateForm($.rup_utils.jsontoarray(data), "#maestrotipoalarma_detail_form");	
				              },
				              error: function(error){ 
				                  alert("error")
				              }
				        });
					}		
			};
			customButons.push(editButton);
			
			const eliminarButton = {
					text: function () {
						return $.rup.i18n.app.botonera.eliminar;
					},
					id: 'eliminarmaestrotipoalarma', // 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.borrarMaestrotipoalarma, 
							$.rup.i18n.app.dialog.label.borrarMaestrotipoalarma,  
							[
					        	{
					                text: $.rup.i18n.app.botonera.cancelar,
					                click: function () { 
					                    $('#dialogConfirm').rup_dialog('close'); 
					                },
					                btnType: $.rup.dialog.LINK
					            },
						        {
						            text: $.rup.i18n.app.botonera.aceptar,
						            id: "btnEliminarmaestrotipoalarma",
						            class: 'btn-material btn-material-primary-high-emphasis',
						            click: function () { 
						            	$.rup_ajax({
								              url: $.rup.CTX_PATH +"maestrotipoalarma/"+ $('#maestrotipoalarma').rup_table('getSelectedRows')['idTipoAlarma48'],
								              type: "DELETE",            
								              dataType: 'json',
								              contentType: 'application/json',
								              beforeSend: function() {
								            	  Ad74aUtils.bloquearInterfaz();
								              },
								              complete: function() {
								            	  Ad74aUtils.desBloquearInterfaz();
								            	  $('#dialogConfirm').rup_dialog('close');
								              },
								              success: function (data){
								              	  $('#rup_feedback_maestrotipoalarma').rup_feedback("option", "delay", null);
								              	  $('#rup_feedback_maestrotipoalarma').rup_feedback("set", $.rup.i18n.app.feedback.eliminadoOk, "ok");
								              	  $('#maestrotipoalarma').rup_table("reload");
								              },
								              error: function(error){ 
								                  alert("error")
								              }
								        });
						            }
						        }
					        ]
						);
					}		
			};
			customButons.push(eliminarButton);
			
			return customButons;
		};

		$("#maestrotipoalarma").rup_table({
			colReorder: {
				fixedColumnsLeft: 1
			},
			colModel: getColumnsModel(),
			columnDefs: getColumnsDefs(),
			primaryKey: "idTipoAlarma48",
	     	filter:{
	  	  		id:"maestrotipoalarma_filter_form",
	  	  		filterToolbar:"maestrotipoalarma_filter_toolbar",
	  	  		collapsableLayerId:"maestrotipoalarma_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"]]
		});
		
	}
	
});
