let calendar = document.querySelector('.calendar') const month_names = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] function showModal() { const modal = document.getElementById('confirmation-modal'); modal.classList.add('modal'); modal.style.display = 'block'; } document.getElementById("cancel-booking").addEventListener("click", function() { window.location.reload(); }); document.getElementById('book-button').addEventListener('click', () => { if (!window.selectedTime) { alert('Please select an appointment time.'); return; } else{ document.getElementById("last-check").innerHTML=window.selectedTime; showModal(); } }); document.getElementById("confirm-booking").addEventListener("click",function(){ const termData={ term: window.selectedTime, couponCode: document.getElementById("coupon-type").value, medicalCondition:document.getElementById("medical-condition").value }; fetch('/api/requests/book', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(termData) }) .then(response => { if (response.ok) { alert('Appointment booked successfully!'); document.getElementById('confirmation-modal').style.display = 'none'; window.selectedTime = null; document.getElementById('book-button').disabled = true; } else { alert('Failed to book the appointment.'); } }) .catch(error => { console.error('Error:', error); alert('An error occurred while booking the appointment.'); }); }); isLeapYear = (year) => { return (year % 4 === 0 && year % 100 !== 0 && year % 400 !== 0) || (year % 100 === 0 && year % 400 ===0) } getFebDays = (year) => { return isLeapYear(year) ? 29 : 28 } function fetchFreeOrPendingAppointments(date) { fetch(`/api/appointments/free?date=${date}`) .then(response => response.json()) .then(data => { console.log(data); displayFreeAppointments(data); }) .catch(error => console.error('Error fetching free appointments:', error)); } function displayFreeAppointments(appointments) { const container = document.getElementById('hourly-terms'); container.innerHTML = ''; // Clear previous appointments appointments.forEach(appointment => { const appointmentDiv = document.createElement('div'); appointmentDiv.classList.add('appointment-item'); appointmentDiv.style.border = '1px solid black'; appointmentDiv.style.padding = '20px'; appointmentDiv.style.display = 'inline-block'; appointmentDiv.style.marginRight = '10px'; const appointmentDate = new Date(appointment.term); const timeOptions = { hour: '2-digit', minute: '2-digit', hour12: false }; const dateOptions = { year: 'numeric', month: '2-digit', day: '2-digit' }; const formattedTime = appointmentDate.toLocaleTimeString([], timeOptions); const formattedDate=appointmentDate.toLocaleDateString('en-CA', dateOptions); appointmentDiv.textContent = formattedTime; appointmentDiv.dataset.time = formattedDate+" "+formattedTime; // Store full date-time appointmentDiv.addEventListener('click', () => { window.selectedTime = appointmentDiv.dataset.time; document.getElementById('book-button').disabled = false; }); container.appendChild(appointmentDiv); }); } generateCalendar = (month, year) => { let calendar_days = calendar.querySelector('.calendar-days') let calendar_header_year = calendar.querySelector('#year') let days_of_month = [31, getFebDays(year), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] calendar_days.innerHTML = '' let currDate = new Date() if (!month) month = currDate.getMonth() if (!year) year = currDate.getFullYear() let curr_month = `${month_names[month]}` month_picker.innerHTML = curr_month calendar_header_year.innerHTML = year let first_day = new Date(year, month, 1) for (let i = 0; i <= days_of_month[month] + first_day.getDay() - 1; i++) { let day = document.createElement('div') if (i >= first_day.getDay()) { day.classList.add('calendar-day-hover') day.innerHTML = i - first_day.getDay() + 1; day.innerHTML += ` `; let selectedDate = `${year}-${(month + 1).toString().padStart(2, '0')}-${(i - first_day.getDay() + 1).toString().padStart(2, '0')}`; day.addEventListener('click', () => fetchFreeOrPendingAppointments(selectedDate)); if (i - first_day.getDay() + 1 === currDate.getDate() && year === currDate.getFullYear() && month === currDate.getMonth()) { day.classList.add('curr-date') } } calendar_days.appendChild(day) } } let month_list = calendar.querySelector('.month-list') month_names.forEach((e, index) => { let month = document.createElement('div') month.innerHTML = `