let dishes = []; 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 loadDishes() { const apiUrl = 'https://edu.std-900.ist.mospolytech.ru/labs/api/dishes'; const apiKey = '358a63a5-52ae-4ab0-800b-90f75ce5a5c2'; fetch(apiUrl) .then(function(response) { if (!response.ok) { throw new Error('Ошибка загрузки данных'); } return response.json(); }) .then(function(data) { dishes = data; sortDishes(); displayDishes(); }) .catch(function(error) { console.error('Ошибка при загрузке блюд:', error); }); } 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(); restoreSelection(); } function createDishCard(dish) { return `
${dish.name}

${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) { const previousDish = selectedDishes[dish.category]; if (previousDish) { const previousCard = document.querySelector( `.dish-card[data-dish="${previousDish.keyword}"]` ); if (previousCard) { previousCard.classList.remove('selected'); } } selectedDishes[dish.category] = dish; const currentCard = document.querySelector( `.dish-card[data-dish="${dish.keyword}"]` ); if (currentCard) { currentCard.classList.add('selected'); } 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(); }); }); } function isValidCombo() { const hasSoup = selectedDishes.soup !== null; const hasMainCourse = selectedDishes['main-course'] !== null; const hasSalad = selectedDishes.salad !== null; const hasDrink = selectedDishes.drink !== null; if (hasSoup && hasMainCourse && hasSalad && hasDrink) { return true; } if (hasSoup && hasMainCourse && hasDrink) { return true; } if (hasSoup && hasSalad && hasDrink) { return true; } if (hasMainCourse && hasSalad && hasDrink) { return true; } if (hasMainCourse && hasDrink) { return true; } return false; } function getValidationMessage() { const hasSoup = selectedDishes.soup !== null; const hasMainCourse = selectedDishes['main-course'] !== null; const hasSalad = selectedDishes.salad !== null; const hasDrink = selectedDishes.drink !== null; if (!hasSoup && !hasMainCourse && !hasSalad && !hasDrink) { return 'Ничего не выбрано. Выберите блюда для заказа'; } if (!hasDrink) { return 'Выберите напиток'; } if (hasSoup && !hasMainCourse && !hasSalad) { return 'Выберите главное блюдо/салат/стартер'; } if (hasSalad && !hasSoup && !hasMainCourse) { return 'Выберите суп или главное блюдо'; } if (!hasMainCourse && !hasSoup && (hasDrink || selectedDishes.dessert)) { return 'Выберите главное блюдо'; } return ''; } function showNotification(message) { const notification = document.createElement('div'); notification.classList.add('notification'); const messageText = document.createElement('p'); messageText.textContent = message; const okButton = document.createElement('button'); okButton.textContent = 'Окей'; okButton.addEventListener('click', function() { notification.remove(); }); notification.append(messageText); notification.append(okButton); document.body.append(notification); } function setupFormValidation() { const orderForm = document.getElementById('order-form'); orderForm.addEventListener('submit', function(event) { if (!isValidCombo()) { event.preventDefault(); const message = getValidationMessage(); showNotification(message); } }); } function restoreSelection() { Object.keys(selectedDishes).forEach(function(category) { const dish = selectedDishes[category]; if (dish) { const card = document.querySelector( `.dish-card[data-dish="${dish.keyword}"]` ); if (card) { card.classList.add('selected'); } } }); } function resetSelection() { Object.keys(selectedDishes).forEach(function(category) { if (selectedDishes[category]) { const card = document.querySelector( `.dish-card[data-dish="${selectedDishes[category].keyword}"]` ); if (card) { card.classList.remove('selected'); } } selectedDishes[category] = null; }); updateOrderSummary(); } document.addEventListener('DOMContentLoaded', function() { loadDishes(); setupFilters(); setupFormValidation(); const resetButton = document.querySelector('button[type="reset"]'); if (resetButton) { resetButton.addEventListener('click', function() { resetSelection(); }); } });