-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
61 lines (48 loc) · 2.3 KB
/
script.js
File metadata and controls
61 lines (48 loc) · 2.3 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
document.addEventListener('DOMContentLoaded', () => {
const form = document.getElementById('nutriplan-form');
if (!form) return;
const pricePerDayEl = document.getElementById('price-per-day');
const totalPriceEl = document.getElementById('total-price');
// --- CONFIGURATION ---
const BASE_PRICE_PER_DAY = 1000;
const calculateAndDisplayPrice = () => {
// --- GATHER DATA ---
const formData = new FormData(form);
const caloriesValue = form.querySelector('input[name="calories"]:checked').value;
const calorieModifier = parseInt(form.querySelector('input[name="calories"]:checked').dataset.modifier, 10);
const mealsValue = form.querySelector('input[name="meals"]:checked').value;
const mealsModifier = parseInt(form.querySelector('input[name="meals"]:checked').dataset.modifier, 10);
const durationInDays = parseInt(form.querySelector('input[name="duration"]:checked').value, 10);
let totalExtras = 0;
const extraOptions = [];
form.querySelectorAll('input[name="extras"]:checked').forEach(el => {
totalExtras += parseInt(el.dataset.modifier, 10);
extraOptions.push(el.value);
});
// --- CALCULATE PRICE ---
const pricePerDay = BASE_PRICE_PER_DAY + calorieModifier + mealsModifier;
const totalPrice = (pricePerDay * durationInDays) + totalExtras;
// --- DISPLAY PRICE ---
pricePerDayEl.textContent = `${pricePerDay} ₽`;
totalPriceEl.textContent = `${totalPrice} ₽`;
return {
calories: caloriesValue,
meals: mealsValue,
duration: durationInDays,
extras: extraOptions,
pricePerDay: pricePerDay,
totalPrice: totalPrice
};
};
// --- EVENT LISTENERS ---
form.addEventListener('change', calculateAndDisplayPrice);
form.addEventListener('submit', (e) => {
e.preventDefault();
const finalSelection = calculateAndDisplayPrice();
console.log("--- NutriPlan Order ---");
console.log(JSON.stringify(finalSelection, null, 2));
alert('Заказ оформлен! Проверьте детали в консоли разработчика.');
});
// --- INITIAL CALCULATION ---
calculateAndDisplayPrice();
});