Spaces:
Sleeping
Sleeping
File size: 19,525 Bytes
f421d9b ee2fedc b8e10df c7d1bc3 b8e10df 95ff181 b8e10df c7d1bc3 b8e10df 95ff181 b8e10df f421d9b 56c11e6 f421d9b c7d1bc3 56c11e6 b8e10df f421d9b 56c11e6 b8e10df f421d9b ee2fedc f421d9b b8e10df f421d9b ee2fedc f421d9b 95ff181 f421d9b 95ff181 f421d9b 56c11e6 b8e10df f421d9b 95ff181 f421d9b c7d1bc3 f421d9b 95ff181 f421d9b c7d1bc3 f89d8d8 | 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 | // 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();
}
} |