const listarCargos = () => {
	$(`#cboCargo`).select2({
		containerCssClass: 'select-xs',
		ajax: {
			url: baseURLRest + URI_CARGOS,
			dataType: `json`,
			delay: 1000,
			data: function (params) {
				return {
					q: params.term, // search term
					page: params.page
				};
			},
			processResults: function (data, params) {
				// parse the results into the format expected by Select2
				// since we are using custom formatting functions we do not need to
				// alter the remote JSON data, except to indicate that infinite
				// scrolling can be used
				params.page = params.page || 1;

				return {
					results: data,
					pagination: {
						more: (params.page * 30) < data.length
					}
				};
			},
			cache: true
		},
		placeholder: `Buscar`,
		allowClear: true,
		escapeMarkup: function (markup) {
			return markup;
		}, // let our custom formatter work
		minimumInputLength: 1,
		templateResult: formatRepo,
		templateSelection: formatRepoSelection
	});

	function formatRepo(repo) {
		if (repo.loading) {
			return repo.text;
		}

		var markup = `
			<div class='select2-result-repository clearfix'>
				<div class='select2-result-repository__title'>${repo.nombre}</div>
			</div>`;

		return markup;
	}

	function formatRepoSelection(repo) {
		if (repo.id) {
			return repo.nombre;
		} else {
			return repo.text;
		}
	}
};


const formControls = {
	init() {
		this.initDatePickers()
		this.initSedes()
		this.initCargos()
	},
	initDatePickers() {
		initDatePicker("#dpFechaInicio", "#dpFechaFin")
	},
	initSedes() {
		listarSedes("#cboSede");
	},
	initCargos() {
		listarCargos()
	}
}

const registrar = () => {
	let sede_id
	let cargo_id
	let fecha_inicio
	let fecha_fin

	swal({
		type: 'success',
		title: '¡Vacaciones masivas registradas exitosamente!',
		showConfirmButton: false,
		timer: 1500
	});

}

$(document).ready(function () {
	formControls.init()
	$("#btnRegistrar").click(registrar)
})