;
// Если нет price_ra_pack, используем price_ra или price
const basePrice = priceRaPack > 0 && quantityPerPack > 1
? priceRaPack
: parseFloat(document.querySelector('.product__summ').dataset.basePrice) || 0;
const baseOldPrice = parseFloat(document.querySelector('.product__summ-old').dataset.baseOldPrice) || 0;
const priceDisplay = document.querySelector('.product__summ');
const oldPriceDisplay = document.querySelector('.product__summ-old');
const hintDisplay = document.querySelector('.product__hint span');
const addButton = document.querySelector('.product__button .button_catalog');
const buttonText = addButton.querySelector('span');
function updateHint() {
hintDisplay.textContent = `по ${quantityPerPack} шт в упаковке`;
}
updateHint();
function updatePrice() {
let packQuantity = parseInt(input.value) || 1;
packQuantity = Math.max(1, packQuantity); // Минимум 1 упаковка
input.value = packQuantity;
// Итоговая цена: basePrice * количество упаковок
const totalPriceValue = basePrice * packQuantity;
// Форматируем цену: убираем лишние нули после запятой
const totalPrice = Number.isInteger(totalPriceValue)
? totalPriceValue.toString()
: totalPriceValue.toFixed(2).replace(/\.?0+$/, '');
const totalOldPrice = baseOldPrice
? (baseOldPrice * packQuantity).toFixed(2).replace(/\.?0+$/, '')
: '';
priceDisplay.textContent = totalPrice + ' ₽';
if (totalOldPrice) {
oldPriceDisplay.textContent = totalOldPrice + ' ₽';
}
updateHint();
}
plusButton.addEventListener('click', function () {
let currentValue = parseInt(input.value) || 1;
input.value = currentValue + 1;
updatePrice();
});
minusButton.addEventListener('click', function () {
let currentValue = parseInt(input.value) || 1;
if (currentValue > 1) {
input.value = currentValue - 1;
updatePrice();
}
});
input.addEventListener('input', function () {
let value = parseInt(input.value) || 1;
value = Math.max(1, value); // Минимум 1 упаковка
input.value = value;
updatePrice();
});
addButton.addEventListener('click', function () {
if (buttonText.textContent === 'Добавить') {
this.classList.add('clicked');
buttonText.textContent = 'В корзину';
} else if (buttonText.textContent === 'В корзину') {
window.location.href = '/korzina';
}
});
// Инициализация отображения цены
updatePrice();
});