/* Whole-ruble pricing release; paired with configurator-data-v2.js. */ (function () { 'use strict'; var catalog = window.XiotEnergyCatalog; var container = document.getElementById('breakerRows'); if (!catalog || !container) return; var rows = []; var nextId = 0; var selectedPanel = catalog.panels[0]; var addButton = document.getElementById('addBreaker'); var orderButton = document.querySelector('[data-config-request]'); function money(kopecks) { return new Intl.NumberFormat('ru-RU', { style: 'currency', currency: 'RUB', minimumFractionDigits: 0, maximumFractionDigits: 0 }).format(kopecks / 100); } function name(item) { return item.model + ' · ' + item.poles + ' · ' + item.curve + item.amps + (item.leakage ? ' · ' + item.leakage : ''); } function element(tag, className, text) { var node = document.createElement(tag); if (className) node.className = className; if (text !== undefined) node.textContent = text; return node; } function getSelection() { var breakers = rows.map(function (row) { return { sku: row.item.sku, name: 'CHINT ' + name(row.item), quantity: row.quantity, unitPriceKopecks: row.item.priceKopecks, totalKopecks: row.item.priceKopecks * row.quantity }; }); var breakerTotal = breakers.reduce(function (sum, item) { return sum + item.totalKopecks; }, 0); return { priceDate: catalog.priceDate, panel: { id: selectedPanel.id, name: selectedPanel.name, quantity: 1, unitPriceKopecks: selectedPanel.priceKopecks }, breakers: breakers, breakerTotalKopecks: breakerTotal, totalKopecks: selectedPanel.priceKopecks + breakerTotal }; } function selectionText(selection) { var lines = [ 'Состав заказа:', 'Панель + ПО XIOT-ENERGY: ' + selection.panel.name + ' — 1 шт. × ' + money(selection.panel.unitPriceKopecks) + ' = ' + money(selection.panel.unitPriceKopecks) ]; selection.breakers.forEach(function (item) { lines.push(item.name + ', арт. ' + item.sku + ' — ' + item.quantity + ' шт. × ' + money(item.unitPriceKopecks) + ' = ' + money(item.totalKopecks)); }); if (!selection.breakers.length) lines.push('Автоматы не выбраны — заказ только панели.'); lines.push('Итого по оборудованию: ' + money(selection.totalKopecks)); lines.push('Цены CHINT предварительные, прайс на ' + selection.priceDate + '. Стоимость и срок поставки требуют подтверждения XIOT.'); lines.push('Без монтажа, пусконаладки, доставки и прочего оборудования щита.'); return lines.join('\n'); } function update() { var selection = getSelection(); var count = selection.breakers.reduce(function (sum, item) { return sum + item.quantity; }, 0); document.getElementById('configPanelName').textContent = selectedPanel.name; document.getElementById('configPanelPrice').textContent = money(selectedPanel.priceKopecks); document.getElementById('configBreakerPrice').textContent = money(selection.breakerTotalKopecks); document.getElementById('configTotal').textContent = money(selection.totalKopecks); document.getElementById('configBreakerCount').textContent = count + ' шт.'; document.getElementById('configSummaryCount').textContent = count ? '· ' + count + ' шт.' : '· не выбраны'; document.getElementById('configEmpty').hidden = rows.length !== 0; addButton.disabled = rows.length >= catalog.breakers.length; orderButton.disabled = rows.some(function (row) { return !row.input.checkValidity(); }); rows.forEach(function (row) { row.detail.textContent = 'Артикул ' + row.item.sku + ' · 6 кА · ' + (row.item.leakage ? 'с дифференциальной защитой' : 'без дифференциальной защиты'); row.unit.textContent = money(row.item.priceKopecks) + ' / шт.'; row.total.textContent = money(row.item.priceKopecks * row.quantity); row.minus.disabled = row.quantity <= 1; row.plus.disabled = row.quantity >= 99; var description = name(row.item) + ', артикул ' + row.item.sku; row.input.setAttribute('aria-label', 'Количество ' + description + ', от 1 до 99'); row.minus.setAttribute('aria-label', 'Уменьшить количество ' + description); row.plus.setAttribute('aria-label', 'Увеличить количество ' + description); row.remove.setAttribute('aria-label', 'Убрать ' + description); Array.prototype.forEach.call(row.select.options, function (option) { option.disabled = rows.some(function (other) { return other !== row && other.item.sku === option.value; }); }); }); document.querySelectorAll('[data-panel-card]').forEach(function (card) { card.classList.toggle('is-selected', card.getAttribute('data-panel-card') === selectedPanel.id); }); } function addRow(shouldFocus) { var item = catalog.breakers.find(function (candidate) { return !rows.some(function (row) { return row.item.sku === candidate.sku; }); }); if (!item) return; var id = 'breaker-' + (++nextId); var row = { item: item, quantity: 1 }; var article = element('article', 'breaker-row'); var field = element('div', 'breaker-select-field'); var label = element('label', '', 'Модель и исполнение'); label.htmlFor = id; row.select = element('select'); row.select.id = id; catalog.breakers.forEach(function (candidate) { var option = element('option', '', name(candidate)); option.value = candidate.sku; option.selected = candidate.sku === item.sku; row.select.appendChild(option); }); field.append(label, row.select); row.detail = element('p', 'breaker-detail'); row.detail.id = id + '-detail'; row.select.setAttribute('aria-describedby', row.detail.id); var bottom = element('div', 'breaker-row-bottom'); var quantity = element('div', 'breaker-quantity'); row.minus = element('button', '', '−'); row.plus = element('button', '', '+'); row.minus.type = row.plus.type = 'button'; row.minus.setAttribute('aria-label', 'Уменьшить количество автоматов'); row.plus.setAttribute('aria-label', 'Увеличить количество автоматов'); row.input = element('input'); row.input.type = 'number'; row.input.inputMode = 'numeric'; row.input.min = '1'; row.input.max = '99'; row.input.step = '1'; row.input.required = true; row.input.value = '1'; row.input.setAttribute('aria-label', 'Количество автоматов, от 1 до 99'); quantity.append(row.minus, row.input, row.plus); var price = element('div', 'breaker-row-price'); row.unit = element('small'); row.total = element('strong'); price.append(row.unit, row.total); var remove = element('button', 'breaker-remove', 'Убрать'); row.remove = remove; remove.type = 'button'; bottom.append(quantity, price, remove); article.append(field, row.detail, bottom); container.appendChild(article); rows.push(row); row.select.addEventListener('change', function () { row.item = catalog.breakers.find(function (candidate) { return candidate.sku === row.select.value; }); update(); }); function setQuantity(value) { row.quantity = Math.max(1, Math.min(99, Math.floor(Number(value) || 1))); row.input.value = String(row.quantity); row.input.removeAttribute('aria-invalid'); update(); } row.input.addEventListener('input', function () { if (row.input.checkValidity()) { row.quantity = Number(row.input.value); row.input.removeAttribute('aria-invalid'); } else row.input.setAttribute('aria-invalid', 'true'); update(); }); row.input.addEventListener('change', function () { setQuantity(row.input.value); }); row.minus.addEventListener('click', function () { setQuantity(row.quantity - 1); }); row.plus.addEventListener('click', function () { setQuantity(row.quantity + 1); }); remove.addEventListener('click', function () { rows = rows.filter(function (candidate) { return candidate !== row; }); article.remove(); update(); addButton.focus({ preventScroll: true }); }); update(); if (shouldFocus) row.select.focus({ preventScroll: true }); } document.querySelectorAll('[name="energyPanel"]').forEach(function (input) { // Restore the browser's radio state when returning via history. if (input.checked) selectedPanel = catalog.panels.find(function (panel) { return panel.id === input.value; }); input.addEventListener('change', function () { if (input.checked) selectedPanel = catalog.panels.find(function (panel) { return panel.id === input.value; }); update(); }); }); document.querySelectorAll('[data-product-view]').forEach(function (button) { button.addEventListener('click', function () { var card = button.closest('[data-panel-card]'); var view = button.getAttribute('data-product-view'); card.querySelectorAll('[data-product-photo]').forEach(function (photo) { photo.hidden = photo.getAttribute('data-product-photo') !== view; }); card.querySelectorAll('[data-product-view]').forEach(function (control) { control.setAttribute('aria-pressed', String(control === button)); }); }); }); addButton.addEventListener('click', function () { addRow(true); }); window.XiotEnergyConfigurator = { getSelection: getSelection, selectionText: selectionText }; addRow(false); document.getElementById('configContent').hidden = false; })();