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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
let conversation = [
|
| 2 |
+
{ role: 'bot', message: "Hi there! I'm Chat Bot! May I know your name?" }
|
| 3 |
+
];
|
| 4 |
+
let userName = ''; // To store the user's name
|
| 5 |
+
|
| 6 |
+
// Reset conversation on page load
|
| 7 |
+
document.addEventListener('DOMContentLoaded', () => {
|
| 8 |
+
conversation = [
|
| 9 |
+
{ role: 'bot', message: "Hi there! I'm Chat Bot! May I know your name?" }
|
| 10 |
+
];
|
| 11 |
+
const chatMessages = document.getElementById('chatMessages');
|
| 12 |
+
if (chatMessages) {
|
| 13 |
+
chatMessages.innerHTML = '';
|
| 14 |
+
addMessage('bot', "Hi there! I'm Chat Bot! May I know your name?");
|
| 15 |
+
}
|
| 16 |
+
console.log('Page loaded, conversation reset:', conversation);
|
| 17 |
+
});
|
| 18 |
+
|
| 19 |
+
function addMessage(role, message) {
|
| 20 |
+
const chatMessages = document.getElementById('chatMessages');
|
| 21 |
+
if (!chatMessages) {
|
| 22 |
+
console.error('Chat messages container not found!');
|
| 23 |
+
return;
|
| 24 |
+
}
|
| 25 |
+
const messageDiv = document.createElement('div');
|
| 26 |
+
messageDiv.className = role === 'bot' ? 'bot-message' : 'user-message';
|
| 27 |
+
messageDiv.textContent = message;
|
| 28 |
+
chatMessages.appendChild(messageDiv);
|
| 29 |
+
chatMessages.scrollTop = chatMessages.scrollHeight;
|
| 30 |
+
console.log(`Added ${role} message: ${message}`);
|
| 31 |
+
console.log('Current conversation:', conversation);
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
function sendMessage() {
|
| 35 |
+
const userInput = document.getElementById('userInput');
|
| 36 |
+
if (!userInput) {
|
| 37 |
+
console.error('User input field not found!');
|
| 38 |
+
return;
|
| 39 |
+
}
|
| 40 |
+
const message = userInput.value.trim();
|
| 41 |
+
if (message) {
|
| 42 |
+
addMessage('user', message);
|
| 43 |
+
conversation.push({ role: 'user', message: message });
|
| 44 |
+
userInput.value = '';
|
| 45 |
+
setTimeout(() => {
|
| 46 |
+
try {
|
| 47 |
+
handleResponse(message);
|
| 48 |
+
} catch (error) {
|
| 49 |
+
console.error('Error in handleResponse:', error);
|
| 50 |
+
addMessage('bot', 'Sorry, something went wrong. Please try again.');
|
| 51 |
+
}
|
| 52 |
+
}, 500);
|
| 53 |
+
} else {
|
| 54 |
+
console.warn('Empty message!');
|
| 55 |
+
}
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
// Add Enter key event listener for the input field
|
| 59 |
+
document.addEventListener('DOMContentLoaded', () => {
|
| 60 |
+
const userInput = document.getElementById('userInput');
|
| 61 |
+
if (userInput) {
|
| 62 |
+
userInput.addEventListener('keydown', (event) => {
|
| 63 |
+
if (event.key === 'Enter' && !event.shiftKey) {
|
| 64 |
+
event.preventDefault(); // Prevent default behavior (e.g., form submission)
|
| 65 |
+
sendMessage();
|
| 66 |
+
}
|
| 67 |
+
});
|
| 68 |
+
} else {
|
| 69 |
+
console.error('User input field not found during initialization!');
|
| 70 |
+
}
|
| 71 |
+
});
|
| 72 |
+
|
| 73 |
+
function displayForm() {
|
| 74 |
+
const chatMessages = document.getElementById('chatMessages');
|
| 75 |
+
if (!chatMessages) {
|
| 76 |
+
console.error('Chat messages container not found for form!');
|
| 77 |
+
return;
|
| 78 |
+
}
|
| 79 |
+
// Prevent duplicate forms
|
| 80 |
+
if (chatMessages.querySelector('.registration-form')) {
|
| 81 |
+
console.log('Form already exists, skipping display.');
|
| 82 |
+
return;
|
| 83 |
+
}
|
| 84 |
+
const formDiv = document.createElement('div');
|
| 85 |
+
formDiv.className = 'bot-message';
|
| 86 |
+
formDiv.innerHTML = `
|
| 87 |
+
<p>Please register here with these details. Our service agent will get back to you soon.</p>
|
| 88 |
+
<div class="registration-form">
|
| 89 |
+
<label for="mobileNumber">Mobile Number:</label>
|
| 90 |
+
<input type="text" id="mobileNumber" placeholder="Enter your mobile number" /><br/>
|
| 91 |
+
<label for="email">Email:</label>
|
| 92 |
+
<input type="email" id="email" placeholder="Enter your email" /><br/>
|
| 93 |
+
<label for="issueDescription">Issue Description:</label>
|
| 94 |
+
<textarea id="issueDescription" placeholder="Describe your issue"></textarea><br/>
|
| 95 |
+
<button onclick="submitForm()">Submit</button>
|
| 96 |
+
</div>
|
| 97 |
+
`;
|
| 98 |
+
chatMessages.appendChild(formDiv);
|
| 99 |
+
chatMessages.scrollTop = chatMessages.scrollHeight;
|
| 100 |
+
console.log('Form displayed successfully.');
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
function submitForm() {
|
| 104 |
+
const mobileNumber = document.getElementById('mobileNumber').value.trim();
|
| 105 |
+
const email = document.getElementById('email').value.trim();
|
| 106 |
+
const issueDescription = document.getElementById('issueDescription').value.trim();
|
| 107 |
+
|
| 108 |
+
if (!mobileNumber || !email || !issueDescription) {
|
| 109 |
+
addMessage('bot', 'Please fill in all fields before submitting.');
|
| 110 |
+
return;
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
const formData = {
|
| 114 |
+
name: userName,
|
| 115 |
+
mobileNumber: mobileNumber,
|
| 116 |
+
email: email,
|
| 117 |
+
issueDescription: issueDescription
|
| 118 |
+
};
|
| 119 |
+
|
| 120 |
+
fetch('/submit-case', {
|
| 121 |
+
method: 'POST',
|
| 122 |
+
headers: {
|
| 123 |
+
'Content-Type': 'application/json',
|
| 124 |
+
},
|
| 125 |
+
body: JSON.stringify(formData),
|
| 126 |
+
})
|
| 127 |
+
.then(response => response.json())
|
| 128 |
+
.then(data => {
|
| 129 |
+
if (data.success) {
|
| 130 |
+
addMessage('bot', 'Thank you! Your case has been submitted. Our service agent will get back to you soon.');
|
| 131 |
+
} else {
|
| 132 |
+
addMessage('bot', 'There was an error submitting your case. Please try again later.');
|
| 133 |
+
}
|
| 134 |
+
})
|
| 135 |
+
.catch(error => {
|
| 136 |
+
console.error('Error submitting form:', error);
|
| 137 |
+
addMessage('bot', 'There was an error submitting your case. Please try again later.');
|
| 138 |
+
});
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
function clearOptions() {
|
| 142 |
+
const chatMessages = document.getElementById('chatMessages');
|
| 143 |
+
if (!chatMessages) return;
|
| 144 |
+
const optionButtons = chatMessages.querySelectorAll('.bot-message .option-button');
|
| 145 |
+
console.log(`Clearing ${optionButtons.length} existing option buttons.`);
|
| 146 |
+
optionButtons.forEach(button => {
|
| 147 |
+
if (button.parentElement) {
|
| 148 |
+
button.parentElement.remove();
|
| 149 |
+
}
|
| 150 |
+
});
|
| 151 |
+
// Log the current DOM state after clearing
|
| 152 |
+
console.log('DOM after clearing options:', chatMessages.innerHTML);
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
function displayOptions(options, isCategory = false) {
|
| 156 |
+
const chatMessages = document.getElementById('chatMessages');
|
| 157 |
+
if (!chatMessages) {
|
| 158 |
+
console.error('Chat messages container not found for options!');
|
| 159 |
+
return;
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
// Clear existing options before displaying new ones
|
| 163 |
+
clearOptions();
|
| 164 |
+
|
| 165 |
+
console.log(`Displaying ${options.length} options:`, options);
|
| 166 |
+
options.forEach((opt, index) => {
|
| 167 |
+
const messageDiv = document.createElement('div');
|
| 168 |
+
messageDiv.className = 'bot-message';
|
| 169 |
+
const button = document.createElement('button');
|
| 170 |
+
button.textContent = opt.text;
|
| 171 |
+
button.className = `option-button ${opt.class}`;
|
| 172 |
+
|
| 173 |
+
button.addEventListener('click', () => {
|
| 174 |
+
console.log(`Button clicked: ${opt.text}`);
|
| 175 |
+
addMessage('user', opt.text);
|
| 176 |
+
conversation.push({ role: 'user', message: opt.text });
|
| 177 |
+
setTimeout(() => {
|
| 178 |
+
try {
|
| 179 |
+
handleResponse(opt.text);
|
| 180 |
+
} catch (error) {
|
| 181 |
+
console.error('Error in handleResponse after button click:', error);
|
| 182 |
+
addMessage('bot', 'Sorry, something went wrong. Please try again.');
|
| 183 |
+
}
|
| 184 |
+
}, 500);
|
| 185 |
+
});
|
| 186 |
+
|
| 187 |
+
messageDiv.appendChild(button);
|
| 188 |
+
chatMessages.appendChild(messageDiv);
|
| 189 |
+
console.log(`Added button ${index + 1}: ${opt.text}`);
|
| 190 |
+
});
|
| 191 |
+
|
| 192 |
+
chatMessages.scrollTop = chatMessages.scrollHeight;
|
| 193 |
+
// Log the DOM state after adding options
|
| 194 |
+
console.log('DOM after adding options:', chatMessages.innerHTML);
|
| 195 |
+
}
|
| 196 |
+
|
| 197 |
+
function handleResponse(userInput) {
|
| 198 |
+
const lastMessage = conversation[conversation.length - 1].message.toLowerCase();
|
| 199 |
+
let botResponse = '';
|
| 200 |
+
|
| 201 |
+
// Define categories
|
| 202 |
+
const categories = [
|
| 203 |
+
{ text: 'About TioNat and Account Setup', class: 'blue' },
|
| 204 |
+
{ text: 'Nutritional Supplements', class: 'blue' },
|
| 205 |
+
{ text: 'Health Care Products', class: 'blue' },
|
| 206 |
+
{ text: 'Personal Care and Skincare', class: 'blue' },
|
| 207 |
+
{ text: 'Returns and Customer Support', class: 'blue' },
|
| 208 |
+
{ text: 'Security and Account Management', class: 'blue' },
|
| 209 |
+
{ text: 'Other', class: 'green' }
|
| 210 |
+
];
|
| 211 |
+
|
| 212 |
+
// Handle name input (first user response after bot's greeting)
|
| 213 |
+
if (conversation.length === 2 && conversation[0].role === 'bot' && conversation[0].message.includes('May I know your name?')) {
|
| 214 |
+
userName = userInput; // Store the user's name
|
| 215 |
+
botResponse = `Nice to meet you, ${userInput}! 😊 Please select a category for your query:`;
|
| 216 |
+
addMessage('bot', botResponse);
|
| 217 |
+
displayOptions(categories, true); // Display category buttons
|
| 218 |
+
return;
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
// Handle category selection
|
| 222 |
+
if (categories.some(cat => cat.text.toLowerCase() === lastMessage)) {
|
| 223 |
+
botResponse = `Thank you for selecting "${userInput}". Please provide more details about your query so our service agent can assist you.`;
|
| 224 |
+
addMessage('bot', botResponse);
|
| 225 |
+
displayForm();
|
| 226 |
+
return;
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
// Handle unrecognized input
|
| 230 |
+
botResponse = "I'm sorry, I don't understand your query. Please select a category for your query:";
|
| 231 |
+
addMessage('bot', botResponse);
|
| 232 |
+
if (!document.querySelector('.option-button')) {
|
| 233 |
+
displayOptions(categories, true);
|
| 234 |
+
}
|
| 235 |
+
}
|