(function($){
'use strict';
var priceUpdated=false;
var lastUpdateDate=null;
function updateSeasonalPrice(productId, selectedDate){
if(priceUpdated&&lastUpdateDate===selectedDate){
return;
}
var $bookingInfo=$('#booking_car_info, .tm_booking_car_info, .auto-price-info');
var $debugBlock=$('.tmb-seasonal-debug-fixed, .tmb-seasonal-debug-shortcode, [style*="background: #e3f2fd"], [style*="background: #fff3cd"]');
$bookingInfo.addClass('tmbx-opacity-loading').removeClass('tmbx-opacity-ready');
$('.auto-price-info').addClass('tmbx-opacity-loading').removeClass('tmbx-opacity-ready');
$.ajax({
url: tm_booking_ajax.url,
type: 'POST',
data: {
action: 'tmbooking_get_seasonal_price',
product_id: productId,
start_date: selectedDate
},
success: function(response){
if(response.success&&response.data){
var data=response.data;
var updatedCount=0;
$('.equipment-order__price .current-price .prc.currency_left').each(function(){
var oldPrice=$(this).text();
$(this).text(data.price);
updatedCount++;
});
$('.car_premium_price li').each(function(){
var $li=$(this);
var text=$li.text();
if(text.indexOf('Day')!==-1||text.indexOf('day')!==-1){
var $priceSpan=$li.find('.prc.currency_left');
if($priceSpan.length > 0){
var oldPrice=$priceSpan.text();
$priceSpan.text(data.price);
updatedCount++;
}}
});
$('.prc.currency_left[data-symbol]').each(function(){
var $el=$(this);
var parentText=$el.parent().text();
if(parentText.indexOf('Day')!==-1||parentText.indexOf('/Day')!==-1 ||
$el.closest('.list-grid-item-show-price').length > 0){
var oldPrice=$el.text();
$el.text(data.price);
updatedCount++;
}});
if($debugBlock.length > 0){
updateDebugBlock($debugBlock, data);
}
priceUpdated=true;
lastUpdateDate=selectedDate;
$bookingInfo.removeClass('tmbx-opacity-loading')
.addClass('tmbx-opacity-ready');
$('.auto-price-info').removeClass('tmbx-opacity-loading')
.addClass('tmbx-opacity-ready');
$(document).trigger('tmb_seasonal_price_updated', [data]);
}else{
console.error('Failed to get seasonal price:', response);
$bookingInfo.removeClass('tmbx-opacity-loading')
.addClass('tmbx-opacity-ready');
$('.auto-price-info').removeClass('tmbx-opacity-loading')
.addClass('tmbx-opacity-ready');
}},
error: function(xhr, status, error){
console.error('AJAX error:', error);
$bookingInfo.removeClass('tmbx-opacity-loading')
.addClass('tmbx-opacity-ready');
$('.auto-price-info').removeClass('tmbx-opacity-loading')
.addClass('tmbx-opacity-ready');
}});
}
function updateDebugBlock($block, data){
$block.find('div').each(function(){
var $div=$(this);
var text=$div.text();
if(text.indexOf('Start Date')!==-1){
$div.html('<strong>Start Date (Selected):</strong> ' + data.start_date);
}else if(text.indexOf('End Date')!==-1){
$div.html('<strong>End Date (Tomorrow):</strong> ' + data.end_date);
}else if(text.indexOf('After Seasonal Filter')!==-1){
$div.html('<strong>After Seasonal Filter:</strong> ' + data.price + data.currency_symbol);
}else if(text.indexOf('Seasonal Delta')!==-1){
var deltaSign=data.seasonal_delta >=0 ? '+':'';
var deltaColor=data.seasonal_delta >=0 ? 'green':'red';
$div.html('<strong>Seasonal Delta:</strong> <span style="color: ' + deltaColor + ';">' + deltaSign + data.seasonal_delta + data.currency_symbol + '</span>');
}else if(text.indexOf('Final')!==-1&&text.indexOf('price')!==-1){
$div.html('<strong>Final Display Price:</strong> <span style="color: green; font-size: 14px; font-weight: bold;">' + data.price + data.currency_symbol + '/day</span>');
}});
var $gradientBlock=$block.find('[style*="linear-gradient"]');
if($gradientBlock.length > 0){
$gradientBlock.find('span[style*="font-size: 24px"]').html(data.price + data.currency_symbol + '/day');
}}
function getCurrentDate(format){
var now=new Date();
var day=String(now.getDate()).padStart(2, '0');
var month=String(now.getMonth() + 1).padStart(2, '0');
var year=now.getFullYear();
if(format==='DD/MM/YYYY'||format==='dd/mm/yyyy'){
return day + '/' + month + '/' + year;
}else if(format==='MM/DD/YYYY'||format==='mm/dd/yyyy'){
return month + '/' + day + '/' + year;
}else if(format==='YYYY-MM-DD'||format==='yyyy-mm-dd'){
return year + '-' + month + '-' + day;
}else{
return month + '/' + day + '/' + year;
}}
function checkAndUpdatePriceOnLoad(){
var productId=null;
var dateFormat='MM/DD/YYYY';
var currentDate=null;
if(typeof tm_booking_seasonal!=='undefined'&&tm_booking_seasonal.product_id){
productId=tm_booking_seasonal.product_id;
dateFormat=tm_booking_seasonal.date_format||'MM/DD/YYYY';
currentDate=tm_booking_seasonal.current_date;
}
if(!productId){
var $form=$('form[class*="tm_booking"], form[id*="booking"]').first();
if($form.length > 0){
productId=$form.find('input[name="id"]').val()||$form.data('id');
}}
if(!productId){
productId=$('[data-product-id]').first().data('product-id');
}
if(!productId){
productId=$('input[name="product_id"], input[name="transport_id"]').first().val();
}
if(!productId&&typeof tm_booking_product_id!=='undefined'){
productId=tm_booking_product_id;
}
if(productId){
if(!currentDate){
dateFormat=$('[class*="tm_booking_date"]').first().data('date-format')||dateFormat;
currentDate=getCurrentDate(dateFormat);
}
updateSeasonalPrice(productId, currentDate);
}else{
}}
$(document).ready(function(){
setTimeout(function(){
checkAndUpdatePriceOnLoad();
}, 500);
setTimeout(function(){
checkAndUpdatePriceOnLoad();
}, 1500);
setTimeout(function(){
checkAndUpdatePriceOnLoad();
}, 3000);
setTimeout(function(){
$('[class*="tm_booking_date"]').each(function(){
var $input=$(this);
var $form=$input.closest('form');
var productId=$form.find('input[name="id"]').val();
if(!productId){
productId=$form.data('id');
}
$input.on('apply.daterangepicker', function(ev, picker){
var selectedDate=picker.startDate.format($input.data('date-format')||'MM/DD/YYYY');
if(lastUpdateDate!==selectedDate){
priceUpdated=false;
}
if(productId){
updateSeasonalPrice(productId, selectedDate);
}});
$input.on('change', function(){
var selectedDate=$(this).val().split(' - ')[0];
if(selectedDate&&productId){
updateSeasonalPrice(productId, selectedDate);
}});
});
}, 1000);
});
})(jQuery);