 
jQuery(function($){
	initRupI18nPromise.then(function() {
		prepareData();	
	});
	
	function prepareData(){
	
		$("#sentido29_filter_table").rup_combo({
			loadFromSelect:true,
			width: 'auto',
	        ordered: false,
			customClasses: ['select-material'],
			rowStriping: true,
			blank: '',
			selected: ''
		});
		
		$("#idTipoAlarma48_filter_table").rup_combo({
			loadFromSelect:true,
			width: 'auto',
	        ordered: false,
			customClasses: ['select-material'],
			rowStriping: true,
			blank: '',
			selected: '',
			
			change:function(){
				if($("#idTipoAlarma48_filter_table").val()!=TIPOALARMA_LLAMADA){
					$("#idTipoLlamada55_filter_table").rup_combo("disable");
					$("#idTipoLlamada55_filter_table").rup_combo("clear");
				}else{
					$("#idTipoLlamada55_filter_table").rup_combo("enable");
				}
			
			}
		});
		$("#idTipoLlamada55_filter_table").rup_combo({
			loadFromSelect:true,
			width: 'auto',
	        ordered: false,
			customClasses: ['select-material'],
			rowStriping: true,
			blank: '',
			selected: '',
			onLoadSuccess:function(){
				if($("#idTipoAlarma48_filter_table").val()!=TIPOALARMA_LLAMADA){
					$("#idTipoLlamada55_filter_table").rup_combo("disable");
					$("#idTipoLlamada55_filter_table").rup_combo("clear");
				}else{
					$("#idTipoLlamada55_filter_table").rup_combo("enable");
				}
			}
		});
		$("#idSubtipoLlamada56_filter_table").rup_combo({
			parent:['idTipoLlamada55_filter_table'],
			source: $.rup.CTX_PATH + 'maestrosubtpollamada/subtipoLlamada',
			sourceParam: {label: "desc", value:"idSubtipoLlamada56"},
			loadFromSelect:false,
			width: 'auto',
	        ordered: false,
			customClasses: ['select-material'],
			rowStriping: true,
			blank: '',
			selected: ''
		});
		
		
		$("#idTipoResol24_filter_table").rup_combo({
			loadFromSelect:true,
			width: 'auto',
	        ordered: false,
			customClasses: ['select-material'],
			rowStriping: true,
			blank: '',
			selected: ''
		});
		
		$.rup_date({
			//yearRange: 'c-100:c+0',
	        from: "fAlarma24Desde_filter_table",
	        to: "fAlarma24Hasta_filter_table",
	        mask: 'dd/mm/yyyy',
			showButtonPanel : true,
			showOtherMonths : true,
			noWeekend : true
	     });

			
		
		let tableColModels = [		
				
				{ 	name: "maestroTipoAlarma.desc",
					index: "maestroTipoAlarma.desc",
					editable: true, 
					hidden: false
				},
				{ 	name: "llamadas.maestroTipoLlamada.desc",
					index: "llamadas.maestroTipoLlamada.desc",
					editable: true, 
					hidden: false
				},
				{ 	name: "llamadas.maestroSubtpoLlamada.desc",
					index: "llamadas.maestroSubtpoLlamada.desc",
					editable: true, 
					hidden: false
				},
				{ 	name: "fAlarma24",
					index: "fAlarma24",
					editable: true, 
					hidden: false
				},
				{ 	name: "llamadas.descSentido",
					index: "llamadas.descSentido",
					editable: true, 
					hidden: false
				},
				{ 	name: "idTipoResol24",
					index: "idTipoResol24",
					editable: true, 
					hidden: false
				},
				{ 	name: "llamadas.idLlamadaPs29",
					index: "llamadas.idLlamadaPs29",
					editable: true, 
					hidden: false
				},
				{ 	name: "llamadas.respuesta29",
					index: "llamadas.respuesta29",
					editable: true, 
					hidden: false
				},
				{ 	name: "llamadas.tiempoRespuesta29",
					index: "llamadas.tiempoRespuesta29",
					editable: true, 
					hidden: false
				},
				{ 	name: "llamadas.duracion29",
					index: "llamadas.duracion29",
					editable: true, 
					hidden: false
				},
				];
	
		
		let columnDefs = [ 
			{ 
				"targets": 1,
				"render": function(data, type, row, settings){	
					if(row.maestroTipoAlarma != null && row.maestroTipoAlarma.idTipoAlarma48 != TIPOALARMA_LLAMADA){
						return '';
					}
					return  row.llamadas.maestroTipoLlamada.desc;
				}
			},
			{ 
				"targets": 2,
				"render": function(data, type, row, settings){	
					if(row.maestroTipoAlarma != null && row.maestroTipoAlarma.idTipoAlarma48 != TIPOALARMA_LLAMADA){
						return '';
					}
					return  row.llamadas.maestroSubtpoLlamada.desc;
				}
			}
			,{ 
				"targets": 5,
				"render": function(data, type, row, settings){	
					if (row.idTipoResol24==1 ){
						return $.rup.i18n.app.alarma.tipoResolucionResueltasDesdeCA ;
					}else if(row.idTipoResol24==2){
						return $.rup.i18n.app.alarma.tipoResolucionIntervencionDomiciliaria ;
					}else if(row.idTipoResol24==3){
						return $.rup.i18n.app.alarma.tipoResolucionEntornoSociofamiliar;
					}else if(row.idTipoResol24==4){
						return $.rup.i18n.app.alarma.tipoResolucionServiciosDeEmergencia;
					}
					
					return "";
				}
			},
			{ 
				"targets": 7,
				"render": function(data, type, row, settings){	
//					if( data == 1 ){
//						return  `<input type="checkbox" checked="checked" disabled/>`;
//					}
//					return  `<input type="checkbox" disabled/>`;
					if( data == 1 ){
						return $.rup.i18n.app.comun.si;
					}else{
						return $.rup.i18n.app.comun.no;
					}

				}
			},
			{ 
				"targets": [8, 9],
				"render": function(data, type, row, settings){
					console.log(data);
				    let horas = Math.floor(data / 60);
				    let minutos = data - (horas * 60);
				    return `${horas}:${String(minutos).padStart(2, '0')}`;
				}
			}
			
		];
		$("#alarmas").rup_table({
			colReorder: {
			 fixedColumnsLeft: 1
			}
			,colModel: tableColModels
			,columnDefs : columnDefs
			,primaryKey: "idAlarma24"
			,filter:{
	  	  		id:"alarmas_filter_form",
	  	  		filterToolbar:"alarmas_filter_toolbar",
	  	  		collapsableLayerId:"alarmas_filter_fieldset",
	  	  		showHidden: true
	     	}
			,noEdit : true
       		,select: {
           		activate: true
       		}
    		,seeker: {
    			activate: true
			}
    		,buttons: {
				activate: true,
				contextMenu: false,
				blackListButtons: ['addButton', 'editButton', 'cloneButton', 'copyButton','deleteButton','pdfButton', 'odsButton', 'csvButton'],
			},
			columnOrderArrows: {
			    showOnlyActive: false,
			    display: 'inline'
			}
			,order: [[0, "asc"],[3, "des"]]
		});
	}
	
});