const MAX_VISIBLE_SLOTS=1;
const ALL_SLOTS_VISIBLE=10000;
let selectedEmployee="";
let selectedService="";
const url=window.location.href;
if(url.includes('/specjalisci/')){
let t=therapists.filter(therapist=> therapist.profile===url);
if(t.length > 0){
selectedEmployee=t[0].code;
var hook=document.createElement("div");
hook.id="specialist-widget";
document.querySelector(".et_pb_section_0 .et_pb_row_0 .et_pb_column_0").appendChild(hook);
}}
if(document.querySelector("#specialist-widget")!==null){
if(url.includes('/uslugi/psychoterapia-poznan')) selectedService='Psychoterapia indywidualna';
else if(url.includes('/uslugi/psychologia-dziecieca-i-mlodziezy/')) selectedService='Psycholog dla młodzieży (od 13 roku życia)';
else if(url.includes('/uslugi/konsultacja-psychologiczna')) selectedService='Konsultacja psychologiczna';
else if(url.includes('/leczymy/zaburzenia-odzywiania/')) selectedService='Leczenie zaburzenia odżywiania';
else if(url.includes('/uslugi/terapia-malzenska-i-par')) selectedService='Terapia małżeńska i par';
else if(url.includes('/uslugi/test-osobowosci-mmpi-2')) selectedService='Test osobowości MMPI-2';
else if(url.includes('/uslugi/psychoterapia-online')) selectedService='Psychoterapia indywidualna online';
else if(url.includes('/uslugi/interwencja-kryzysowa')) selectedService='Konsultacja psychologiczna';
else if(url.includes('/uslugi/seksuolog')) selectedService='Konsultacja seksuologiczna';
else if(url.includes('/uslugi/psychodietetyk')) selectedService='Konsultacja psychodietetyczna';
else if(url.includes('/uslugi/psychiatra-prywatnie')) selectedService='Konsultacja psychiatryczna - pierwsza wizyta';
else if(url.includes('/uslugi/terapia-rodzin')) selectedService='Terapia rodzin';
for (let i=0; i < therapists.length; i++){
let code=therapists[i].code;
therapists[i].showAllSlots=false;
therapists[i].showSlotsLimit=MAX_VISIBLE_SLOTS;
let tmpSlots=terminy
.filter(t=> t.kto===code)
.map(t=> ({
day: t.dzien,
hour: t.godzina
}));
therapists[i].slots=Object.values(tmpSlots.reduce((acc, { day, hour })=> {
if(!acc[day]){
acc[day]={ day, hours: [] };}
acc[day].hours.push(hour);
return acc;
}, {})
);
therapists[i].services=Object.entries(uslugi)
.filter(([name, data])=> data.pracownicy.includes(code))
.map(([name])=> name);
therapists[i].selectedService=(selectedService) ? selectedService:therapists[i].services[0];
}
let filteredTherapist={};
if(selectedService){
filteredTherapist=therapists
.filter(therapist=> therapist.services.includes(selectedService));
}
else filteredTherapist=therapists;
if(selectedEmployee){
filteredTherapist=therapists
.filter(therapist=> therapist.code===selectedEmployee);
}
function compareDate(a, b){
const getNearestSlotInfo=(person)=> {
if(!person.slots?.length) return null;
const nearestDay=person.slots.reduce((earliest, slot)=> {
const date=new Date(slot.day);
return !earliest||date < earliest ? date:earliest;
}, null);
const slotsOnNearestDay=person.slots.filter((slot)=> {
return new Date(slot.day).toDateString()===nearestDay.toDateString();
});
const latestHour=slotsOnNearestDay.reduce((latest, slot)=> {
if(!slot.hours?.length) return latest;
const slotLatestHour=slot.hours.reduce((maxHour, hour)=> {
return !maxHour||hour > maxHour ? hour:maxHour;
}, null);
return !latest||slotLatestHour > latest ? slotLatestHour:latest;
}, null);
return {
day: nearestDay,
latestHour,
};};
const slotA=getNearestSlotInfo(a);
const slotB=getNearestSlotInfo(b);
if(!slotA&&!slotB) return 0;
if(!slotA) return 1;
if(!slotB) return -1;
const dayDiff=slotA.day - slotB.day;
if(dayDiff!==0){
return dayDiff;
}
if(!slotA.latestHour&&!slotB.latestHour) return 0;
if(!slotA.latestHour) return 1;
if(!slotB.latestHour) return -1;
return slotB.latestHour.localeCompare(slotA.latestHour);
}
filteredTherapist=filteredTherapist.sort(compareDate);
for(let i=0; i < filteredTherapist.length; i++){
if(i<6) filteredTherapist[i].page=0;
else filteredTherapist[i].page=1;
if(!filteredTherapist[i].slots.length&&i >=9) filteredTherapist[i].page=2;
}
const lastPage=filteredTherapist.reduce(function(prev, curr){ return prev.page < curr.page ? prev.page:curr.page; });
const scrollToElement=(selector)=> {
const el=document.querySelector(selector);
const header=document.querySelector('#main-header');
const offset=30;
let headerHeight=0;
if(header) headerHeight=header.getBoundingClientRect().bottom
if(headerHeight < 0) headerHeight=0;
if(el){
const y=el.getBoundingClientRect().top + window.scrollY - headerHeight - offset;
window.scrollTo({
top: y,
behavior: 'smooth'
})
}}
const { createApp }=Vue
createApp({
data(){
return {
slotsLimit: MAX_VISIBLE_SLOTS,
page: 0,
lastPage: lastPage,
therapists: filteredTherapist,
selectedEmployee: selectedEmployee,
}},
methods: {
formatDate(inputDate){
const miesiace=[
"stycznia", "lutego", "marca", "kwietnia", "maja", "czerwca",
"lipca", "sierpnia", "września", "października", "listopada", "grudnia"
];
const dniTygodnia=[
"Niedziela", "Poniedziałek", "Wtorek", "Środa",
"Czwartek", "Piątek", "Sobota"
];
const today=new Date();
const tomorrow=new Date();
tomorrow.setDate(today.getDate() + 1);
const date=new Date(inputDate);
const isTomorrow =
date.getFullYear()===tomorrow.getFullYear() &&
date.getMonth()===tomorrow.getMonth() &&
date.getDate()===tomorrow.getDate();
const dzien=date.getDate();
const miesiac=miesiace[date.getMonth()];
if(isTomorrow){
return `Jutro, ${dzien} ${miesiac}`;
}else{
const dzienTygodnia=dniTygodnia[date.getDay()];
return `${dzienTygodnia}, ${dzien} ${miesiac}`;
}},
showMore(therapist){
therapist.showAllSlots = !therapist.showAllSlots;
if(therapist.showAllSlots) therapist.showSlotsLimit=ALL_SLOTS_VISIBLE;
else {
therapist.showSlotsLimit=MAX_VISIBLE_SLOTS;
scrollToElement('#' + therapist.code);
}},
showMoreSpecialist(){
this.page++;
},
showLessSpecialist(){
this.page=0;
scrollToElement('.specialist-list');
},
serviceChange(therapist){
therapist.class=['hidden'];
setTimeout(()=> { therapist.class=[]; }, 100);
},
widgetClick(){
lmctracker('Widget_Click');
},
openBooking(therapist, slot, hour){
openBooksy(therapist.selectedService, therapist.code, slot.day, hour);
}},
template: '#specialist-template'
}).mount("#specialist-widget");
};