jQuery(function($){
'use strict';
var config=sabineFleet;
document.querySelectorAll('form.booking_form').forEach(function(bookingForm){
var locations=bookingForm.querySelectorAll('select[name="location"]');
if(locations.length > 1){ locations[1].name='dropoff_location'; }
[['location', 'Pickup location'], ['dropoff_location', 'Return location']].forEach(function(pair){
var select=bookingForm.querySelector('select[name="' + pair[0] + '"]');
if(!select){ return; }
select.required=true;
select.setAttribute('aria-label', pair[1]);
select.classList.add('sabine-booking-location');
if(select.options.length&&select.options[0].value==='0'){ select.options[0].value=''; }});
var book=bookingForm.querySelector('.book_now_btn');
if(book){ book.addEventListener('click', function(event){
if(!bookingForm.reportValidity()){ event.preventDefault(); event.stopImmediatePropagation(); }}, true); }});
var form=document.getElementById('findCarNowForm');
if(form){
form.classList.add('sabine-fleet-form');
var start=form.querySelector('#pixad-time-start');
var end=form.querySelector('#pixad-time-finish');
var button=form.querySelector('button');
if(!start||!end||!button){ return; }
var heading=document.createElement('h2');
heading.textContent='Find your vehicle';
form.prepend(heading);
start.setAttribute('aria-label', 'Pickup date');
end.setAttribute('aria-label', 'Return date');
start.placeholder=config.dateFormat;
end.placeholder=config.dateFormat;
start.readOnly=end.readOnly=true;
var category=form.querySelector('.jelect-current');
if(category){ category.textContent='All vehicles'; }
var allOption=form.querySelector('.jelect-options .jelect-option[data-val=""]');
if(allOption){ allOption.textContent='All vehicles'; }
var results=document.createElement('section');
results.className='sabine-fleet-results';
results.setAttribute('aria-live', 'polite');
form.after(results);
function locationField(id, label, options){
var wrapper=document.createElement('div'); wrapper.className='select sabine-location-field';
var text=document.createElement('label'); text.className='home-search-label'; text.htmlFor=id; text.textContent=label;
var select=document.createElement('select'); select.id=id; select.className='sabine-search-location'; select.required=true;
select.add(new Option('Select a location', ''));
options.forEach(function(option){ select.add(new Option(option.label, option.value)); });
wrapper.append(text, select);
form.querySelector('.vc-auto-search').insertBefore(wrapper, form.querySelector('.pixad-makes-models-select'));
return select;
}
var pickupLocation=locationField('sabine-pickup-location', 'Pickup location', config.pickupLocations||[]);
var returnLocation=locationField('sabine-return-location', 'Return location', config.returnLocations||[]);
function searchTime(input, id, label){
var wrapper=document.createElement('div'); wrapper.className='select sabine-time-field';
var text=document.createElement('label'); text.className='home-search-label';text.htmlFor=id;text.textContent=label;
var select=document.createElement('select');select.id=id;select.className='sabine-search-time';select.setAttribute('aria-label',label);
for(var hour=0;hour<24;hour++){for(var minute=0;minute<60;minute+=15){var value=String(hour).padStart(2,'0')+':'+String(minute).padStart(2,'0');select.add(new Option(value,value));}}
select.value='09:00';wrapper.append(text,select);input.parentElement.after(wrapper);return select;
}
var pickupTime=searchTime(start,'sabine-pickup-time','Pickup time');
var returnTime=searchTime(end,'sabine-return-time','Return time');
var sequence=0;
var locale={format: config.dateFormat, firstDay: 1, applyLabel: 'Apply', cancelLabel: 'Cancel'};
$(start).daterangepicker({singleDatePicker:true, autoUpdateInput:false, minDate:moment(config.today), locale:locale});
$(end).daterangepicker({singleDatePicker:true, autoUpdateInput:false, minDate:moment(config.today).add(1,'day'), locale:locale});
$(start).on('apply.daterangepicker', function(event, picker){
start.value=picker.startDate.format(config.dateFormat);
var minimum=picker.startDate.clone().add(1,'day');
$(end).data('daterangepicker').minDate=minimum;
var selected=moment(end.value, config.dateFormat, true);
if(!selected.isValid()||!selected.isAfter(picker.startDate, 'day')){ end.value=''; }
results.replaceChildren();
sequence++;
});
$(end).on('apply.daterangepicker', function(event, picker){
end.value=picker.startDate.format(config.dateFormat);
results.replaceChildren();
sequence++;
});
button.removeAttribute('onclick');
button.type='submit';
button.textContent='Search vehicles';
form.addEventListener('submit', function(event){
event.preventDefault();
var pickup=moment(start.value, config.dateFormat, true);
var dropoff=moment(end.value, config.dateFormat, true);
if(!pickup.isValid()||!dropoff.isValid()||!dropoff.isAfter(pickup,'day')){
results.textContent='Please choose a pickup date and a later return date.';
return;
}
var current=++sequence;
button.disabled=true;
results.textContent='Checking available vehicles…';
$.getJSON(config.ajaxUrl, {action:'sabine_search_fleet', pickup:pickup.format('YYYY-MM-DD'), dropoff:dropoff.format('YYYY-MM-DD'), category:form.querySelector('[name="make"]').value, pickup_location:pickupLocation.value, return_location:returnLocation.value, pickup_time:pickupTime.value, return_time:returnTime.value})
.done(function(response){
if(current!==sequence){ return; }
results.replaceChildren();
if(!response.success){ results.textContent=response.data.message||'Search unavailable. Please try again.'; return; }
var cars=response.data.cars;
var title=document.createElement('h3');
title.textContent=cars.length ? cars.length + ' available vehicles':'No vehicles available for these dates';
results.append(title);
var summary=document.createElement('p');
summary.textContent=pickup.format('DD MMM YYYY') + ' at ' + pickupTime.value + ' – ' + dropoff.format('DD MMM YYYY') + ' at ' + returnTime.value + '. Select a vehicle to confirm your total and booking details.';
results.append(summary);
var grid=document.createElement('div');
grid.className='sabine-fleet-grid';
cars.forEach(function(car){
var card=document.createElement('article');
var link=document.createElement('a');
link.href=car.url;
if(car.image){ var image=document.createElement('img'); image.src=car.image; image.alt=car.title; image.loading='lazy'; link.append(image); }
var name=document.createElement('h4'); name.textContent=car.title; link.append(name); card.append(link);
var rate=document.createElement('p'); rate.textContent='From ' + car.rate + ' / day'; card.append(rate);
var book=document.createElement('a'); book.href=car.url; book.className='sabine-book-car'; book.textContent='Choose this vehicle'; card.append(book);
grid.append(card);
});
results.append(grid);
})
.fail(function(){ if(current===sequence){ results.textContent='Unable to search right now. Please try again.'; }})
.always(function(){ button.disabled=false; });
});
}
var params=new URLSearchParams(window.location.search);
var pickup=moment(params.get('sabine_pickup'), 'YYYY-MM-DD', true);
var dropoff=moment(params.get('sabine_dropoff'), 'YYYY-MM-DD', true);
var input=$('.booking_form .tm_booking_date');
if(input.length&&pickup.isValid()&&dropoff.isValid()&&!pickup.isBefore(moment(config.today),'day')&&dropoff.isAfter(pickup,'day')&&dropoff.diff(pickup,'days') <=366){
var bookingForm=input.closest('form.booking_form');
[['sabine_pickup_location', 'location'], ['sabine_return_location', 'dropoff_location']].forEach(function(pair){
var value=params.get(pair[0]);
var select=bookingForm.find('select[name="' + pair[1] + '"]');
if(value&&select.find('option').toArray().some(function(option){ return option.value===value; })){ select.val(value); }});
var picker=input.data('daterangepicker');
if(picker){
var startTime=params.get('sabine_pickup_time')||'09:00';
var endTime=params.get('sabine_return_time')||'09:00';
if(!/^(?:[01]\d|2[0-3]):[0-5]\d$/.test(startTime)){ startTime='09:00'; }
if(!/^(?:[01]\d|2[0-3]):[0-5]\d$/.test(endTime)){ endTime='09:00'; }
picker.setStartDate(pickup.clone().hour(Number(startTime.split(':')[0])).minute(Number(startTime.split(':')[1])));
picker.setEndDate(dropoff.clone().hour(Number(endTime.split(':')[0])).minute(Number(endTime.split(':')[1])));
input.val(picker.startDate.format(config.dateFormat) + ' - ' + picker.endDate.format(config.dateFormat));
input.trigger('apply.daterangepicker', [picker]);
}}
});