jQuery(document).ready(function ($) { function handleUserSelections() { let returnLocation = $('select[name="return-location"]').val(); let pickupLocation = $('select[name="pick-up-location"]').val(); let checkinDate = $('input[name="checkin_checkout_from"]').val(); let checkoutDate = $('input[name="checkin_checkout_to"]').val(); // ✅ Only get text if an actual value is selected (prevents default option from being stored) let returnLocationText = returnLocation ? $('select[name="return-location"] option:selected').first().text().trim() : ""; let pickupLocationText = pickupLocation ? $('select[name="pick-up-location"] option:selected').first().text().trim() : ""; let isAirportPickup = pickupLocation === "47"; let isAirportDelivery = returnLocation === "55"; let shouldCalculate = isAirportPickup || isAirportDelivery; let data = { action: 'calculate_pickup_delivery', security: ajax_object.nonce, deliveryname: returnLocation || "", deliverytext: returnLocationText || "", deliverytime: checkinDate || "", pickupname: pickupLocation || "", pickuptext: pickupLocationText || "", pickuptime: checkoutDate || "", shouldCalculate: shouldCalculate ? 1 : 0 }; $.ajax({ url: ajax_object.ajax_url, type: 'POST', data: data, success: function (response) { console.log("Success:", response); }, error: function (error) { console.error("Error:", error); } }); } $('select[name="return-location"], select[name="pick-up-location"], input[name="checkin_checkout_from"], input[name="checkin_checkout_to"]') .off('change') .on('change', handleUserSelections); // Function to hide date picker function hideDatePicker() { $(".jet-abaf-separate-field.jet-form-col-6").css("display", "none"); // Hide the date picker } // Function to calculate nights function getNightsCount(start, end) { let startDate = new Date(start.split('/').reverse().join('-')); let endDate = new Date(end.split('/').reverse().join('-')); let nights = (endDate - startDate) / (1000 * 60 * 60 * 24); return nights || 1; // Ensure at least 1 night is displayed } // Check if session dates exist if (typeof checkinDate !== "undefined" && typeof checkoutDate !== "undefined" && checkinDate && checkoutDate) { hideDatePicker(); let dateRange = checkoutDate + " - " + checkinDate; // Set the value of the hidden input field $("#jet_abaf_field_range").val(dateRange); // Update the displayed selected dates in the UI $("#selected-dates-display").text(dateRange).css("color", "#333"); // Make text visible $(".start-day").text(checkinDate); $(".end-day").text(checkoutDate); $(".selected-days-num").text(getNightsCount(checkinDate, checkoutDate)); $(".selected-days").show(); // Add a visible input to show selected dates if not already present if ($("#date-display").length === 0) { $("#jet_abaf_field_range").after( '' ); } // Hide the date picker completely } });