
export function pagination(el, paginator, fnAction) {
    const $el = $(el ?? '#pagination');
    if (paginator.pageCount <= 1) {
        $el.addClass('d-none');
        return;
    }
    $el.removeClass('d-none');
    $(el ?? '#pagination').twbsPagination({
        totalPages: paginator.pageCount,
        visiblePages: 5,
        startPage: paginator.page,
        next: '>',
        prev: '<',
        last: '>>',
        first: '<<',
        initiateStartPageClick: false,
        onInit: function () {
            // fire first page loading
        },
        onPageClick: fnAction, //function (event, page) {}
    });

}

/**
 * 
 * @param HTMLElement|string el 
 * @param HTMLElement|string elRefresh 
 * @param {postKey?:string, path: string, pageCount: int, page: int} paginator 
 */
export function ajaxPagination(
	el,
	elRefresh,
	paginator,
	fnGetFormData,
	fnCallbackSuccess
) {
	pagination(el, paginator, function (event, page) {
		let formData = fnGetFormData();
		formData.append(paginator.postKey, page);

		$.ajax({
			url: paginator.path,
			type: "POST",
			contentType: false,
			processData: false,
			data: formData,
			success: function (htmlData) {
				desactiveLoader();
				var $elrefresh = $(elRefresh);
				var $result = $("<div/>")
					.append(htmlData)
					.find("#" + $elrefresh[0].id);
				$elrefresh.replaceWith($result);
				$result.find(".table")[0].scrollIntoView({
					behavior: "smooth",
					block: "start",
				});
				if (typeof fnCallbackSuccess === "function") {
					fnCallbackSuccess();
				}
			},
		});
	});
}