File: /mnt/data/dreamsrent-wp-demo/wp-content/themes/dreamsrent/assets/js/vendor/script-ajax.js
/*
Author : Dreamstechnologies
Template Name: Dreams rent - Bootstrap Admin Template
*/
(function ($) {
"use strict";
$(document).ready(function () {
$('#car_brand').on('change', function () {
let brand_id = $(this).val();
$.ajax({
url: VendorAjax.ajax_url,
type: 'POST',
data: {
action: 'load_models_by_brand',
brand_id: brand_id,
security: VendorAjax.nonce
},
success: function (response) {
$('#car_model').html(response);
},
error: function (xhr, status, error) {
console.error('AJAX Error:', error);
}
});
});
});
// jQuery(document).ready(function ($) {
// function checkAvailability() {
// let pickupDate = $('#pickup_date').val();
// let pickupTime = $('#pickup_time').val();
// let endDate = $('#end_date').val();
// let endTime = $('#end_time').val();
// let pickup_location = $('#pickup_location').val();
// let return_location = $('#return_location').val();
// let c_userid = $('#c_userid').val();
// let rental_type = $('select[name="rental_type"]').val();
// const $addCustomerBtn = $('#add_customer_step'); // button reference
// // Only proceed if all values are filled
// if (!pickupDate || !pickupTime || !endDate || !endTime || !c_userid || !pickup_location || !return_location) {
// $('#available_rentals').html('<div class="text-warning p-2">Please fill in all fields.</div>');
// $addCustomerBtn.hide();
// return;
// }
// // Show loading
// $('#available_rentals').html('<div class="text-center p-3"><span>Loading available rentals...</span></div>');
// $.ajax({
// url: VendorAjax.ajax_url,
// type: 'POST',
// data: {
// action: 'check_rental_availability',
// pickup_date: pickupDate,
// pickup_time: pickupTime,
// pickup_location: pickup_location,
// return_location: return_location,
// end_date: endDate,
// end_time: endTime,
// c_userid: c_userid,
// rental_type: rental_type,
// security: VendorAjax.nonce
// },
// success: function (response) {
// $('#available_rentals').html(response);
// // ✅ Clean response text
// let cleanResponse = $('<div>').html(response).text().toLowerCase().trim();
// // ✅ Hide or show button depending on response
// if (
// cleanResponse.includes('no rentals available') ||
// cleanResponse.includes('no rentals available in this time range') ||
// cleanResponse.includes('not available') ||
// $(response).find('input[name="car_id"]').length === 0
// ) {
// console.log('No rentals found – hiding Add Customer button.');
// $addCustomerBtn.hide();
// } else {
// $addCustomerBtn.show();
// }
// },
// error: function (xhr, status, error) {
// console.error('AJAX Error:', error);
// $('#available_rentals').html('<div class="text-danger p-2">An error occurred while checking availability.</div>');
// $addCustomerBtn.hide();
// }
// });
// }
// // Trigger availability check on field changes
// $('#pickup_date, #pickup_time, #end_date, #end_time, #pickup_location, #return_location').on('change', function () {
// checkAvailability();
// });
// $('#pickup_location, #return_location').on('select2:select', function () {
// checkAvailability();
// });
// });
jQuery(document).ready(function ($) {
function checkAvailability() {
let pickupDate = $('#pickup_date').val();
let pickupTime = $('#pickup_time').val();
let endDate = $('#end_date').val();
let endTime = $('#end_time').val();
let pickup_location = $('#pickup_location').val();
let return_location = $('#return_location').val();
let c_userid = $('#c_userid').val();
let rental_type = $('select[name="rental_type"]').val();
const $addCustomerBtn = $('#add_customer_step'); // button reference
// Only proceed if all values are filled
if (!pickupDate || !pickupTime || !endDate || !endTime || !c_userid || !pickup_location || !return_location) {
$('#available_rentals').html('<div class="text-warning p-2">Please fill in all fields.</div>');
$addCustomerBtn.hide();
return;
}
// Show loading
$('#available_rentals').html('<div class="text-center p-3"><span>Loading available rentals...</span></div>');
$.ajax({
url: VendorAjax.ajax_url,
type: 'POST',
data: {
action: 'check_rental_availability',
pickup_date: pickupDate,
pickup_time: pickupTime,
pickup_location: pickup_location,
return_location: return_location,
end_date: endDate,
end_time: endTime,
c_userid: c_userid,
rental_type: rental_type,
security: VendorAjax.nonce
},
success: function (response) {
$('#available_rentals').html(response);
// ✅ Clean response text
let cleanResponse = $('<div>').html(response).text().toLowerCase().trim();
// ✅ Hide or show button depending on response
if (
cleanResponse.includes('no rentals available') ||
cleanResponse.includes('no rentals available in this time range') ||
cleanResponse.includes('not available') ||
$(response).find('input[name="car_id"]').length === 0
) {
console.log('No rentals found – hiding Add Customer button.');
$addCustomerBtn.hide();
} else {
$addCustomerBtn.show();
}
},
error: function (xhr, status, error) {
console.error('AJAX Error:', error);
$('#available_rentals').html('<div class="text-danger p-2">An error occurred while checking availability.</div>');
$addCustomerBtn.hide();
}
});
}
// Trigger availability check on field changes
$('#pickup_date, #pickup_time, #end_date, #end_time, #pickup_location, #return_location').on('change', function () {
checkAvailability();
});
$('#pickup_location, #return_location').on('select2:select', function () {
checkAvailability();
});
});
$('.add_extra_services').on('click', function () {
let carId = $('input[name="car_id"]:checked').val();
let edit_id = $('input[name="edit_id"]').val();
if (!carId) {
alert('Please select a car first.');
return;
}
$.ajax({
url: VendorAjax.ajax_url,
type: 'POST',
data: {
action: 'load_extra_services',
nonce: VendorAjax.nonce,
car_id: carId,
edit_id: edit_id
},
success: function (response) {
if (response.success) {
$('.extraservices_display').html(response.data.html);
} else {
$('.extraservices_display').html('<div class="alert alert-warning">No services found.</div>');
}
},
error: function () {
$('.extraservices_display').html('<div class="alert alert-danger">AJAX request failed.</div>');
}
});
});
$('.add_insurance').on('click', function () {
let carId = $('input[name="car_id"]:checked').val();
let edit_id = $('input[name="edit_id"]').val();
if (!carId) {
alert('Please select a car first.');
return;
}
$.ajax({
url: VendorAjax.ajax_url,
type: 'POST',
data: {
action: 'load_add_insurance',
nonce: VendorAjax.nonce,
car_id: carId,
edit_id: edit_id
},
success: function (response) {
if (response.success) {
$('.inurance_display').html(response.data.html);
} else {
$('.inurance_display').html('<div class="alert alert-warning">No services found.</div>');
}
},
error: function () {
$('.inurance_display').html('<div class="alert alert-danger">AJAX request failed.</div>');
}
});
});
// Delegated event handler
$(document).on('click', '.custom-checkbox', function (e) {
e.stopPropagation();
$(this).toggleClass('active');
var checkbox = $(this).find('input[type="checkbox"]');
checkbox.prop('checked', !checkbox.prop('checked'));
});
// Optional: Prevent double toggle if clicking directly on checkbox
$(document).on('click', '.custom-checkbox input[type="checkbox"]', function (e) {
e.stopPropagation(); // prevent event bubbling to parent
});
})(jQuery);