EasyTicket / static /script.js
MSivasandeep's picture
Create script.js
56c11e6 verified
Raw History Blame Contribute Delete
5.27 kB
// 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);
}
});