-
Notifications
You must be signed in to change notification settings - Fork 0
/
ingridients.php
427 lines (361 loc) · 13.9 KB
/
ingridients.php
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
/* general styles */
html,
body {
font-family: "Roboto", sans-serif;
padding: 0;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
font-size: 24px;
position: relative;
}
/* to dos styles */
.ingredients {
width: 340px;
background-color: #fff;
border-radius: 5px;
color: #3e5481;
padding-top: 100px;
padding-bottom: 200px;
}
.ingredientsContainer {
margin-top: 180px;
}
.ingredients-entry {
position: relative;
margin-bottom: 20px;
border: 1px solid #e1e1e1;
padding: 20px;
border-radius: 30px;
}
.ingredients-entry:nth-of-type(1) {
margin-top: 30px;
}
.ingredients-entry:nth-of-type(4) {
margin-bottom: 30px;
}
.day {
font-weight: 600;
font-size: 24px;
line-height: 30px;
margin: 20px 0 0;
}
.date {
font-size: 14px;
line-height: 18px;
margin: 0 30px 0 30px;
padding-bottom: 20px;
}
/* default checkbox styles */
input[type="checkbox"] {
position: absolute;
right: 9000px;
}
input[type="checkbox"] + .label-text {
position: relative;
color: #497081;
cursor: pointer;
font-size: 13px;
line-height: 14px;
display: flex;
justify-content: space-between;
align-items: center;
transition: all 0.5s ease-in-out;
&::after {
content: "";
border-radius: 50%;
border: 1px solid #497081;
width: 16px;
height: 16px;
transform: scale(1);
transition: all 0.5s ease-in-out 0.4s;
}
.checkmark {
position: absolute;
top: 4px;
right: 2px;
stroke: #c8d4d9;
fill: none;
stroke-width: 1.5;
stroke-dasharray: 30 30;
stroke-dashoffset: 30;
transition: all 0.5s ease-in-out;
}
}
/* checked checkbox styles */
input[type="checkbox"]:checked + .label-text {
color: #c8d4d9;
&::after {
border: 1px solid transparent;
transform: scale(1.2);
transition: all 0.5s ease-in-out;
}
.checkmark {
stroke-dashoffset: 0;
transition: all 0.5s ease-in-out 0.4s;
}
}
/* Hide first two ingredients-entry elements */
.ingredients-entry:nth-child(-n+2) {
display: none;
}
.pad {
height: 170px;
width: 90%;
margin: auto;
background-color: #f1f1f1;
position: fixed;
bottom: 25px;
border-radius: 20px;
display: flex;
flex-direction: column; /* Stack buttons vertically */
justify-content: center; /* Center the buttons vertically */
align-items: center;
}
.pad button {
margin-bottom: 10px; /* Add some space between the buttons */
padding: 10px 40px;
font-size: 16px;
border: none;
border-radius: 20px;
cursor: pointer;
transition: background-color 0.3s ease-in-out, color 0.3s ease-in-out;
}
.pad button#button1 {
background-color: #3498db;
color: #fff;
}
.pad button#button2 {
background-color: #2ecc71;
color: #fff;
}
.pad button:hover {
background-color: #2c3e50;
}
.wrap{
position: absolute;
/* margin: auto; */
width: 60px;
height: 180px;
left: 18px;
top: 0px !important;
}
</style>
</head>
<body>
<div class="ingredients">
<div style="height: 120px; width: 370px; margin: auto; position: absolute;">
<?php include"animation.html"; ?>
<b style="font-size: 18px; position: absolute; width: 260px; margin-left: 120px;" class="diet-preference-label" id="typewriter"></b>
</div>
<!--<p class="date">As per your conversation, here are the suggested ingredients you can use:</p>-->
<div id="ingredientsContainer" class="ingredientsContainer"></div>
</div>
<div class="pad" style="">
<!-- First Button -->
<button id="button1" onclick="generateMeals(1)">Generate Meals</button>
<!-- Second Button -->
<button id="button2" onclick="generateMealPlan(2)">Generate Meal Plan</button>
</div>
<script>
// Function to generate a random string
function generateRandomString(length) {
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
return result;
}
// Function to extract parameter values from the URL
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
// Function to log ingredients on the console and send data to the server
function logIngredientsAndSendToServer() {
// Extract all ingredients and their respective IDs
const allIngredients = document.querySelectorAll('.ingredients-entry');
// Create an array to store ingredient information
const ingredientsArray = [];
// Loop through each ingredient, exclude the first two elements, and store its ID and text in the array
for (let i = 2; i < allIngredients.length; i++) {
const ingredient = allIngredients[i];
const checkbox = ingredient.querySelector('input[type="checkbox"]');
const label = ingredient.querySelector('.label-text');
const ingredientInfo = {
id: checkbox.id,
name: label.textContent.trim().replace(/^\d+\.\s*/, ''), // Remove numbering
};
ingredientsArray.push(ingredientInfo);
}
// Extracting parameter values
const selectedContinent = getParameterByName('continent');
const selectedCountry = getParameterByName('country');
const selectedDiet = getParameterByName('preference');
const selectedDiabeticStatus = getParameterByName('medical-condition');
const selectedMajorMedicalCondition = getParameterByName('major-medical-condition');
const selectedCookingType = getParameterByName('cooking');
const selectedTasteType = getParameterByName('taste');
// Additional data to be sent to the server
const additionalData = {
selectedContinent,
selectedCountry,
selectedDiet,
selectedDiabeticStatus,
selectedMajorMedicalCondition,
selectedCookingType,
selectedTasteType,
};
// Log the array and additional data to the console
console.log("Ingredients Array:", ingredientsArray);
console.log("Additional Data:", additionalData);
// Use AJAX to send data to the server
const xhr = new XMLHttpRequest();
const url = 'server/serv-ingredients.php'; // Replace with the actual server-side PHP script URL
// Set up the request
xhr.open('POST', url, true);
xhr.setRequestHeader('Content-Type', 'application/json');
// Convert the array and additional data to JSON and send it in the request body
xhr.send(JSON.stringify({ ingredients: ingredientsArray, ...additionalData }));
// Log a message when the data is successfully sent
xhr.onload = function () {
if (xhr.status === 200) {
console.log('Data sent successfully to the server.');
} else {
console.error('Error sending data to the server.');
}
};
}
// Call the function to log ingredients and send data to the server after they are loaded
logIngredientsAndSendToServer();
// Extracting parameter values
const selectedContinent = getParameterByName('continent');
const selectedCountry = getParameterByName('country');
const selectedDiet = getParameterByName('preference');
const selectedDiabeticStatus = getParameterByName('medical-condition');
const selectedMajorMedicalCondition = getParameterByName('major-medical-condition');
const selectedCookingType = getParameterByName('cooking');
const selectedTasteType = getParameterByName('taste');
// Constructing the prompt for ChatGPT API
const prompt = `Find 10 best ingredients name for a dish that suits the following preferences remove spices and basic ingredients:
- Continent: ${selectedContinent}
- Country: ${selectedCountry}
- Dietary Preference: ${selectedDiet}
- Diabetic Status: ${selectedDiabeticStatus}
- Any Major Medical Condition: ${selectedMajorMedicalCondition}
- Cooking Type: ${selectedCookingType}
- Taste Type: ${selectedTasteType}`;
// Define your API endpoint
const apiEndpoint = 'https://api.openai.com/v1/completions';
// Define other parameters like API key and max tokens
const apiKey = 'OPEN_AI-API';
const maxTokens = 100;
// Fetch ingredients from the ChatGPT API
fetch(apiEndpoint, {
method: "POST",
headers: {
"Authorization": `Bearer ${apiKey}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "text-davinci-003",
prompt,
max_tokens: maxTokens,
}),
})
.then(response => response.json())
.then(data => {
// Displaying ingredients in the HTML
const ingredientsContainer = document.getElementById('ingredientsContainer');
// Split the text into lines
const ingredientsArray = data.choices[0].text.split('\n');
// Create a div for each ingredient
ingredientsArray.forEach(ingredientText => {
const randomId = generateRandomString(6); // Generate a random string for ID
const ingredientDiv = document.createElement('div');
ingredientDiv.className = 'ingredients-entry'; // Use the existing class
ingredientDiv.innerHTML = `
<input type="checkbox" name="check" id="${randomId}">
<label for="${randomId}" class="label-text">${ingredientText.trim()}
<svg width="15px" height="10px" class="checkmark">
<polyline points="1,5 6,9 14,1" />
</svg>
</label>
`;
// Add event listener to the checkbox
const checkbox = ingredientDiv.querySelector('input[type="checkbox"]');
checkbox.addEventListener('change', () => {
// Log a message to the console when the checkbox is checked or unchecked
console.log(`Checkbox with ID ${checkbox.id} is ${checkbox.checked ? 'checked' : 'unchecked'}`);
});
ingredientsContainer.appendChild(ingredientDiv);
});
// Call the function to log ingredients after they are loaded
//logIngredients();
logIngredientsAndSendToServer();
})
.catch(error => console.error('Error:', error));
</script>
<script type="text/javascript">
// Function to generate meals and redirect
function generateMeals(buttonId) {
// Extracting parameter values
const selectedContinent = getParameterByName('continent');
const selectedCountry = getParameterByName('country');
const selectedDiet = getParameterByName('preference');
const selectedDiabeticStatus = getParameterByName('medical-condition');
const selectedMajorMedicalCondition = getParameterByName('major-medical-condition');
const selectedCookingType = getParameterByName('cooking');
const selectedTasteType = getParameterByName('taste');
// Extract all checked ingredients
const checkedIngredients = document.querySelectorAll('.ingredients-entry input[type="checkbox"]:checked');
// Create an array to store checked ingredient names
const checkedIngredientsArray = Array.from(checkedIngredients).map(checkbox => {
const label = checkbox.closest('.ingredients-entry').querySelector('.label-text');
return label.textContent.trim().replace(/^\d+\.\s*/, ''); // Remove numbering
});
// Construct the URL with parameters
const url = `explore.php?continent=${selectedContinent}&country=${selectedCountry}&preference=${selectedDiet}&medical-condition=${selectedDiabeticStatus}&major-medical-condition=${selectedMajorMedicalCondition}&cooking=${selectedCookingType}&taste=${selectedTasteType}&checkedIngredients=${checkedIngredientsArray.join(',')}`;
// Log the data to the console
console.log("Redirecting to:", url);
// Redirect to the explore.php page with parameters
window.location.href = url;
}
</script>
<script>
const text = "As per your conversation, here are the suggested ingredients you can use:";
const delay = 40;
const typewriterElement = document.getElementById("typewriter");
function typeWriter() {
let i = 0;
function type() {
if (i < text.length) {
typewriterElement.innerHTML += text.charAt(i);
i++;
setTimeout(type, delay);
} else {
// Typewriter effect is complete, delay the drop-from-top animation by 1 second
setTimeout(() => {
}, 1000);
}
}
type();
}
typeWriter();
</script>
</body>
</html>