;(function($, undefined) {
const {ssa, user_id, included_products, past_appointments, translations} = ssaMeprAccountView;
const $iframesContainer = $('#ssa-mepr-booking-app-container');
const $iframes = $('.ssa-mepr-booking-app-iframe-container');
const $appointmentTableBody = $('#ssa-mepr-appointments-table-body');
const $appointmentsTable = $('#mepr-account-appointment-table');
const $pastAppointmentsSection = $('#ssa-mepr__past-appointments-section');
const $backBtn = $('#ssa-mepr-back-appointments')
// Add eventListener on Book Now buttons
$appointmentTableBody.on('click', '.ssa-mepr-book-now', function($event, el) {
$event.preventDefault();
bookNow(this);
});
$backBtn.on('click', function() {
$iframes.fadeOut(300);
$iframesContainer.fadeOut(300, function(){
$appointmentsTable.show()
$pastAppointmentsSection.show();
});
render();
})
function bookNow(el) {
var appointment_type_id = $(el).data('appointment_type_id');
var membership_id = $(el).data('membership_id');
let $iframe = $(`.ssa-mepr-booking-app-iframe-container[data-type-id="${appointment_type_id}"][data-product-id="${membership_id}"]`);
$pastAppointmentsSection.fadeOut(300);
$appointmentsTable.fadeOut(300, function(){
$iframesContainer.fadeIn();
$iframe.show();
$('html, body').animate({
scrollTop: $iframe.offset().top
}, 500);
});
}
function fetchCurrentCycleAppointmentsPerProduct(appointment_type_id, product_id ) {
return new Promise((resolve, reject) => {
$.ajax({
url: ssa.api.root + '/memberpress/user/curr_cycle_appointments_per_product',
method: 'GET',
data: {
user_id,
appointment_type_id,
product_id
},
cache: false,
dataType: 'json',
contentType: 'application/json',
beforeSend: (xhr) => {
xhr.setRequestHeader('X-WP-Nonce', ssa.api.nonce)
}
})
.done((data) => {
resolve(data.data)
})
.fail((xhr, status, error) => {
reject(error)
})
})
}
function renderUpcomingAppointments(){
var $tbody = $('#ssa-mepr-appointments-table-body');
$tbody.empty()
// Create an array to store all the promises
var promises = [];
var rows = [];
$.each(included_products, function(index, product) {
$.each(product.appointment_types, function(appointment_type_id, appointment_type) {
let allowTrialBooking = appointment_type.allow_booking_during_trials
let is_on_trial = product.is_on_trial
if (!allowTrialBooking && is_on_trial) {
return; // Skip this iteration
}
let product_id = product.product_id
let limit_booking_per_cycle = appointment_type.limit_booking_per_cycle;
let max_num_of_appointments_per_cycle = appointment_type.max_num_of_appointments_per_cycle;
let is_renewable = product.is_renewable
let is_active = appointment_type.active
let active_txn_count = product.active_txn_count || 1; // Number of active transactions/renewals
var tr = $('
').addClass('mepr-appointment-row');
let membershipCell = $('
').addClass('ssa-loading-indicator').text(product.membership).appendTo(tr);
let appointmentTypesCell = $('
').addClass('ssa-loading-indicator').text(translations.loading + '...').appendTo(tr);
let bookedCell = $('
').addClass('ssa-loading-indicator').text(translations.loading + '...').appendTo(tr)
$tbody.append(tr);
promises.push(fetchCurrentCycleAppointmentsPerProduct(appointment_type_id, product.product_id));
rows.push({
// Info
appointment_type,
limit_booking_per_cycle,
max_num_of_appointments_per_cycle,
appointment_type_id,
is_renewable,
is_on_trial,
product_id,
is_active,
active_txn_count,
// Row & Cells
tr,
membershipCell,
appointmentTypesCell,
bookedCell,
});
});
});
// Wait for all the promises to resolve
Promise.all(promises)
.then(function(dataArray) {
// Loop through the data and update the corresponding cells
dataArray.forEach(function(appointments, i) {
let row = rows[i];
// If appointment type is inactive and there are no upcoming appointments, remove the row
if (!row.is_active && getUpcoming(appointments).length == 0) {
row.tr.remove();
return;
}
row.membershipCell.removeClass('ssa-loading-indicator');
row.appointmentTypesCell.removeClass('ssa-loading-indicator').html(parseApptTypeInfo(row));
row.bookedCell.removeClass('ssa-loading-indicator').html(parseAppointmentsDate(appointments, row));
});
// Check if the tbody is empty
if ($tbody.children().length === 0) {
var $tr = $('
');
$('
').attr('colspan', '3')
.css({'text-align': 'center', 'padding': '20px'})
.text(translations.empty_table_msg)
.appendTo($tr);
$tbody.append($tr);
}
})
}
function getUpcoming(appointments){
let currentDate = new Date();
let upcomingAppointments = appointments.filter(function(appointment){
let appointmentEndDate = new Date(appointment.end_date + "Z");
// Return true if the appointment's end date is in the future, false otherwise
return appointmentEndDate > currentDate;
});
return upcomingAppointments;
}
function parseAppointmentsDate(appointments, info){
let $output = $('
').addClass('ssa-mepr_cell__upcoming-appts')
let upcomingAppts = getUpcoming(appointments);
// If no upcoming appointments
if(upcomingAppts.length === 0){
$('').text(translations.none).appendTo($output);
} else {
let $ul = $('