Spaces:
Sleeping
Sleeping
File size: 5,265 Bytes
56c11e6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | // Booking form functionality
document.addEventListener('DOMContentLoaded', function() {
const bookingForm = document.getElementById('bookingForm');
const visitorsInput = document.getElementById('visitors');
const totalAmountElement = document.getElementById('totalAmount');
const visitorCountElement = document.getElementById('visitorCount');
const ticketPrice = parseInt(document.getElementById('ticketPrice').textContent.replace('₹', ''));
// Update total amount when number of visitors changes
if (visitorsInput && totalAmountElement) {
visitorsInput.addEventListener('input', updateTotalAmount);
updateTotalAmount(); // Initial calculation
}
// Form submission
if (bookingForm) {
bookingForm.addEventListener('submit', handleBooking);
}
// Check availability when date or time slot changes
const visitDateInput = document.getElementById('visitDate');
const timeSlotSelect = document.getElementById('timeSlot');
if (visitDateInput && timeSlotSelect) {
visitDateInput.addEventListener('change', checkAvailability);
timeSlotSelect.addEventListener('change', checkAvailability);
// Set minimum date to today
const today = new Date().toISOString().split('T')[0];
visitDateInput.min = today;
}
function updateTotalAmount() {
const visitors = parseInt(visitorsInput.value) || 1;
const totalAmount = visitors * ticketPrice;
totalAmountElement.textContent = `₹${totalAmount}`;
visitorCountElement.textContent = visitors;
}
async function checkAvailability() {
const visitDate = visitDateInput.value;
const timeSlot = timeSlotSelect.value;
const museumId = document.getElementById('museumId').value;
if (!visitDate || !timeSlot) return;
try {
const response = await fetch('/api/check-availability', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
museum_id: museumId,
visit_date: visitDate,
time_slot: timeSlot
})
});
const data = await response.json();
if (data.available) {
showMessage(`✅ ${data.available_slots} slots available`, 'success');
} else {
showMessage('❌ This time slot is fully booked. Please choose another slot.', 'error');
}
} catch (error) {
console.error('Error checking availability:', error);
}
}
async function handleBooking(event) {
event.preventDefault();
const formData = new FormData(bookingForm);
const bookingData = {
museum_id: document.getElementById('museumId').value,
visit_date: formData.get('visitDate'),
time_slot: formData.get('timeSlot'),
visitors: formData.get('visitors'),
language: formData.get('language'),
name: formData.get('name'),
email: formData.get('email'),
phone: formData.get('phone')
};
const submitBtn = bookingForm.querySelector('.submit-btn');
submitBtn.disabled = true;
submitBtn.textContent = 'Booking...';
try {
const response = await fetch('/api/book', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(bookingData)
});
const result = await response.json();
if (result.success) {
window.location.href = `/booking/${result.booking_id}`;
} else {
showMessage(`Booking failed: ${result.error}`, 'error');
}
} catch (error) {
showMessage('Network error. Please try again.', 'error');
console.error('Booking error:', error);
} finally {
submitBtn.disabled = false;
submitBtn.textContent = 'Confirm Booking';
}
}
function showMessage(message, type) {
// Remove existing messages
const existingMessage = document.querySelector('.message');
if (existingMessage) {
existingMessage.remove();
}
const messageDiv = document.createElement('div');
messageDiv.className = `message ${type}`;
messageDiv.textContent = message;
messageDiv.style.cssText = `
position: fixed;
top: 20px;
right: 20px;
padding: 15px 20px;
border-radius: 5px;
color: white;
font-weight: bold;
z-index: 1000;
background: ${type === 'success' ? '#27ae60' : '#e74c3c'};
box-shadow: 0 3px 10px rgba(0,0,0,0.2);
`;
document.body.appendChild(messageDiv);
setTimeout(() => {
messageDiv.remove();
}, 5000);
}
}); |