Spaces:
Sleeping
Sleeping
Download static/script.js from MSivasandeep/EasyTicket_test2: direct link, hf CLI and curl.
- Browser
- Download file 19.5 kB
-
https://huggingface.co/spaces/MSivasandeep/EasyTicket_test2/resolve/main/static/script.js
- Command line
-
hf download hf://spaces/MSivasandeep/EasyTicket_test2/static/script.js
-
curl -L -o script.js https://huggingface.co/spaces/MSivasandeep/EasyTicket_test2/resolve/main/static/script.js
19.5 kB
| // Museum Ticket Booking System with Advanced Pricing | |
| class MuseumChatbot { | |
| constructor() { | |
| this.isOpen = false; | |
| this.init(); | |
| } | |
| init() { | |
| this.createChatbotHTML(); | |
| this.bindEvents(); | |
| } | |
| createChatbotHTML() { | |
| const chatbotHTML = ` | |
| <div class="chatbot-container"> | |
| <button class="chatbot-button" id="chatbotToggle"> | |
| 🎫 | |
| </button> | |
| <div class="chatbot-window" id="chatbotWindow"> | |
| <div class="chatbot-header"> | |
| <h3>Museum Assistant</h3> | |
| </div> | |
| <div class="chatbot-messages" id="chatMessages"></div> | |
| <div class="chatbot-options" id="chatOptions"></div> | |
| </div> | |
| </div> | |
| `; | |
| document.body.insertAdjacentHTML('beforeend', chatbotHTML); | |
| } | |
| bindEvents() { | |
| const toggleBtn = document.getElementById('chatbotToggle'); | |
| const chatWindow = document.getElementById('chatbotWindow'); | |
| toggleBtn.addEventListener('click', () => { | |
| this.toggleChat(); | |
| }); | |
| document.addEventListener('click', (e) => { | |
| if (!e.target.closest('.chatbot-container') && this.isOpen) { | |
| this.closeChat(); | |
| } | |
| }); | |
| } | |
| toggleChat() { | |
| const chatWindow = document.getElementById('chatbotWindow'); | |
| this.isOpen = !this.isOpen; | |
| if (this.isOpen) { | |
| chatWindow.classList.add('active'); | |
| this.startChat(); | |
| } else { | |
| chatWindow.classList.remove('active'); | |
| } | |
| } | |
| closeChat() { | |
| const chatWindow = document.getElementById('chatbotWindow'); | |
| this.isOpen = false; | |
| chatWindow.classList.remove('active'); | |
| } | |
| async startChat() { | |
| this.showTypingIndicator(); | |
| try { | |
| const response = await fetch('/api/chatbot/start', { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| } | |
| }); | |
| const data = await response.json(); | |
| this.removeTypingIndicator(); | |
| this.displayBotMessage(data.message); | |
| this.displayOptions(data.options); | |
| } catch (error) { | |
| this.removeTypingIndicator(); | |
| this.displayBotMessage("Sorry, I'm having trouble connecting. Please try again later."); | |
| } | |
| } | |
| async handleOptionSelect(optionValue, optionText) { | |
| this.addUserMessage(optionText); | |
| this.showTypingIndicator(); | |
| try { | |
| const response = await fetch('/api/chatbot/response', { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| }, | |
| body: JSON.stringify({ input: optionValue }) | |
| }); | |
| const data = await response.json(); | |
| this.removeTypingIndicator(); | |
| if (optionValue === 'back') { | |
| this.startChat(); | |
| } else if (optionValue in { | |
| 'national_museum': true, | |
| 'indian_museum': true, | |
| 'chhatrapati_museum': true, | |
| 'salar_jung': true | |
| }) { | |
| window.location.href = `/museum/${optionValue}`; | |
| } else { | |
| this.displayBotMessage(data.message); | |
| this.displayOptions(data.options); | |
| } | |
| } catch (error) { | |
| this.removeTypingIndicator(); | |
| this.displayBotMessage("Sorry, I encountered an error. Please try again."); | |
| } | |
| } | |
| displayBotMessage(message) { | |
| const messagesContainer = document.getElementById('chatMessages'); | |
| const messageDiv = document.createElement('div'); | |
| messageDiv.className = 'message bot'; | |
| const messageContent = document.createElement('div'); | |
| messageContent.className = 'message-content'; | |
| messageContent.innerHTML = message.replace(/\n/g, '<br>'); | |
| messageDiv.appendChild(messageContent); | |
| messagesContainer.appendChild(messageDiv); | |
| messagesContainer.scrollTop = messagesContainer.scrollHeight; | |
| } | |
| addUserMessage(message) { | |
| const messagesContainer = document.getElementById('chatMessages'); | |
| const messageDiv = document.createElement('div'); | |
| messageDiv.className = 'message user'; | |
| const messageContent = document.createElement('div'); | |
| messageContent.className = 'message-content'; | |
| messageContent.textContent = message; | |
| messageDiv.appendChild(messageContent); | |
| messagesContainer.appendChild(messageDiv); | |
| messagesContainer.scrollTop = messagesContainer.scrollHeight; | |
| } | |
| displayOptions(options) { | |
| const optionsContainer = document.getElementById('chatOptions'); | |
| optionsContainer.innerHTML = ''; | |
| options.forEach(option => { | |
| const button = document.createElement('button'); | |
| button.className = 'option-button'; | |
| button.textContent = option.text; | |
| button.addEventListener('click', () => { | |
| this.handleOptionSelect(option.value, option.text); | |
| }); | |
| optionsContainer.appendChild(button); | |
| }); | |
| } | |
| showTypingIndicator() { | |
| const messagesContainer = document.getElementById('chatMessages'); | |
| const typingDiv = document.createElement('div'); | |
| typingDiv.className = 'message bot'; | |
| typingDiv.id = 'typingIndicator'; | |
| const typingContent = document.createElement('div'); | |
| typingContent.className = 'typing-indicator'; | |
| const typingDots = document.createElement('div'); | |
| typingDots.className = 'typing-dots'; | |
| typingDots.innerHTML = '<span></span><span></span><span></span>'; | |
| typingContent.appendChild(typingDots); | |
| typingDiv.appendChild(typingContent); | |
| messagesContainer.appendChild(typingDiv); | |
| messagesContainer.scrollTop = messagesContainer.scrollHeight; | |
| } | |
| removeTypingIndicator() { | |
| const typingIndicator = document.getElementById('typingIndicator'); | |
| if (typingIndicator) { | |
| typingIndicator.remove(); | |
| } | |
| } | |
| } | |
| // Pricing Calculator | |
| class PricingCalculator { | |
| constructor() { | |
| this.currentPrices = null; | |
| this.init(); | |
| } | |
| init() { | |
| this.bindEvents(); | |
| // Load initial prices for today | |
| const today = new Date().toISOString().split('T')[0]; | |
| document.getElementById('visitDate').value = today; | |
| this.loadPrices(today); | |
| } | |
| bindEvents() { | |
| // Date change event | |
| const visitDateInput = document.getElementById('visitDate'); | |
| if (visitDateInput) { | |
| visitDateInput.addEventListener('change', (e) => { | |
| this.loadPrices(e.target.value); | |
| }); | |
| } | |
| // Visitor type change | |
| const visitorTypeSelect = document.getElementById('visitorType'); | |
| if (visitorTypeSelect) { | |
| visitorTypeSelect.addEventListener('change', () => { | |
| this.calculateTotal(); | |
| }); | |
| } | |
| // Number inputs change | |
| const numberInputs = ['adults', 'children', 'seniors']; | |
| numberInputs.forEach(inputId => { | |
| const input = document.getElementById(inputId); | |
| if (input) { | |
| input.addEventListener('input', () => { | |
| this.calculateTotal(); | |
| }); | |
| } | |
| }); | |
| // Checkbox changes | |
| const checkboxes = ['photography', 'audioGuide', 'guidedTour']; | |
| checkboxes.forEach(checkboxId => { | |
| const checkbox = document.getElementById(checkboxId); | |
| if (checkbox) { | |
| checkbox.addEventListener('change', () => { | |
| this.calculateTotal(); | |
| }); | |
| } | |
| }); | |
| } | |
| async loadPrices(date) { | |
| const museumId = document.getElementById('museumId').value; | |
| try { | |
| const response = await fetch('/api/calculate-price', { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| }, | |
| body: JSON.stringify({ | |
| museum_id: museumId, | |
| visit_date: date | |
| }) | |
| }); | |
| const result = await response.json(); | |
| if (result.success) { | |
| this.currentPrices = result.prices; | |
| this.updatePriceDisplays(); | |
| this.calculateTotal(); | |
| } else { | |
| console.error('Failed to load prices:', result.error); | |
| } | |
| } catch (error) { | |
| console.error('Error loading prices:', error); | |
| } | |
| } | |
| updatePriceDisplays() { | |
| if (!this.currentPrices) return; | |
| // Update sidebar pricing | |
| document.getElementById('indianAdultPrice').textContent = `₹${this.currentPrices.indian_adult}`; | |
| document.getElementById('indianChildPrice').textContent = `₹${this.currentPrices.indian_child}`; | |
| document.getElementById('foreignerAdultPrice').textContent = `₹${this.currentPrices.foreigner_adult}`; | |
| document.getElementById('foreignerChildPrice').textContent = `₹${this.currentPrices.foreigner_child}`; | |
| if (this.currentPrices.indian_senior) { | |
| document.getElementById('indianSeniorPrice').textContent = `₹${this.currentPrices.indian_senior}`; | |
| } | |
| // Update extra services | |
| document.getElementById('extraPhotography').textContent = `₹${this.currentPrices.photography}`; | |
| document.getElementById('extraAudioGuide').textContent = `₹${this.currentPrices.audio_guide}`; | |
| document.getElementById('extraGuidedTour').textContent = `₹${this.currentPrices.guided_tour}`; | |
| // Update service prices | |
| document.getElementById('photographyPrice').textContent = `(+ ₹${this.currentPrices.photography})`; | |
| document.getElementById('audioGuidePrice').textContent = `(+ ₹${this.currentPrices.audio_guide})`; | |
| document.getElementById('guidedTourPrice').textContent = `(+ ₹${this.currentPrices.guided_tour})`; | |
| // Update weekend note | |
| const weekendNote = document.getElementById('weekendNote'); | |
| if (this.currentPrices.is_weekend) { | |
| weekendNote.textContent = '📅 Weekend pricing applied'; | |
| weekendNote.style.color = '#e74c3c'; | |
| } else { | |
| weekendNote.textContent = '📅 Weekday pricing'; | |
| weekendNote.style.color = '#27ae60'; | |
| } | |
| } | |
| calculateTotal() { | |
| if (!this.currentPrices) return; | |
| const visitorType = document.getElementById('visitorType').value; | |
| const adults = parseInt(document.getElementById('adults').value) || 0; | |
| const children = parseInt(document.getElementById('children').value) || 0; | |
| const seniors = parseInt(document.getElementById('seniors').value) || 0; | |
| let total = 0; | |
| // Calculate ticket prices | |
| if (visitorType === 'indian') { | |
| total += adults * this.currentPrices.indian_adult; | |
| total += children * this.currentPrices.indian_child; | |
| if (this.currentPrices.indian_senior) { | |
| total += seniors * this.currentPrices.indian_senior; | |
| } | |
| // Update individual price displays | |
| document.getElementById('adultPrice').textContent = `₹${this.currentPrices.indian_adult} each`; | |
| document.getElementById('childPrice').textContent = `₹${this.currentPrices.indian_child} each`; | |
| if (this.currentPrices.indian_senior) { | |
| document.getElementById('seniorPrice').textContent = `₹${this.currentPrices.indian_senior} each`; | |
| } | |
| } else { | |
| total += adults * this.currentPrices.foreigner_adult; | |
| total += children * this.currentPrices.foreigner_child; | |
| // Update individual price displays | |
| document.getElementById('adultPrice').textContent = `₹${this.currentPrices.foreigner_adult} each`; | |
| document.getElementById('childPrice').textContent = `₹${this.currentPrices.foreigner_child} each`; | |
| document.getElementById('seniorPrice').textContent = 'Not available'; | |
| } | |
| // Add extra services | |
| const photography = document.getElementById('photography').checked; | |
| const audioGuide = document.getElementById('audioGuide').checked; | |
| const guidedTour = document.getElementById('guidedTour').checked; | |
| if (photography) { | |
| total += this.currentPrices.photography; | |
| document.getElementById('photographyRow').style.display = 'flex'; | |
| document.getElementById('photographyTotal').textContent = `₹${this.currentPrices.photography}`; | |
| } else { | |
| document.getElementById('photographyRow').style.display = 'none'; | |
| } | |
| if (audioGuide) { | |
| total += this.currentPrices.audio_guide; | |
| document.getElementById('audioGuideRow').style.display = 'flex'; | |
| document.getElementById('audioGuideTotal').textContent = `₹${this.currentPrices.audio_guide}`; | |
| } else { | |
| document.getElementById('audioGuideRow').style.display = 'none'; | |
| } | |
| if (guidedTour) { | |
| total += this.currentPrices.guided_tour; | |
| document.getElementById('guidedTourRow').style.display = 'flex'; | |
| document.getElementById('guidedTourTotal').textContent = `₹${this.currentPrices.guided_tour}`; | |
| } else { | |
| document.getElementById('guidedTourRow').style.display = 'none'; | |
| } | |
| // Update summary | |
| document.getElementById('adultCount').textContent = adults; | |
| document.getElementById('childCount').textContent = children; | |
| document.getElementById('seniorCount').textContent = seniors; | |
| document.getElementById('adultTotal').textContent = `₹${adults * (visitorType === 'indian' ? this.currentPrices.indian_adult : this.currentPrices.foreigner_adult)}`; | |
| document.getElementById('childTotal').textContent = `₹${children * (visitorType === 'indian' ? this.currentPrices.indian_child : this.currentPrices.foreigner_child)}`; | |
| if (visitorType === 'indian' && this.currentPrices.indian_senior) { | |
| document.getElementById('seniorTotal').textContent = `₹${seniors * this.currentPrices.indian_senior}`; | |
| } else { | |
| document.getElementById('seniorTotal').textContent = '₹0'; | |
| } | |
| document.getElementById('totalAmount').textContent = `₹${total}`; | |
| } | |
| } | |
| // Initialize when DOM is loaded | |
| document.addEventListener('DOMContentLoaded', function() { | |
| // Initialize chatbot | |
| new MuseumChatbot(); | |
| // Initialize pricing calculator if on booking page | |
| const bookingForm = document.getElementById('bookingForm'); | |
| if (bookingForm) { | |
| new PricingCalculator(); | |
| } | |
| // Payment button handler | |
| const payButton = document.getElementById('payButton'); | |
| if (payButton) { | |
| payButton.addEventListener('click', handlePayment); | |
| } | |
| }); | |
| // Main payment handling function | |
| async function handlePayment() { | |
| const formData = new FormData(document.getElementById('bookingForm')); | |
| const bookingData = { | |
| museum_id: document.getElementById('museumId').value, | |
| visit_date: formData.get('visitDate'), | |
| time_slot: formData.get('timeSlot'), | |
| visitor_type: formData.get('visitorType'), | |
| adults: parseInt(formData.get('adults')) || 0, | |
| children: parseInt(formData.get('children')) || 0, | |
| seniors: parseInt(formData.get('seniors')) || 0, | |
| photography: document.getElementById('photography').checked ? 'yes' : 'no', | |
| audio_guide: document.getElementById('audioGuide').checked ? 'yes' : 'no', | |
| guided_tour: document.getElementById('guidedTour').checked ? 'yes' : 'no', | |
| language: formData.get('language'), | |
| name: formData.get('name'), | |
| email: formData.get('email'), | |
| phone: formData.get('phone') | |
| }; | |
| // Validate form | |
| if (!bookingData.visit_date || !bookingData.time_slot || !bookingData.name || !bookingData.email || !bookingData.phone) { | |
| showMessage('Please fill all required fields', 'error'); | |
| return; | |
| } | |
| if (bookingData.adults + bookingData.children + bookingData.seniors === 0) { | |
| showMessage('Please add at least one visitor', 'error'); | |
| return; | |
| } | |
| const payButton = document.getElementById('payButton'); | |
| const paymentStatus = document.getElementById('paymentStatus'); | |
| payButton.disabled = true; | |
| payButton.textContent = 'Processing...'; | |
| paymentStatus.textContent = 'Processing your booking...'; | |
| paymentStatus.className = 'payment-status processing'; | |
| 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) { | |
| paymentStatus.textContent = 'Booking confirmed! Redirecting...'; | |
| paymentStatus.className = 'payment-status success'; | |
| // Redirect to confirmation page after short delay | |
| setTimeout(() => { | |
| window.location.href = `/booking/${result.booking_id}`; | |
| }, 1500); | |
| } else { | |
| paymentStatus.textContent = `Booking failed: ${result.error}`; | |
| paymentStatus.className = 'payment-status error'; | |
| payButton.disabled = false; | |
| payButton.textContent = 'Proceed to Payment'; | |
| } | |
| } catch (error) { | |
| paymentStatus.textContent = 'Network error. Please try again.'; | |
| paymentStatus.className = 'payment-status error'; | |
| payButton.disabled = false; | |
| payButton.textContent = 'Proceed to Payment'; | |
| console.error('Payment error:', error); | |
| } | |
| } | |
| // Show message function | |
| function showMessage(message, type) { | |
| // Remove existing messages | |
| const existingMessages = document.querySelectorAll('.flash-message'); | |
| existingMessages.forEach(msg => msg.remove()); | |
| const messageDiv = document.createElement('div'); | |
| messageDiv.className = `flash-message ${type}`; | |
| messageDiv.textContent = message; | |
| messageDiv.style.cssText = ` | |
| position: fixed; | |
| top: 20px; | |
| right: 20px; | |
| padding: 15px 20px; | |
| border-radius: 8px; | |
| color: white; | |
| font-weight: bold; | |
| z-index: 1000; | |
| background: ${type === 'success' ? '#27ae60' : '#e74c3c'}; | |
| box-shadow: 0 5px 15px rgba(0,0,0,0.2); | |
| `; | |
| document.body.appendChild(messageDiv); | |
| setTimeout(() => { | |
| messageDiv.remove(); | |
| }, 5000); | |
| } | |
| // QR Code download function | |
| function downloadQRCode() { | |
| const qrCodeImg = document.querySelector('img[alt="QR Code"]'); | |
| if (qrCodeImg) { | |
| const link = document.createElement('a'); | |
| link.download = 'museum-ticket-qr.png'; | |
| link.href = qrCodeImg.src; | |
| link.click(); | |
| } | |
| } |