
jQuery(function ($) {
    initRupI18nPromise.then(function () {
		initFilter();
		initTable();
		
		$("#copagosseguimiento_filter_cleanButton").click(initFilter);
		
		$("#copagosseguimiento_filter_form").rup_validate({
		    liveCheckingErrors: false,
		    showFieldErrorAsDefault: true,
		    showErrorsInFeedback: true,
		    showFieldErrorsInFeedback: true,
		    rules:{
		    	"fEmisionDesde":{date:true},
		    	"fEmisionHasta":{date:true},
		    }
		});
		
		$("#copagosseguimiento_filter_form [ruptype='date']").blur(function(elem) { 

			if(!$(elem.target).valid()){
	            $(elem.target).val('')
	        }

		})
	});
	
	const initFilter = () => {
		
		$("#idTerritorio_copagosseguimiento_filter_table").rup_combo({	
			rowStriping: true,
			ordered: true,
			blank: '',
			customClasses: ['select-material'],
			loadFromSelect:true,
			width: 'auto',
			height: 'auto'
		});
		
		$.rup_date({
			//yearRange: 'c-100:c+0',
	        from: "fEmisionDesde_copagosseguimiento_filter_table",
	        to: "fEmisionHasta_copagosseguimiento_filter_table",
	        mask: 'dd/mm/yyyy',
			showButtonPanel : true,
			showOtherMonths : true,
			noWeekend : true
	    });
		
		$("#anyoPago89_pagos_filter_table").rup_combo({	
			source: $.rup.CTX_PATH + 'combo/anyosCopagos',
			sourceParam: {label: "descripcion", value:"value"},
			loadFromSelect:false,
			width: 'auto',
	        ordered: true,
			blank: '',
			customClasses: ['select-material'],
			rowStriping: true
		});
		
		$("#mesPago89_filter_table").rup_combo({
			parent: ["anyoPago89_pagos_filter_table"],			
			source: $.rup.CTX_PATH + 'combo/meses',
			sourceParam: {label: "descripcion", value:"value"},
			rowStriping: true,
			width: 'auto',
			ordered: false,
			orderedByValue: false,
			blank: '',
			customClasses: ['select-material']
		});
		
		$("#anyoPago89Hasta_pagos_filter_table").rup_combo({	
			source: $.rup.CTX_PATH + 'combo/anyosCopagos',
			sourceParam: {label: "descripcion", value:"value"},
			loadFromSelect:false,
			width: 'auto',
	        ordered: true,
			blank: '',
			customClasses: ['select-material'],
			rowStriping: true
		});
		
		$("#mesPago89Hasta_filter_table").rup_combo({
			parent: ["anyoPago89Hasta_pagos_filter_table"],			
			source: $.rup.CTX_PATH + 'combo/meses',
			sourceParam: {label: "descripcion", value:"value"},
			rowStriping: true,
			width: 'auto',
			ordered: false,
			orderedByValue: false,
			blank: '',
			customClasses: ['select-material']
		});
		
	}
	
	const initTable = () => {
		
		const getColumnsModelCopago = function(){
			const getColumnModelCopago = function (name, index, editable, hidden, orderable) {
				return {
					name: name,
					index: index,
					editable: editable,
					hidden: hidden,
					orderable: orderable
				}
			};
			let columnsModelCopago = [];
			columnsModelCopago.push(getColumnModelCopago("idLiquidacion", "idLiquidacion", false, true, false));
			columnsModelCopago.push(getColumnModelCopago("anyo", "anyo", true, false));
			columnsModelCopago.push(getColumnModelCopago("mes", "mes", true, false));
			columnsModelCopago.push(getColumnModelCopago("idTerritorio", "idTerritorio", true, false));
			columnsModelCopago.push(getColumnModelCopago("numBeneficiarios", "numBeneficiarios", true, false));
			columnsModelCopago.push(getColumnModelCopago("importeTotal", "importeTotal", true, false));
			return columnsModelCopago;
		};
		const getColumnsDefsCopago = function () {
			return [ 
				{
					"targets": 2,
					"render": function (data, type, row, meta){
						if (row.mes != undefined && row.mes != null){
							if      ( row.mes == eneroId )     	{return eneroDesc;
							}else if( row.mes == febreroId )	{return febreroDesc;
							}else if( row.mes == marzoId )		{return marzoDesc;
							}else if( row.mes == abrilId )		{return abrilDesc;
							}else if( row.mes == mayoId )		{return mayoDesc;
							}else if( row.mes == junioId )		{return junioDesc;
							}else if( row.mes == julioId )		{return julioDesc;
							}else if( row.mes == agostoId )		{return agostoDesc;
							}else if( row.mes == septiembreId )	{return septiembreDesc;
							}else if( row.mes == octubreId )	{return octubreDesc;
							}else if( row.mes == noviembreId )	{return noviembreDesc;
							}else if( row.mes == diciembreId )	{return diciembreDesc;
							}							
						}
						return "";
					}
				},
				{
					"targets": 3,
					"render": function (data, type, row, meta){
						if (row.idTerritorio != undefined && row.idTerritorio != null){
							if      ( data == TERRITORIO_SIPCA_ARABA )   	{return TERRITORIO_SIPCA_ARABA_DESC;
							}else if( data == TERRITORIO_SIPCA_GIPUZKOA )	{return TERRITORIO_SIPCA_GIPUZKOA_DESC;
							}else if( data == TERRITORIO_SIPCA_BIZKAIA )	{return TERRITORIO_SIPCA_BIZKAIA_DESC;
							}
						}
						return "";
					}
				},
				{
					"targets": 4,
					"className":"text-right",
					"render": function (data, type, row, meta){
						
						return Ad74aUtils.valorFormatoMiles(data);
					}
				},
				{
					"targets": 5,
					"className":"text-right",
					"render": function ( data, type, row, meta ) {
						if(row.importeTotal != undefined && row.importeTotal != null){
							return row.importeTotalStr;
						}
						return "";
					}
				}
			];
		};
		
		const getCustomButtonsCopago = function () {
			let customButonsCopago = [];	
					
			return customButonsCopago;
		};

		$("#copagosseguimiento").rup_table({
			colReorder: {
				fixedColumnsLeft: 1
			},
			colModel: getColumnsModelCopago(),
			columnDefs: getColumnsDefsCopago(),
			primaryKey: "anyo",
	     	filter:{
	  	  		id:"copagosseguimiento_filter_form",
	  	  		filterToolbar:"copagosseguimiento_filter_toolbar",
	  	  		collapsableLayerId:"copagosseguimiento_filter_fieldset",
	  	  		showHidden: true
	     	},
	     	createTooltipBody: false,// No muestra el tooltip automático de las columnas de la tabla
	     	noEdit: true,
	   		select: {
	       		activate: true,
	       		deleteDoubleClick:true
	   		},
			buttons: {
				activate: true,
				blackListButtons: ['addButton', 'editButton', 'cloneButton', 'deleteButton', 'copyButton', 'pdfButton', 'odsButton', 'csvButton', 'reportsButton'],
				myButtons: getCustomButtonsCopago()
			},
			columnOrderArrows: {
			    showOnlyActive: false,
			    display: 'inline'
			},
			order: [[1, "desc"], [2, "desc"], [3, "asc"]]
		});
	}

});
