File: /mnt/data/dreamsrent-wp-demo/wp-content/plugins/dreamsrent-booking/js/dreams-ajax.js
(function($) {
"use strict";
jQuery(document).ready(function($) {
// Initialize datepickers and timepickers
$('.datepicker').datepicker({
dateFormat: 'yy-mm-dd',
minDate: 0 // Disables all past dates
});
$('.timepicker').timepicker({
timeFormat: 'HH:mm',
interval: 30,
minTime: '00:00',
maxTime: '23:30',
defaultTime: '08:00',
startTime: '00:00',
dynamic: false,
dropdown: true,
scrollbar: true
});
// Handle vehicle dropdown change
$('#vehicle_dropdown').on('change', function() {
var vehicle_id = $(this).val();
if (vehicle_id) {
$.ajax({
url: dreams_ajax_obj.ajax_url,
type: 'POST',
data: {
action: 'get_vehicle_meta',
vehicle_id: vehicle_id,
nonce: dreams_ajax_obj.nonce
},
success: function(response) {
if (response.success) {
$('#post_meta').html(response.data);
$('#view_price').show();
} else {
$('#post_meta').html('<p>' + response.data + '</p>');
}
}
});
} else {
$('#post_meta').empty();
}
});
// Handle 'View Cost' button click
$('#view_price').on('click', function() {
var timeoption = $('input[name="timeoption"]:checked').val();
var dsrent_pickup_loc = $('#dsrent_pickup_loc').val();
var dsrent_pickoff_loc = $('#dsrent_pickoff_loc').val();
var vehicle_id = $('#vehicle_dropdown').val();
var start_date = $('#start_date').val();
var start_time = $('#start_time').val();
var end_date = $('#end_date').val();
var end_time = $('#end_time').val();
var extra_price_get = 0; // Initialize extra price variable
$('input[name="selected_extra_services[]"]:checked').each(function() {
var price = $(this).data('price');
if (!isNaN(price) && price !== undefined) {
extra_price_get += parseFloat(price);
}
});
// Validate Pickup and Dropoff locations
if (!dsrent_pickup_loc || !dsrent_pickoff_loc) {
$('#dsrent_pickoff_loc_error').html('<div class="notice reserv_notice notice-error is-dismissible"><button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button><p>Please select Pickup and Dropoff Location</p> </div>');
$('.notice-dismiss').on('click', function() {
$(this).closest('.notice').remove(); // Remove the notice when "X" is clicked
});
return; // Stop further execution
}
// Validate Day timeoption
if ( !start_date || !end_date) {
$('#extra_services_total').html('<div class="notice reserv_notice notice-error is-dismissible"><button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button><p>Please select Pickup and Start Date and End Date</p> </div>');
$('.notice-dismiss').on('click', function() {
$(this).closest('.notice').remove(); // Remove the notice when "X" is clicked
});
return; // Stop further execution
}
// Validate Day timeoption
if (timeoption === 'day' && start_date === end_date) {
$('#extra_services_total').html('<div class="notice reserv_notice notice-error is-dismissible"><button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button><p>The selected dates must be more than one day for "Day" option.</p> </div>');
$('.notice-dismiss').on('click', function() {
$(this).closest('.notice').remove(); // Remove the notice when "X" is clicked
});
return; // Stop further execution
}
// Validate Hour timeoption
if (timeoption === 'hour' && start_date === end_date && start_time === end_time) {
$('#extra_services_total').html('<div class="notice reserv_notice notice-error is-dismissible"><button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button><p>The selected time must be more than one hour for "Hour" option.</p> </div>');
$('.notice-dismiss').on('click', function() {
$(this).closest('.notice').remove(); // Remove the notice when "X" is clicked
});
return; // Stop further execution
}
if (timeoption === 'hour' && start_date === end_date && end_time <= start_time) {
$('#extra_services_total').html('<div class="notice reserv_notice notice-error is-dismissible"><button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button><p>The selected end time must be greater than start time</p> </div>');
$('.notice-dismiss').on('click', function() {
$(this).closest('.notice').remove(); // Remove the notice when "X" is clicked
});
return; // Stop further execution
}
// Proceed with the AJAX call if validation passes
if (start_date && end_date ) {
$.ajax({
url: dreams_ajax_obj.ajax_url,
type: 'POST',
data: {
action: 'extra_price_get',
timeoption: timeoption,
dsrent_pickup_loc: dsrent_pickup_loc,
dsrent_pickoff_loc: dsrent_pickoff_loc,
vehicle_id: vehicle_id,
start_date: start_date,
start_time: start_time,
end_date: end_date,
end_time: end_time,
extra_price_get: extra_price_get,
nonce: dreams_ajax_obj.nonce
},
success: function(response) {
if (response.success) {
$('#extra_services_total').html('' + response.data + '');
$('#payment_status').show();
$('#create_order').show();
} else {
$('#extra_services_total').html('' + response.data + '');
}
}
});
} else {
// $('#extra_services_total').html('<p class="validation_error">Please select location and date </p>');
$('#extra_services_total').html('<div class="notice reserv_notice notice-error is-dismissible"><button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button><p>Please select Start date and End Date</p> </div>');
$('.notice-dismiss').on('click', function() {
$(this).closest('.notice').remove(); // Remove the notice when "X" is clicked
});
}
});
// Handle form submission for creating order
$('#create_order').on('click', function () {
var timeoption = $('input[name="timeoption"]:checked').val();
var dsrent_pickup_loc = $('#dsrent_pickup_loc').val();
var dsrent_pickoff_loc = $('#dsrent_pickoff_loc').val();
var rental_price_total = $('#rental_price_total').val();
var payment_status = $('#status_order').val();
var extra_price = 0;
$('input[name="selected_extra_services[]"]:checked').each(function() {
var price = $(this).data('price');
if (!isNaN(price) && price !== undefined) {
extra_price += parseFloat(price);
}
});
var vehicle_id = $('#vehicle_dropdown').val();
var start_date = $('#start_date').val();
var start_time = $('#start_time').val();
var end_date = $('#end_date').val();
var end_time = $('#end_time').val();
var customer_info = {
first_name: $('#first_name').val(),
last_name: $('#last_name').val(),
country: $('#country').val(),
street_address: $('#street_address').val(),
city: $('#city').val(),
state: $('#state').val(),
postcode: $('#postcode').val(),
phone: $('#phone').val(),
email: $('#email').val()
};
$.ajax({
url: dreams_ajax_obj.ajax_url,
type: 'POST',
data: {
action: 'create_woocommerce_order',
timeoption: timeoption,
dsrent_pickup_loc: dsrent_pickup_loc,
dsrent_pickoff_loc: dsrent_pickoff_loc,
vehicle_id: vehicle_id,
start_date: start_date,
start_time: start_time,
end_date: end_date,
end_time: end_time,
extra_price: extra_price,
rental_price_total: rental_price_total,
payment_status: payment_status,
customer_info: customer_info,
nonce: dreams_ajax_obj.nonce
},
success: function (response) {
if (response.success) {
//alert('Order created successfully!');
showAdminNotice('Success: ' + response.data, 'success');
location.reload(); // Reload the page to refresh orders
} else {
// alert('Error: ' + response.data);
showAdminNotice('Error: ' + response.data, 'error');
}
}
});
});
// Function to display WordPress admin notices
function showAdminNotice(message, type) {
var noticeClass = (type === 'success') ? 'notice-success' : 'notice-error';
var noticeHtml = '<div class="notice reserv_notice ' + noticeClass + ' is-dismissible"><button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button><p>' + message + '</p></div>';
// Append the notice to the WordPress admin page
$('.erro_message').prepend(noticeHtml);
// // If there's an error, scroll to the error section
// if (type === 'error') {
// $('html, body').animate({
// scrollTop: $('.wrap').offset().top
// }, 'slow');
// }
// Add event listener for the dismiss button
$('.notice-dismiss').on('click', function() {
$(this).closest('.notice').remove(); // Remove the notice when "X" is clicked
});
}
});
})(jQuery);