(function($){'use strict';$(document).ready(function(){initHistoryPagination()});function initHistoryPagination(){$(document).on('click','.wr-history-page-btn:not(.active):not(:disabled)',function(e){e.preventDefault();var $btn=$(this);var page=parseInt($btn.data('page'))||1;var $wrapper=$btn.closest('.wr-history-wrapper');var $grid=$wrapper.find('.wr-history-grid');var $pagination=$wrapper.find('.wr-history-pagination');if($wrapper.hasClass('loading')){return}
var userId=$wrapper.data('user-id');var nonce=$wrapper.data('nonce');var perPage=$wrapper.data('per-page');var system=$wrapper.data('system');var columns=$wrapper.data('columns');var headers=$wrapper.data('headers');$wrapper.addClass('loading');$pagination.addClass('loading');var currentHeight=$grid.outerHeight();$grid.css('min-height',currentHeight+'px');var ajaxData={action:lwsWooRewardsHistory.action,nonce:nonce,page:page,user_id:userId,atts:{per_page:perPage,system:system,columns:columns,headers:headers}};$.ajax({url:lwsWooRewardsHistory.ajaxUrl,type:'POST',data:ajaxData,dataType:'json',success:function(response){if(response.success&&response.data){if(response.data.html){var $dataCells=$grid.find('.cell');$grid.fadeOut(200,function(){$dataCells.remove();$grid.append(response.data.html);$grid.css('min-height','');$grid.fadeIn(200)})}
if(response.data.pagination){var $oldPagination=$wrapper.find('.wr-history-pagination');var $newPagination=$(response.data.pagination);$oldPagination.replaceWith($newPagination)}else{$pagination.remove()}}else{var errorMsg=response.data&&response.data.message?response.data.message:lwsWooRewardsHistory.errorText;showError($wrapper,errorMsg)}
$wrapper.removeClass('loading')},error:function(xhr,status,error){console.error('History Pagination AJAX Error:',status,error);var errorMsg=lwsWooRewardsHistory.errorText;try{var response=JSON.parse(xhr.responseText);if(response&&response.data&&response.data.message){errorMsg=response.data.message}}catch(e){if(xhr.responseText&&console&&console.error){console.error('Server error:',xhr.responseText.substring(0,500))}}
showError($wrapper,errorMsg);$wrapper.removeClass('loading');var $currentPagination=$wrapper.find('.wr-history-pagination');$currentPagination.removeClass('loading');$grid.css('min-height','')}})})}
function showError($wrapper,message){$wrapper.find('.wr-history-error').remove();var $errorDiv=$('<div class="wr-history-error">'+message+'</div>');$wrapper.prepend($errorDiv);setTimeout(function(){$errorDiv.fadeOut(300,function(){$(this).remove()})},5000)}})(jQuery)