// Подтверждение удаления document.addEventListener("click", (event) => { const button = event.target.closest("[data-confirm]"); if (!button) return; if (!window.confirm(button.getAttribute("data-confirm"))) { event.preventDefault(); } }); // Автоматический расчет стоимости товара при изменении полей function calculateProductPrice() { const metalSelect = document.getElementById('metal_type_id'); const weightInput = document.getElementById('weight_grams'); const complexityInput = document.getElementById('complexity_factor'); const priceDisplay = document.getElementById('calculated_price_preview'); if (metalSelect && weightInput && complexityInput && priceDisplay) { const selectedOption = metalSelect.options[metalSelect.selectedIndex]; const pricePerGram = parseFloat(selectedOption?.dataset?.pricePerGram || 0); const purity = parseFloat(selectedOption?.dataset?.purity || 0); const weight = parseFloat(weightInput.value) || 0; const complexity = parseFloat(complexityInput.value) || 1; if (pricePerGram > 0 && purity > 0 && weight > 0) { const price = pricePerGram * weight * (purity / 1000) * complexity; priceDisplay.innerText = price.toFixed(2) + ' ₽'; } else { priceDisplay.innerText = '—'; } } } // Расчет скидки партнера в форме заявки function calculatePartnerDiscount(totalSales) { if (totalSales > 2000000) return 15; if (totalSales > 1000000) return 10; if (totalSales > 500000) return 5; return 0; } // Обновление предварительного расчета в форме заявки function updateOrderPreview() { const partnerSelect = document.getElementById('partner_id'); const itemsContainer = document.getElementById('items_container'); if (!partnerSelect || !itemsContainer) return; let subtotal = 0; const rows = itemsContainer.querySelectorAll('.item-row'); rows.forEach(row => { const productSelect = row.querySelector('select[name="product_id[]"]'); const quantityInput = row.querySelector('input[name="quantity[]"]'); if (productSelect && productSelect.value && quantityInput && quantityInput.value) { const price = parseFloat(productSelect.options[productSelect.selectedIndex]?.dataset?.price || 0); const quantity = parseFloat(quantityInput.value) || 0; subtotal += price * quantity; } }); let discount = 0; if (partnerSelect.value) { const totalSales = parseFloat(partnerSelect.options[partnerSelect.selectedIndex]?.dataset?.sales || 0); discount = calculatePartnerDiscount(totalSales); } const total = subtotal * (1 - discount / 100); const subtotalSpan = document.getElementById('preview_subtotal'); const discountSpan = document.getElementById('preview_discount'); const totalSpan = document.getElementById('preview_total'); if (subtotalSpan) subtotalSpan.innerText = subtotal.toFixed(2); if (discountSpan) discountSpan.innerText = discount; if (totalSpan) totalSpan.innerText = total.toFixed(2); } // Добавление строки товара в заявку function addOrderItemRow() { const container = document.getElementById('items_container'); if (!container) return; const template = document.getElementById('item_row_template'); if (!template) return; const newRow = template.content.cloneNode(true); container.appendChild(newRow); attachItemEvents(container.lastElementChild); updateOrderPreview(); } // Удаление строки товара function removeOrderItem(button) { const container = document.getElementById('items_container'); const row = button.closest('.item-row'); if (container.children.length > 1) { row.remove(); updateOrderPreview(); } else { alert('Должен быть хотя бы один товар в заявке'); } } // Привязка событий к строке товара function attachItemEvents(row) { const select = row.querySelector('select[name="product_id[]"]'); const quantity = row.querySelector('input[name="quantity[]"]'); const removeBtn = row.querySelector('.remove-item'); if (select) select.addEventListener('change', updateOrderPreview); if (quantity) quantity.addEventListener('input', updateOrderPreview); if (removeBtn) { removeBtn.addEventListener('click', () => removeOrderItem(removeBtn)); } } // Инициализация всех динамических элементов document.addEventListener('DOMContentLoaded', function() { // Для формы товара const metalSelect = document.getElementById('metal_type_id'); const weightInput = document.getElementById('weight_grams'); const complexityInput = document.getElementById('complexity_factor'); if (metalSelect) metalSelect.addEventListener('change', calculateProductPrice); if (weightInput) weightInput.addEventListener('input', calculateProductPrice); if (complexityInput) complexityInput.addEventListener('input', calculateProductPrice); calculateProductPrice(); // Для формы заявки const partnerSelect = document.getElementById('partner_id'); if (partnerSelect) partnerSelect.addEventListener('change', updateOrderPreview); const addItemBtn = document.getElementById('add_item_btn'); if (addItemBtn) addItemBtn.addEventListener('click', addOrderItemRow); const existingRows = document.querySelectorAll('#items_container .item-row'); existingRows.forEach(row => attachItemEvents(row)); updateOrderPreview(); });