-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
155 lines (139 loc) · 5.68 KB
/
script.js
File metadata and controls
155 lines (139 loc) · 5.68 KB
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
// ===== GLOBAL UTILITIES =====
// Toggle mobile nav (if needed)
document.addEventListener('DOMContentLoaded', function () {
// ===== ADMIN TABS =====
const tabBtns = document.querySelectorAll('.tab-btn');
const tabContents = document.querySelectorAll('.tab-content');
tabBtns.forEach(btn => {
btn.addEventListener('click', function () {
tabBtns.forEach(b => b.classList.remove('active'));
tabContents.forEach(c => c.classList.remove('active'));
this.classList.add('active');
const target = this.getAttribute('data-tab');
const targetEl = document.getElementById(target);
if (targetEl) targetEl.classList.add('active');
});
});
// ===== PAYMENT METHOD SELECTOR =====
const paymentOptions = document.querySelectorAll('.payment-method-option');
paymentOptions.forEach(opt => {
opt.addEventListener('click', function () {
paymentOptions.forEach(o => o.classList.remove('selected'));
this.classList.add('selected');
const val = this.getAttribute('data-value');
const hiddenInput = document.getElementById('payment_method_input');
if (hiddenInput) hiddenInput.value = val;
});
});
// ===== BOOKING PRICE CALCULATOR =====
const travelersInput = document.getElementById('travelers');
const packagePrice = document.getElementById('package_price');
const totalDisplay = document.getElementById('total_price_display');
if (travelersInput && packagePrice && totalDisplay) {
travelersInput.addEventListener('input', function () {
const count = parseInt(this.value) || 1;
const price = parseFloat(packagePrice.value) || 0;
const total = (count * price).toLocaleString('en-IN');
totalDisplay.textContent = '₹' + total;
});
}
// ===== AUTO DISMISS ALERTS =====
const alerts = document.querySelectorAll('.alert');
alerts.forEach(alert => {
setTimeout(() => {
alert.style.opacity = '0';
alert.style.transition = 'opacity 0.5s ease';
setTimeout(() => alert.remove(), 500);
}, 4000);
});
// ===== FORM VALIDATION =====
const registerForm = document.getElementById('register-form');
if (registerForm) {
registerForm.addEventListener('submit', function (e) {
const pass = document.getElementById('password').value;
const confirm = document.getElementById('confirm_password').value;
if (pass !== confirm) {
e.preventDefault();
showAlert('Passwords do not match!', 'danger');
}
if (pass.length < 6) {
e.preventDefault();
showAlert('Password must be at least 6 characters.', 'danger');
}
});
}
// ===== AI CHAT =====
const chatForm = document.getElementById('chat-form');
const chatInput = document.getElementById('chat-input');
const chatMessages = document.getElementById('chat-messages');
if (chatForm) {
chatForm.addEventListener('submit', function (e) {
e.preventDefault();
const msg = chatInput.value.trim();
if (!msg) return;
appendMessage(msg, 'user');
chatInput.value = '';
fetchBotReply(msg);
});
}
// Quick chat buttons
const quickBtns = document.querySelectorAll('.quick-btn');
quickBtns.forEach(btn => {
btn.addEventListener('click', function () {
const query = this.textContent.trim();
if (chatInput && chatMessages) {
appendMessage(query, 'user');
fetchBotReply(query);
}
});
});
function appendMessage(text, type) {
if (!chatMessages) return;
const div = document.createElement('div');
div.classList.add('message', type);
div.innerHTML = text;
chatMessages.appendChild(div);
chatMessages.scrollTop = chatMessages.scrollHeight;
}
function fetchBotReply(msg) {
appendMessage('<i>Thinking...</i>', 'bot');
fetch('ai_assistant.php', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: 'message=' + encodeURIComponent(msg) + '&ajax=1'
})
.then(res => res.json())
.then(data => {
// Remove "Thinking..." message
const msgs = chatMessages.querySelectorAll('.message.bot');
const last = msgs[msgs.length - 1];
if (last && last.innerHTML.includes('Thinking')) last.remove();
appendMessage(data.reply, 'bot');
})
.catch(() => {
const msgs = chatMessages.querySelectorAll('.message.bot');
const last = msgs[msgs.length - 1];
if (last) last.remove();
appendMessage('Sorry, something went wrong. Please try again.', 'bot');
});
}
// ===== CONFIRM DELETE =====
const deleteForms = document.querySelectorAll('.delete-form');
deleteForms.forEach(form => {
form.addEventListener('submit', function (e) {
if (!confirm('Are you sure you want to delete this item?')) {
e.preventDefault();
}
});
});
});
// ===== HELPER: Show alert dynamically =====
function showAlert(message, type) {
const existing = document.querySelector('.alert');
if (existing) existing.remove();
const alert = document.createElement('div');
alert.className = `alert alert-${type}`;
alert.textContent = message;
const form = document.querySelector('form');
if (form) form.insertBefore(alert, form.firstChild);
}