jithenderchoudary commited on
Commit
4982f99
·
verified ·
1 Parent(s): 557b26d

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +96 -64
static/script.js CHANGED
@@ -4,6 +4,7 @@ let conversation = [
4
  let selectedIngredients = [];
5
  let selectedMenuItem = null;
6
  let cart = [];
 
7
 
8
  function addMessage(role, message) {
9
  const chatMessages = document.getElementById('chatMessages');
@@ -36,6 +37,96 @@ function sendMessage() {
36
  }
37
  }
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  function handleResponse(userInput) {
40
  const lastMessage = conversation[conversation.length - 1].message.toLowerCase();
41
  let botResponse = '';
@@ -43,6 +134,7 @@ function handleResponse(userInput) {
43
 
44
  // Handle name input (first user response after bot's greeting)
45
  if (conversation.length === 2 && conversation[0].role === 'bot' && conversation[0].message.includes('May I know your name?')) {
 
46
  botResponse = `Nice to meet you, ${userInput}! 😊 How can I assist you today?`;
47
  options = [
48
  { text: 'What is TioNat?', class: 'blue' },
@@ -135,73 +227,13 @@ function handleResponse(userInput) {
135
  botResponse = 'If you receive a defective or damaged product, please contact our customer service immediately for assistance with returns or exchanges.';
136
  } else {
137
  // Fallback for unrecognized input
138
- botResponse = "Sorry, I didn't understand that. Could you please clarify or choose an option?";
139
- if (conversation.length === 2) {
140
- options = [
141
- { text: 'What is TioNat?', class: 'blue' },
142
- { text: 'How do I create an account on TioNat?', class: 'blue' },
143
- { text: 'How can I track my order?', class: 'blue' },
144
- { text: 'Do you ship internationally?', class: 'blue' },
145
- { text: 'What payment methods do you accept?', class: 'blue' },
146
- { text: 'What is the best supplement for boosting immunity?', class: 'blue' },
147
- { text: 'Are your nutritional supplements vegan-friendly?', class: 'blue' },
148
- { text: 'How do I know which supplement is right for me?', class: 'blue' },
149
- { text: 'Are there any side effects of using your supplements?', class: 'blue' },
150
- { text: 'Do you offer products for specific health conditions?', class: 'blue' },
151
- { text: 'How do I use your health care products?', class: 'blue' },
152
- { text: 'Do your products come with a money-back guarantee?', class: 'blue' },
153
- { text: 'Are your health care products FDA-approved?', class: 'blue' },
154
- { text: 'Are your personal care products cruelty-free?', class: 'blue' },
155
- { text: 'What ingredients do you use in your skincare products?', class: 'blue' },
156
- { text: 'Can I use your personal care products for sensitive skin?', class: 'blue' },
157
- { text: 'How do I find the right skincare routine?', class: 'blue' },
158
- { text: 'Can I modify or cancel my order after placing it?', class: 'blue' },
159
- { text: 'How long does it take to receive my order?', class: 'blue' },
160
- { text: 'Do you offer free shipping?', class: 'blue' },
161
- { text: 'How do I return a product?', class: 'blue' },
162
- { text: 'How can I contact customer service?', class: 'blue' },
163
- { text: 'Can I track my return?', class: 'blue' },
164
- { text: 'How can I get a product recommendation?', class: 'blue' },
165
- { text: 'How do I reset my password?', class: 'blue' },
166
- { text: 'Is my payment information secure?', class: 'blue' },
167
- { text: 'How do I update my account information?', class: 'blue' },
168
- { text: 'Can I leave a review for a product?', class: 'blue' },
169
- { text: 'How do I report a product issue?', class: 'blue' }
170
- ];
171
- }
172
  }
173
 
174
  addMessage('bot', botResponse);
175
  if (options.length > 0) {
176
  displayOptions(options);
177
  }
178
- }
179
- function displayOptions(options) {
180
- const chatMessages = document.getElementById('chatMessages');
181
- if (!chatMessages) {
182
- console.error('Chat messages container not found for options!');
183
- return;
184
- }
185
-
186
- // Display each option as a bot message in the chat
187
- options.forEach(opt => {
188
- const messageDiv = document.createElement('div');
189
- messageDiv.className = 'bot-message'; // This makes the options look like bot messages
190
- const button = document.createElement('button');
191
- button.textContent = opt.text;
192
- button.className = `option-button ${opt.class}`;
193
-
194
- // When an option is clicked, treat it as a user response and trigger the appropriate handler
195
- button.onclick = () => {
196
- addMessage('user', opt.text);
197
- conversation.push({ role: 'user', message: opt.text });
198
- setTimeout(() => handleResponse(opt.text), 500);
199
- };
200
-
201
- messageDiv.appendChild(button);
202
- chatMessages.appendChild(messageDiv); // Append the button inside a bot message container
203
- });
204
-
205
- // Scroll to the bottom of the chat container
206
- chatMessages.scrollTop = chatMessages.scrollHeight;
207
- }
 
4
  let selectedIngredients = [];
5
  let selectedMenuItem = null;
6
  let cart = [];
7
+ let userName = ''; // To store the user's name
8
 
9
  function addMessage(role, message) {
10
  const chatMessages = document.getElementById('chatMessages');
 
37
  }
38
  }
39
 
40
+ function displayForm() {
41
+ const chatMessages = document.getElementById('chatMessages');
42
+ if (!chatMessages) {
43
+ console.error('Chat messages container not found for form!');
44
+ return;
45
+ }
46
+
47
+ const formDiv = document.createElement('div');
48
+ formDiv.className = 'bot-message';
49
+ formDiv.innerHTML = `
50
+ <p>Please register here with these details. Our service agent will get back to you soon.</p>
51
+ <div class="registration-form">
52
+ <label for="mobileNumber">Mobile Number:</label>
53
+ <input type="text" id="mobileNumber" placeholder="Enter your mobile number" /><br/>
54
+ <label for="email">Email:</label>
55
+ <input type="email" id="email" placeholder="Enter your email" /><br/>
56
+ <label for="issueDescription">Issue Description:</label>
57
+ <textarea id="issueDescription" placeholder="Describe your issue"></textarea><br/>
58
+ <button onclick="submitForm()">Submit</button>
59
+ </div>
60
+ `;
61
+ chatMessages.appendChild(formDiv);
62
+ chatMessages.scrollTop = chatMessages.scrollHeight;
63
+ }
64
+
65
+ function submitForm() {
66
+ const mobileNumber = document.getElementById('mobileNumber').value.trim();
67
+ const email = document.getElementById('email').value.trim();
68
+ const issueDescription = document.getElementById('issueDescription').value.trim();
69
+
70
+ if (!mobileNumber || !email || !issueDescription) {
71
+ addMessage('bot', 'Please fill in all fields before submitting.');
72
+ return;
73
+ }
74
+
75
+ const formData = {
76
+ name: userName,
77
+ mobileNumber: mobileNumber,
78
+ email: email,
79
+ issueDescription: issueDescription
80
+ };
81
+
82
+ fetch('/submit-case', {
83
+ method: 'POST',
84
+ headers: {
85
+ 'Content-Type': 'application/json',
86
+ },
87
+ body: JSON.stringify(formData),
88
+ })
89
+ .then(response => response.json())
90
+ .then(data => {
91
+ if (data.success) {
92
+ addMessage('bot', 'Thank you! Your case has been submitted. Our service agent will get back to you soon.');
93
+ } else {
94
+ addMessage('bot', 'There was an error submitting your case. Please try again later.');
95
+ }
96
+ })
97
+ .catch(error => {
98
+ console.error('Error submitting form:', error);
99
+ addMessage('bot', 'There was an error submitting your case. Please try again later.');
100
+ });
101
+ }
102
+
103
+ function displayOptions(options) {
104
+ const chatMessages = document.getElementById('chatMessages');
105
+ if (!chatMessages) {
106
+ console.error('Chat messages container not found for options!');
107
+ return;
108
+ }
109
+
110
+ options.forEach(opt => {
111
+ const messageDiv = document.createElement('div');
112
+ messageDiv.className = 'bot-message';
113
+ const button = document.createElement('button');
114
+ button.textContent = opt.text;
115
+ button.className = `option-button ${opt.class}`;
116
+
117
+ button.onclick = () => {
118
+ addMessage('user', opt.text);
119
+ conversation.push({ role: 'user', message: opt.text });
120
+ setTimeout(() => handleResponse(opt.text), 500);
121
+ };
122
+
123
+ messageDiv.appendChild(button);
124
+ chatMessages.appendChild(messageDiv);
125
+ });
126
+
127
+ chatMessages.scrollTop = chatMessages.scrollHeight;
128
+ }
129
+
130
  function handleResponse(userInput) {
131
  const lastMessage = conversation[conversation.length - 1].message.toLowerCase();
132
  let botResponse = '';
 
134
 
135
  // Handle name input (first user response after bot's greeting)
136
  if (conversation.length === 2 && conversation[0].role === 'bot' && conversation[0].message.includes('May I know your name?')) {
137
+ userName = userInput; // Store the user's name
138
  botResponse = `Nice to meet you, ${userInput}! 😊 How can I assist you today?`;
139
  options = [
140
  { text: 'What is TioNat?', class: 'blue' },
 
227
  botResponse = 'If you receive a defective or damaged product, please contact our customer service immediately for assistance with returns or exchanges.';
228
  } else {
229
  // Fallback for unrecognized input
230
+ botResponse = "Sorry, I didn't understand that. Let's register your query so our service agent can assist you.";
231
+ addMessage('bot', botResponse);
232
+ displayForm();
233
+ return; // Stop further processing since we're showing the form
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234
  }
235
 
236
  addMessage('bot', botResponse);
237
  if (options.length > 0) {
238
  displayOptions(options);
239
  }