const selectedDishes = {
soup: null,
'main-course': null,
salad: null,
drink: null,
dessert: null
};
let activeFilters = {
soup: null,
'main-course': null,
salad: null,
drink: null,
dessert: null
};
function sortDishes() {
dishes.sort(function(a, b) {
return a.name.localeCompare(b.name, 'ru');
});
}
function displayDishes() {
const soupSection = document.getElementById('soup-section');
const mainCourseSection = document.getElementById('main-course-section');
const saladSection = document.getElementById('salad-section');
const drinkSection = document.getElementById('drink-section');
const dessertSection = document.getElementById('dessert-section');
soupSection.innerHTML = '';
mainCourseSection.innerHTML = '';
saladSection.innerHTML = '';
drinkSection.innerHTML = '';
dessertSection.innerHTML = '';
dishes.forEach(function(dish) {
const dishCard = createDishCard(dish);
if (dish.category === 'soup') {
if (!activeFilters.soup || dish.kind === activeFilters.soup) {
soupSection.insertAdjacentHTML('beforeend', dishCard);
}
} else if (dish.category === 'main-course') {
if (!activeFilters['main-course'] ||
dish.kind === activeFilters['main-course']) {
mainCourseSection.insertAdjacentHTML('beforeend', dishCard);
}
} else if (dish.category === 'salad') {
if (!activeFilters.salad || dish.kind === activeFilters.salad) {
saladSection.insertAdjacentHTML('beforeend', dishCard);
}
} else if (dish.category === 'drink') {
if (!activeFilters.drink || dish.kind === activeFilters.drink) {
drinkSection.insertAdjacentHTML('beforeend', dishCard);
}
} else if (dish.category === 'dessert') {
if (!activeFilters.dessert ||
dish.kind === activeFilters.dessert) {
dessertSection.insertAdjacentHTML('beforeend', dishCard);
}
}
});
addDishClickHandlers();
}
function createDishCard(dish) {
return `
${dish.price} руб.
${dish.name}
${dish.count}
`;
}
function addDishClickHandlers() {
const dishCards = document.querySelectorAll('.dish-card');
dishCards.forEach(function(card) {
card.addEventListener('click', function() {
const keyword = this.dataset.dish;
const dish = dishes.find(function(d) {
return d.keyword === keyword;
});
if (dish) {
selectDish(dish);
}
});
});
}
function selectDish(dish) {
selectedDishes[dish.category] = dish;
updateOrderSummary();
}
function updateOrderSummary() {
const orderSummary = document.getElementById('order-summary');
const hasSelection = selectedDishes.soup ||
selectedDishes['main-course'] ||
selectedDishes.salad ||
selectedDishes.drink ||
selectedDishes.dessert;
if (!hasSelection) {
orderSummary.innerHTML = 'Ничего не выбрано
';
return;
}
let summaryHTML = '';
let totalPrice = 0;
if (selectedDishes.soup) {
summaryHTML += `
Суп
${selectedDishes.soup.name}
${selectedDishes.soup.price} руб.
`;
totalPrice += selectedDishes.soup.price;
} else {
summaryHTML += `
`;
}
if (selectedDishes['main-course']) {
summaryHTML += `
Главное блюдо
${selectedDishes['main-course'].name}
${selectedDishes['main-course'].price} руб.
`;
totalPrice += selectedDishes['main-course'].price;
} else {
summaryHTML += `
Главное блюдо
Блюдо не выбрано
`;
}
if (selectedDishes.salad) {
summaryHTML += `
Салат
${selectedDishes.salad.name}
${selectedDishes.salad.price} руб.
`;
totalPrice += selectedDishes.salad.price;
} else {
summaryHTML += `
`;
}
if (selectedDishes.drink) {
summaryHTML += `
Напиток
${selectedDishes.drink.name}
${selectedDishes.drink.price} руб.
`;
totalPrice += selectedDishes.drink.price;
} else {
summaryHTML += `
Напиток
Напиток не выбран
`;
}
if (selectedDishes.dessert) {
summaryHTML += `
Десерт
${selectedDishes.dessert.name}
${selectedDishes.dessert.price} руб.
`;
totalPrice += selectedDishes.dessert.price;
} else {
summaryHTML += `
`;
}
summaryHTML += `
Стоимость заказа
${totalPrice} руб.
`;
orderSummary.innerHTML = summaryHTML;
}
function setupFilters() {
const filterButtons = document.querySelectorAll('.filter-btn');
filterButtons.forEach(function(button) {
button.addEventListener('click', function() {
const kind = this.dataset.kind;
const section = this.closest('section');
const category = section.querySelector('.dishes-grid').id
.replace('-section', '');
if (this.classList.contains('active')) {
this.classList.remove('active');
activeFilters[category] = null;
} else {
const sectionButtons = section
.querySelectorAll('.filter-btn');
sectionButtons.forEach(function(btn) {
btn.classList.remove('active');
});
this.classList.add('active');
activeFilters[category] = kind;
}
displayDishes();
});
});
}
document.addEventListener('DOMContentLoaded', function() {
sortDishes();
displayDishes();
setupFilters();
});