const $pricingRows = $('#pricing-info-rows'); let pricingRowIndex = $pricingRows.find('.pricing-info-row').length; function calculatePricingLabel($row) { const variantText = $row.find('.pricing-variant-select option:selected').text().trim(); const unitText = $row.find('.pricing-unit-select option:selected').text().trim(); const variant = variantText === 'Select Variant' ? '' : variantText; const unit = unitText === 'Select Unit' ? '' : unitText; $row.find('.pricing-label-input').val(variant && unit ? variant + unit : ''); } function calculateDiscount($row) { const mrp = parseFloat($row.find('.pricing-mrp').val()) || 0; const $sellingInput = $row.find('.pricing-selling-price'); const sellingRaw = $sellingInput.val(); const sellingPrice = parseFloat(sellingRaw) || 0; const $discount = $row.find('.pricing-discount-display'); const isInvalid = sellingRaw !== '' && sellingPrice > mrp; $sellingInput.toggleClass('is-invalid', isInvalid); if (sellingRaw === '' || mrp <= 0 || isInvalid) { $discount.val(''); return; } const discount = ((mrp - sellingPrice) / mrp) * 100; $discount.val(discount.toFixed(2) + '%'); } function syncDefaultPricingToMainFields() { const $defaultRow = $pricingRows.find('.pricing-default-radio:checked').closest('.pricing-info-row'); const mrp = $defaultRow.find('.pricing-mrp').val(); const sellingPrice = $defaultRow.find('.pricing-selling-price').val(); if (mrp !== '') { $('#old_amount').val(mrp); } if (sellingPrice !== '') { $('#amount').val(sellingPrice); } } function updatePricingDefaultValues() { const selectedIndex = $pricingRows.find('.pricing-default-radio:checked').val(); $pricingRows.find('.pricing-info-row').each(function() { const isDefault = $(this).find('.pricing-default-radio').val() === selectedIndex ? 1 : 0; $(this).find('.pricing-default-value').val(isDefault); }); syncDefaultPricingToMainFields(); } function updatePricingRemoveButtons() { const isSingleRow = $pricingRows.find('.pricing-info-row').length === 1; $pricingRows.find('.remove-pricing-row').prop('disabled', isSingleRow); } function preparePricingRow($row, index) { $row.attr('data-index', index); $row.find('.pricing-default-radio').val(index).prop('checked', false); $row.find('.pricing-default-value') .attr('name', `product_pricing[${index}][is_default]`) .val(0); $row.find('.pricing-input').each(function() { const field = $(this).data('field'); $(this).attr('name', `product_pricing[${index}][${field}]`); }); $row.find('select').prop('selectedIndex', 0); $row.find('[data-field="status"]').val('1'); $row.find('input[type="text"], input[type="number"]').val(''); } // Renumber every row to its current DOM position so the posted product_pricing[] array // (and each row's sort_order) follows the drag-and-drop order. Keeps the checked default. function reindexPricingRows() { $pricingRows.find('.pricing-info-row').each(function(position) { const $row = $(this); const $radio = $row.find('.pricing-default-radio'); const wasChecked = $radio.is(':checked'); $row.attr('data-index', position); $radio.val(position).prop('checked', wasChecked); $row.find('.pricing-default-value').attr('name', `product_pricing[${position}][is_default]`); $row.find('.pricing-input').each(function() { $(this).attr('name', `product_pricing[${position}][${$(this).data('field')}]`); }); $row.find('.pricing-sort-order').val(position); }); pricingRowIndex = $pricingRows.find('.pricing-info-row').length; } if ($.fn.sortable) { $pricingRows.sortable({ handle: '.pricing-drag-handle', axis: 'y', items: '> tr.pricing-info-row', placeholder: 'ui-sortable-placeholder', helper: function(e, $tr) { // Freeze cell widths so the dragged row doesn't collapse const $originals = $tr.children(); const $helper = $tr.clone(); $helper.children().each(function(i) { $(this).width($originals.eq(i).width()); }); return $helper; }, update: function() { reindexPricingRows(); updatePricingDefaultValues(); } }); } $('#add-pricing-row').on('click', function() { const $newRow = $pricingRows.find('.pricing-info-row:first').clone(false, false); preparePricingRow($newRow, pricingRowIndex); $pricingRows.append($newRow); pricingRowIndex++; reindexPricingRows(); updatePricingRemoveButtons(); updatePricingDefaultValues(); syncDefaultPricingToMainFields(); }); $(document).on('click', '.remove-pricing-row', function() { if ($pricingRows.find('.pricing-info-row').length === 1) { return; } const $row = $(this).closest('.pricing-info-row'); const wasDefault = $row.find('.pricing-default-radio').is(':checked'); $row.remove(); if (wasDefault) { $pricingRows.find('.pricing-info-row:first .pricing-default-radio').prop('checked', true); } reindexPricingRows(); updatePricingRemoveButtons(); updatePricingDefaultValues(); syncDefaultPricingToMainFields(); }); $(document).on('change', '.pricing-default-radio', updatePricingDefaultValues); $(document).on('change', '.pricing-variant-select, .pricing-unit-select', function() { calculatePricingLabel($(this).closest('.pricing-info-row')); }); $(document).on('input change', '.pricing-mrp, .pricing-selling-price', function() { calculateDiscount($(this).closest('.pricing-info-row')); syncDefaultPricingToMainFields(); }); $pricingRows.find('.pricing-info-row').each(function() { calculatePricingLabel($(this)); calculateDiscount($(this)); }); reindexPricingRows(); updatePricingRemoveButtons(); updatePricingDefaultValues(); syncDefaultPricingToMainFields();