jQuery(document).ready(function($){
var bookAnAppointmentContainer=$('.gm-book-an-appointment-container');
if(bookAnAppointmentContainer.length){
let locationTag=$('#select_locations');
let specialitiesTag=$('#select_specialities');
let doctorsTag=$('#select_doctors');
let ajaxurl=gmc_global_baseUrl+'/wp-json/gmc/v2/doctors?lang='+current_language;
let filtered_doctors_obj=[];
let doctors_obj=[];
let locations_array=[];
let specialities_array=[];
let single_doctor_object=[];
let url_string=window.location.href;
let url=new URL(url_string);
let param_doctor_id=url.searchParams.get("doctor_id") ? parseInt(url.searchParams.get("doctor_id")):null;
$.ajax({
type:'GET',
url:ajaxurl,
dataType: 'JSON',
error:function(response){
console.log('Unable to fetch Doctors! ');
console.log(response);
},
success:function(response){
if(response){
doctors_obj=response;
let filter_location=doctors_obj.filter((doc)=> {
let speciality=Object.assign([], doc.speciality);
speciality.map((spec)=> {
specialities_array.push(spec)
});
});
let speciality_uniq={};
specialities_array=specialities_array.filter(obj=> !speciality_uniq[obj.term_id]&&(speciality_uniq[obj.term_id]=true));
specialitiesTag.append($('<option>', {
'data-specialityid': 'NA',
value: "",
text: "Select Speciality"
}));
specialities_array.forEach(el=> {
specialitiesTag.append($('<option>', {
'data-specialityid': el.term_id,
value: el.name,
text: el.name
}));
});
doctorsTag.append($('<option>', {
'data-doctorid': null,
value: "",
text: "Select Doctor"
}));
doctors_obj.forEach(el=> {
doctorsTag.append($('<option>', {
'data-doctorid': el.id,
value: el.name,
text: el.name
}));
});
}}
});
specialitiesTag.on('change', function (e){
doctorsTag.html("<option>Select Doctor</option>");
let selectedValue=parseInt($(this).find("option:selected").attr('data-specialityid'));
filtered_doctors_obj=[];
doctors_obj.filter((doc)=> {
let speciality=Object.assign([], doc.speciality);
speciality.filter((spec)=> {
if(spec.term_id===selectedValue){
filtered_doctors_obj.push(doc);
}});
});
filtered_doctors_obj.forEach(el=> {
doctorsTag.append($('<option>', {
'data-doctorid': el.id,
value: el.name,
text: el.name
}));
});
});
}});