jithenderchoudary commited on
Commit
d1bf04d
·
verified ·
1 Parent(s): dcac765

Create static/ / script.js

Browse files
Files changed (1) hide show
  1. static/ / script.js +399 -0
static/ / script.js ADDED
@@ -0,0 +1,399 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ let conversation = [
2
+ { role: 'bot', message: "Hi there! I'm TioNat Assistant! May I know your name?" }
3
+ ];
4
+ let userName = ''; // To store the user's name
5
+ let isSubmitting = false; // Flag to prevent multiple submissions
6
+
7
+ function addMessage(role, message) {
8
+ const chatMessages = document.getElementById('chatMessages');
9
+ if (!chatMessages) {
10
+ console.error('Chat messages container not found!');
11
+ return;
12
+ }
13
+ const messageDiv = document.createElement('div');
14
+ messageDiv.className = role === 'bot' ? 'bot-message' : 'user-message';
15
+ messageDiv.textContent = message;
16
+ chatMessages.appendChild(messageDiv);
17
+ chatMessages.scrollTop = chatMessages.scrollHeight;
18
+ console.log(`Added ${role} message: ${message}`);
19
+ }
20
+
21
+ function sendMessage() {
22
+ const userInput = document.getElementById('userInput');
23
+ if (!userInput) {
24
+ console.error('User input field not found!');
25
+ return;
26
+ }
27
+ const message = userInput.value.trim();
28
+ if (message) {
29
+ addMessage('user', message);
30
+ conversation.push({ role: 'user', message: message });
31
+ userInput.value = '';
32
+ setTimeout(() => handleResponse(message), 500);
33
+ } else {
34
+ console.warn('Empty message!');
35
+ }
36
+ }
37
+
38
+ // Add Enter key event listener for the input field
39
+ document.addEventListener('DOMContentLoaded', () => {
40
+ const userInput = document.getElementById('userInput');
41
+ const clearButton = document.getElementById('clearButton');
42
+ if (userInput) {
43
+ userInput.addEventListener('keydown', (event) => {
44
+ if (event.key === 'Enter' && !event.shiftKey) {
45
+ event.preventDefault();
46
+ sendMessage();
47
+ }
48
+ });
49
+ }
50
+ if (clearButton) {
51
+ clearButton.addEventListener('click', clearChat);
52
+ }
53
+ });
54
+
55
+ function clearChat() {
56
+ const chatMessages = document.getElementById('chatMessages');
57
+ chatMessages.innerHTML = '<div class="bot-message">Hi there! I\'m TioNat Assistant! May I know your name?</div>';
58
+ conversation = [{ role: 'bot', message: "Hi there! I'm TioNat Assistant! May I know your name?" }];
59
+ userName = '';
60
+ document.getElementById('userInput').value = '';
61
+ isSubmitting = false; // Reset submission flag on chat clear
62
+ }
63
+
64
+ function displayForm() {
65
+ const chatMessages = document.getElementById('chatMessages');
66
+ if (!chatMessages) {
67
+ console.error('Chat messages container not found for form!');
68
+ return;
69
+ }
70
+ // Prevent duplicate forms
71
+ if (chatMessages.querySelector('.registration-form')) {
72
+ console.log('Form already exists, skipping display.');
73
+ return;
74
+ }
75
+ const formDiv = document.createElement('div');
76
+ formDiv.className = 'bot-message';
77
+ formDiv.innerHTML = `
78
+ <p>Please register here with these details. Our service agent will get back to you soon.</p>
79
+ <div class="registration-form">
80
+ <label for="mobileNumber">Mobile Number:</label>
81
+ <input type="text" id="mobileNumber" placeholder="Enter your mobile number" /><br/>
82
+ <label for="email">Email:</label>
83
+ <input type="email" id="email" placeholder="Enter your email" /><br/>
84
+ <label for="issueDescription">Issue Description:</label>
85
+ <textarea id="issueDescription" placeholder="Describe your issue"></textarea><br/>
86
+ <button id="submitButton" onclick="submitForm()">Submit</button>
87
+ </div>
88
+ `;
89
+ chatMessages.appendChild(formDiv);
90
+ chatMessages.scrollTop = chatMessages.scrollHeight;
91
+ }
92
+
93
+ function resetForm() {
94
+ const mobileNumber = document.getElementById('mobileNumber');
95
+ const email = document.getElementById('email');
96
+ const issueDescription = document.getElementById('issueDescription');
97
+ const submitButton = document.getElementById('submitButton');
98
+
99
+ if (mobileNumber) mobileNumber.value = '';
100
+ if (email) email.value = '';
101
+ if (issueDescription) issueDescription.value = '';
102
+ if (submitButton) submitButton.disabled = false;
103
+ isSubmitting = false; // Reset the flag after form reset
104
+ console.log('Form reset after 4 seconds');
105
+ }
106
+
107
+ function submitForm() {
108
+ if (isSubmitting) {
109
+ console.log('Submission already in progress, ignoring additional click');
110
+ return;
111
+ }
112
+
113
+ const submitButton = document.getElementById('submitButton');
114
+ if (!submitButton) {
115
+ console.error('Submit button not found!');
116
+ return;
117
+ }
118
+
119
+ // Disable the button and set the flag
120
+ isSubmitting = true;
121
+ submitButton.disabled = true;
122
+ console.log('Submit button clicked, starting submission process');
123
+
124
+ const mobileNumber = document.getElementById('mobileNumber').value.trim();
125
+ const email = document.getElementById('email').value.trim();
126
+ const issueDescription = document.getElementById('issueDescription').value.trim();
127
+
128
+ // Validate mobile number (must be 10 digits)
129
+ const mobileRegex = /^\d{10}$/;
130
+ if (!mobileNumber || !mobileRegex.test(mobileNumber)) {
131
+ addMessage('bot', 'Please enter a valid 10-digit mobile number.');
132
+ isSubmitting = false;
133
+ submitButton.disabled = false;
134
+ console.log('Validation failed: Invalid mobile number');
135
+ return;
136
+ }
137
+ if (!email || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
138
+ addMessage('bot', 'Please enter a valid email address.');
139
+ isSubmitting = false;
140
+ submitButton.disabled = false;
141
+ console.log('Validation failed: Invalid email');
142
+ return;
143
+ }
144
+ if (!issueDescription) {
145
+ addMessage('bot', 'Please describe your issue.');
146
+ isSubmitting = false;
147
+ submitButton.disabled = false;
148
+ console.log('Validation failed: Issue description missing');
149
+ return;
150
+ }
151
+
152
+ const formData = {
153
+ name: userName,
154
+ mobileNumber: mobileNumber,
155
+ email: email,
156
+ issueDescription: issueDescription
157
+ };
158
+
159
+ console.log('Sending form data to server:', formData);
160
+
161
+ fetch('/submit-case', {
162
+ method: 'POST',
163
+ headers: {
164
+ 'Content-Type': 'application/json',
165
+ },
166
+ body: JSON.stringify(formData),
167
+ })
168
+ .then(response => {
169
+ console.log('Received response from server:', response);
170
+ return response.json();
171
+ })
172
+ .then(data => {
173
+ console.log('Parsed response data:', data);
174
+ if (data.success) {
175
+ addMessage('bot', 'Thank you! Your case has been submitted. Our service agent will get back to you soon.');
176
+ // Reset the form after 4 seconds
177
+ setTimeout(() => {
178
+ resetForm();
179
+ }, 4000);
180
+ } else {
181
+ addMessage('bot', 'There was an error submitting your case. Please try again later.');
182
+ isSubmitting = false;
183
+ submitButton.disabled = false;
184
+ }
185
+ })
186
+ .catch(error => {
187
+ console.error('Error submitting form:', error);
188
+ addMessage('bot', 'There was an error submitting your case. Please try again later.');
189
+ isSubmitting = false;
190
+ submitButton.disabled = false;
191
+ });
192
+ }
193
+
194
+ function displayOptions(options) {
195
+ const chatMessages = document.getElementById('chatMessages');
196
+ if (!chatMessages) {
197
+ console.error('Chat messages container not found for options!');
198
+ return;
199
+ }
200
+
201
+ options.forEach(opt => {
202
+ const messageDiv = document.createElement('div');
203
+ messageDiv.className = 'bot-message';
204
+ const button = document.createElement('button');
205
+ button.textContent = opt.text;
206
+ button.className = `option-button ${opt.class}`;
207
+
208
+ button.onclick = () => {
209
+ addMessage('user', opt.text);
210
+ conversation.push({ role: 'user', message: opt.text });
211
+ setTimeout(() => handleResponse(opt.text), 500);
212
+ };
213
+
214
+ messageDiv.appendChild(button);
215
+ chatMessages.appendChild(messageDiv);
216
+ });
217
+
218
+ chatMessages.scrollTop = chatMessages.scrollHeight;
219
+ }
220
+
221
+ function displayCategories() {
222
+ const categories = [
223
+ { text: 'About TioNat and Account Setup', class: 'blue', questions: [
224
+ { text: 'What is TioNat?', class: 'blue' },
225
+ { text: 'How do I create an account on TioNat?', class: 'blue' },
226
+ { text: 'How can I track my order?', class: 'blue' },
227
+ { text: 'Do you ship internationally?', class: 'blue' },
228
+ { text: 'What payment methods do you accept?', class: 'blue' }
229
+ ]},
230
+ { text: 'Nutritional Supplements', class: 'blue', questions: [
231
+ { text: 'What is the best supplement for boosting immunity?', class: 'blue' },
232
+ { text: 'Are your nutritional supplements vegan-friendly?', class: 'blue' },
233
+ { text: 'How do I know which supplement is right for me?', class: 'blue' },
234
+ { text: 'Are there any side effects of using your supplements?', class: 'blue' },
235
+ { text: 'Do you offer products for specific health conditions?', class: 'blue' }
236
+ ]},
237
+ { text: 'Health Care Products', class: 'blue', questions: [
238
+ { text: 'How do I use your health care products?', class: 'blue' },
239
+ { text: 'Do your products come with a money-back guarantee?', class: 'blue' },
240
+ { text: 'Are your health care products FDA-approved?', class: 'blue' },
241
+ { text: 'Are your personal care products cruelty-free?', class: 'blue' },
242
+ { text: 'What ingredients do you use in your skincare products?', class: 'blue' }
243
+ ]},
244
+ { text: 'Personal Care and Skincare', class: 'blue', questions: [
245
+ { text: 'Can I use your personal care products for sensitive skin?', class: 'blue' },
246
+ { text: 'How do I find the right skincare routine?', class: 'blue' },
247
+ { text: 'Can I modify or cancel my order after placing it?', class: 'blue' },
248
+ { text: 'How long does it take to receive my order?', class: 'blue' },
249
+ { text: 'Do you offer free shipping?', class: 'blue' }
250
+ ]},
251
+ { text: 'Returns and Customer Support', class: 'blue', questions: [
252
+ { text: 'How do I return a product?', class: 'blue' },
253
+ { text: 'How can I contact customer service?', class: 'blue' },
254
+ { text: 'Can I track my return?', class: 'blue' },
255
+ { text: 'How can I get a product recommendation?', class: 'blue' },
256
+ { text: 'How do I reset my password?', class: 'blue' } // Moved here
257
+ ]},
258
+ { text: 'Security and Account Management', class: 'blue', questions: [
259
+ { text: 'Is my payment information secure?', class: 'blue' },
260
+ { text: 'How do I update my account information?', class: 'blue' },
261
+ { text: 'Can I leave a review for a product?', class: 'blue' },
262
+ { text: 'How do I report a product issue?', class: 'blue' }
263
+ ]},
264
+ { text: 'Other', class: 'green' }
265
+ ];
266
+
267
+ const chatMessages = document.getElementById('chatMessages');
268
+ if (!chatMessages) {
269
+ console.error('Chat messages container not found for categories!');
270
+ return;
271
+ }
272
+
273
+ categories.forEach(category => {
274
+ const messageDiv = document.createElement('div');
275
+ messageDiv.className = 'bot-message';
276
+ const button = document.createElement('button');
277
+ button.textContent = category.text;
278
+ button.className = `option-button ${category.class}`;
279
+
280
+ button.onclick = () => {
281
+ addMessage('user', category.text);
282
+ conversation.push({ role: 'user', message: category.text });
283
+ if (category.text === 'Other') {
284
+ addMessage('bot', "Please provide more details about your query so our service agent can assist you.");
285
+ displayForm();
286
+ } else {
287
+ displayOptions(category.questions);
288
+ }
289
+ };
290
+
291
+ messageDiv.appendChild(button);
292
+ chatMessages.appendChild(messageDiv);
293
+ });
294
+
295
+ chatMessages.scrollTop = chatMessages.scrollHeight;
296
+ }
297
+
298
+ function handleResponse(userInput) {
299
+ const lastMessage = conversation[conversation.length - 1].message.toLowerCase();
300
+ let botResponse = '';
301
+
302
+ // Handle name input (first user response after bot's greeting)
303
+ if (conversation.length === 2 && conversation[0].role === 'bot' && conversation[0].message.includes('May I know your name?')) {
304
+ userName = userInput;
305
+ botResponse = `Nice to meet you, ${userInput}! 😊 Please select a category to explore common questions or choose 'Other' to submit a custom query.`;
306
+ addMessage('bot', botResponse);
307
+ displayCategories();
308
+ return;
309
+ }
310
+
311
+ // Handle category selection and FAQ responses
312
+ if (lastMessage.includes('about tionat and account setup')) {
313
+ return; // Questions are already displayed via displayOptions
314
+ } else if (lastMessage.includes('nutritional supplements')) {
315
+ return;
316
+ } else if (lastMessage.includes('health care products')) {
317
+ return;
318
+ } else if (lastMessage.includes('personal care and skincare')) {
319
+ return;
320
+ } else if (lastMessage.includes('returns and customer support')) {
321
+ return;
322
+ } else if (lastMessage.includes('security and account management')) {
323
+ return;
324
+ } else if (lastMessage.includes('other')) {
325
+ addMessage('bot', "Please provide more details about your query so our service agent can assist you.");
326
+ displayForm();
327
+ return;
328
+ }
329
+
330
+ // Handle FAQ responses
331
+ if (lastMessage.includes('what is tionat?')) {
332
+ botResponse = 'TioNat is an e-commerce platform offering a wide range of products in Nutritional Care, Health Care, and Personal Care categories. We aim to improve the quality of life with top-notch, reliable, and scientifically-backed products.';
333
+ } else if (lastMessage.includes('how do i create an account on tionat?')) {
334
+ botResponse = 'To create an account on TioNat, simply click on the "Sign Up" button on the top-right corner of our website. Enter your name, email address, and a password. Once registered, you can start shopping!';
335
+ } else if (lastMessage.includes('how can i track my order?')) {
336
+ botResponse = 'After placing your order, you will receive a tracking number via email. You can also track your order from your TioNat account under the "Order History" section.';
337
+ } else if (lastMessage.includes('do you ship internationally?')) {
338
+ botResponse = 'Yes, we offer international shipping to select countries. You can check if your country is eligible for delivery during the checkout process.';
339
+ } else if (lastMessage.includes('what payment methods do you accept?')) {
340
+ botResponse = 'We accept a wide variety of payment methods, including credit/debit cards (Visa, MasterCard, American Express), net banking, PayPal, and cash on delivery (where applicable).';
341
+ } else if (lastMessage.includes('what is the best supplement for boosting immunity?')) {
342
+ botResponse = 'Our Immunity Booster supplements, like Vitamin C, Zinc, and Echinacea, are popular choices for improving immune system health. We recommend consulting with a healthcare provider to find the best fit for you.';
343
+ } else if (lastMessage.includes('are your nutritional supplements vegan-friendly?')) {
344
+ botResponse = 'Yes, many of our nutritional supplements are vegan-friendly. Please check the product details for specific information on ingredients and certifications.';
345
+ } else if (lastMessage.includes('how do i know which supplement is right for me?')) {
346
+ botResponse = 'We recommend consulting with a healthcare professional before choosing a supplement. You can also check our detailed product descriptions and consult with our customer service for recommendations.';
347
+ } else if (lastMessage.includes('are there any side effects of using your supplements?')) {
348
+ botResponse = 'All our products are thoroughly tested for safety, but individual reactions may vary. We suggest reading the product description for possible side effects or consulting with a healthcare provider before using any product.';
349
+ } else if (lastMessage.includes('do you offer products for specific health conditions?')) {
350
+ botResponse = 'Yes, we offer a variety of health care products for conditions such as joint pain, heart health, digestion issues, and more. Please browse through our Health Care category for detailed information.';
351
+ } else if (lastMessage.includes('how do i use your health care products?')) {
352
+ botResponse = 'Each product comes with detailed instructions for use on the packaging. Additionally, you can find user guides and recommendations on our website under each product’s description.';
353
+ } else if (lastMessage.includes('do your products come with a money-back guarantee?')) {
354
+ botResponse = 'Yes, we offer a 30-day money-back guarantee on most of our health care products. Please refer to our return policy for specific conditions and guidelines.';
355
+ } else if (lastMessage.includes('are your health care products fda-approved?')) {
356
+ botResponse = 'Many of our health care products are manufactured following strict quality guidelines and may be FDA-approved where applicable. You can check the product details for certification information.';
357
+ } else if (lastMessage.includes('are your personal care products cruelty-free?')) {
358
+ botResponse = 'Yes, all our personal care products are cruelty-free. We ensure that none of our products are tested on animals, and they are made with sustainable and ethical practices.';
359
+ } else if (lastMessage.includes('what ingredients do you use in your skincare products?')) {
360
+ botResponse = 'Our skincare products are formulated with high-quality ingredients, such as natural oils, vitamins, and plant-based extracts. For specific ingredient lists, please refer to the product description on each item page.';
361
+ } else if (lastMessage.includes('can i use your personal care products for sensitive skin?')) {
362
+ botResponse = 'We offer products specifically designed for sensitive skin. Check the product descriptions for details on ingredients and suitability for sensitive skin. We also recommend doing a patch test before full application.';
363
+ } else if (lastMessage.includes('how do i find the right skincare routine?')) {
364
+ botResponse = 'We suggest starting with a basic routine: cleansing, toning, and moisturizing. For more tailored recommendations, please check out our personalized skincare guides, or reach out to our customer service for advice.';
365
+ } else if (lastMessage.includes('can i modify or cancel my order after placing it?')) {
366
+ botResponse = 'Orders can be modified or canceled within 24 hours of placement. Please contact our customer support immediately for any changes.';
367
+ } else if (lastMessage.includes('how long does it take to receive my order?')) {
368
+ botResponse = 'Orders typically take 3-7 business days to process and ship, depending on your location. You can check your tracking number for more accurate delivery details.';
369
+ } else if (lastMessage.includes('do you offer free shipping?')) {
370
+ botResponse = 'Yes, we offer free shipping on orders above a certain amount. Check the shipping details at checkout to confirm eligibility.';
371
+ } else if (lastMessage.includes('how do i return a product?')) {
372
+ botResponse = 'If you are not satisfied with your purchase, you can return most products within 30 days. Please visit our Returns & Exchange page for detailed instructions.';
373
+ } else if (lastMessage.includes('how can i contact customer service?')) {
374
+ botResponse = 'You can reach our customer service team via email at support@tionat.com or call us at +1-800-123-4567. We\'re here to assist you with any queries.';
375
+ } else if (lastMessage.includes('can i track my return?')) {
376
+ botResponse = 'Yes, once your return is processed, you will receive a tracking number to monitor the status of your return shipment.';
377
+ } else if (lastMessage.includes('how can i get a product recommendation?')) {
378
+ botResponse = 'You can check out our product recommendations under each category. If you\'re unsure, feel free to ask our chatbot or reach out to our customer support for personalized advice.';
379
+ } else if (lastMessage.includes('how do i reset my password?')) {
380
+ botResponse = 'If you\'ve forgotten your password, click on the "Forgot Password" link on the login page. You will receive an email with instructions on how to reset your password.';
381
+ } else if (lastMessage.includes('is my payment information secure?')) {
382
+ botResponse = 'Yes, we use industry-standard encryption (SSL) to protect your payment information. All payments are processed securely through trusted payment gateways.';
383
+ } else if (lastMessage.includes('how do i update my account information?')) {
384
+ botResponse = 'You can update your account information by logging into your account and navigating to the "Account Settings" page. From there, you can update your address, payment methods, and personal details.';
385
+ } else if (lastMessage.includes('can i leave a review for a product?')) {
386
+ botResponse = 'Yes! After purchasing a product, you can leave a review directly on the product page. We value your feedback and it helps other customers make informed decisions.';
387
+ } else if (lastMessage.includes('how do i report a product issue?')) {
388
+ botResponse = 'If you receive a defective or damaged product, please contact our customer service immediately for assistance with returns or exchanges.';
389
+ } else {
390
+ botResponse = "I'm sorry, I don't understand your query. Please select a category to explore common questions or choose 'Other' to submit a custom query.";
391
+ addMessage('bot', botResponse);
392
+ if (!document.querySelector('.option-button')) {
393
+ displayCategories();
394
+ }
395
+ return;
396
+ }
397
+
398
+ addMessage('bot', botResponse);
399
+ }