(function($){'use strict';$(document).ready(function(){initHistoryPagination();wrAddShortDate();wrEnableGridCollapse();let resizeTimer;$(window).on('resize',function(){clearTimeout(resizeTimer);resizeTimer=setTimeout(function(){wrAddShortDate();wrEnableGridCollapse()},250)})});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','');wrReapplyCollapsedState();wrAddShortDate();$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)}
function wrAddShortDate(){$('.wr-date-display').each(function(){const $dateSpan=$(this);const rawDate=$dateSpan.data('date');const isMobile=window.innerWidth<=768;if(!rawDate)return;const dateObj=new Date(rawDate);if(isNaN(dateObj.getTime()))return;let formattedDate;if(isMobile){formattedDate=new Intl.DateTimeFormat(navigator.language,{year:'numeric',month:'2-digit',day:'2-digit'}).format(dateObj)}else{formattedDate=$dateSpan.text().trim();if(!$dateSpan.data('original-text')){$dateSpan.data('original-text',formattedDate)}else{formattedDate=$dateSpan.data('original-text')}}
$dateSpan.text(formattedDate)})}
function wrEnableGridCollapse(){const isMobile=window.innerWidth<=768;$('.wr-history-grid').each(function(){const $grid=$(this);if(!$grid.data('original-grid-columns')){const originalCols=$grid.css('grid-template-columns');$grid.data('original-grid-columns',originalCols)}
if(isMobile){$grid.find('.history-grid-title').each(function(){const $header=$(this);if(!$header.find('.wr-tap-hint').length){$header.append('<span class="wr-tap-hint">◄</span>');$header.css('cursor','pointer')}})}else{$grid.find('.history-grid-title .wr-tap-hint').remove();$grid.find('.history-grid-title').css('cursor','');$grid.find('.history-grid-title.column-collapsed').each(function(){const $header=$(this);const columnClass=$header.attr('class').split(' ').find(c=>['date','system','descr','points','total'].includes(c));if(columnClass){const $columnCells=$grid.find('.cell.'+columnClass);$columnCells.css({'width':'','min-width':'','max-width':'','height':'','min-height':'','padding':'','margin':'','border':'','overflow':'','visibility':'','font-size':'','line-height':''});$header.removeClass('column-collapsed')}});const originalCols=$grid.data('original-grid-columns');if(originalCols){$grid.css('grid-template-columns',originalCols)}}});jQuery('.wr-history-grid').off('click.wrcol');if(isMobile){jQuery('.wr-history-grid').on('click.wrcol','.history-grid-title',function(){const $header=jQuery(this);const $grid=$header.closest('.wr-history-grid');const columnClass=$header.attr('class').split(' ').find(c=>['date','system','descr','points','total'].includes(c));if(!columnClass)return;const $columnCells=$grid.find('.cell.'+columnClass);const $arrow=$header.find('.wr-tap-hint');if(!$header.data('original-text')){const originalText=$header.contents().filter(function(){return this.nodeType===3}).text();$header.data('original-text',originalText)}
if(!$header.hasClass('column-collapsed')){$columnCells.css({'width':'0','min-width':'0','max-width':'0','height':'0','min-height':'0','padding':'0','margin':'0','border':'0','overflow':'hidden','visibility':'hidden','font-size':'0','line-height':'0'});$header.addClass('column-collapsed');$header.contents().filter(function(){return this.nodeType===3}).remove();$arrow.text('►')}else{$columnCells.css({'width':'','min-width':'','max-width':'','height':'','min-height':'','padding':'','margin':'','border':'','overflow':'','visibility':'','font-size':'','line-height':''});$header.removeClass('column-collapsed');const originalText=$header.data('original-text');$header.prepend(originalText);$arrow.text('◄')}
updateGridColumns($grid)})}
function updateGridColumns($grid){const $headers=$grid.find('.history-grid-title');const columnWidths=[];$headers.each(function(){const $h=$(this);if($h.hasClass('column-collapsed')){columnWidths.push('30px')}else{columnWidths.push('auto')}});$grid.css('grid-template-columns',columnWidths.join(' '))}
window.wrReapplyCollapsedState=function(){$('.wr-history-grid').each(function(){const $grid=$(this);const $headers=$grid.find('.history-grid-title');$headers.each(function(){const $header=$(this);if($header.hasClass('column-collapsed')){const columnClass=$header.attr('class').split(' ').find(c=>['date','system','descr','points','total'].includes(c));if(columnClass){const $columnCells=$grid.find('.cell.'+columnClass);$columnCells.css({'width':'0','min-width':'0','max-width':'0','height':'0','min-height':'0','padding':'0','margin':'0','border':'0','overflow':'hidden','visibility':'hidden','font-size':'0','line-height':'0'})}}});updateGridColumns($grid)})}}})(jQuery)