document.addEventListener('DOMContentLoaded', function() {
    document.querySelectorAll('.ss-leadtime-table input[type="number"]').forEach(function(input) {
        function updateHint() {
            var days = parseInt(input.value, 10);
            var hint = input.closest('tr') ? input.closest('tr').querySelector('.ss-leadtime-hint') : null;
            if (!hint) { return; }
            if (!isNaN(days) && days > 0) {
                hint.textContent = days + ' ' + (days === 1 ? sensiblestockAdmin.day : sensiblestockAdmin.days) + ' ' + sensiblestockAdmin.buffer;
                hint.style.color = '#1e6b34';
            } else {
                hint.textContent = sensiblestockAdmin.noBuffer;
                hint.style.color = '';
            }
        }
        input.addEventListener('input', updateHint);
        updateHint();
    });

    function updateOrderTotals() {
        var grand = 0;
        document.querySelectorAll('.ss-order-row').forEach(function(row) {
            var input = row.querySelector('.ss-order-qty-input');
            if (!input) { return; }
            var cost = parseFloat(row.getAttribute('data-cost')) || 0;
            var qty = parseInt(input.value, 10) || 0;
            var advised = parseInt(input.getAttribute('data-advised'), 10) || 0;
            var total = cost * qty;
            grand += total;
            var cell = row.querySelector('.ss-order-linetotal');
            if (cell) { cell.textContent = total > 0 ? '€' + total.toFixed(2).replace('.', ',') : '—'; }
            if (qty < advised) {
                input.style.borderColor = '#d63638';
                input.style.background = '#fdf0f0';
            } else if (qty > advised) {
                input.style.borderColor = '#1e6b34';
                input.style.background = '#f0f6f0';
            } else {
                input.style.borderColor = '';
                input.style.background = '';
            }
        });
        var el = document.getElementById('ss-order-grand-total');
        if (el) { el.textContent = grand > 0 ? '€' + grand.toFixed(2).replace('.', ',') : '—'; }
    }
    document.querySelectorAll('.ss-order-qty-input').forEach(function(input) {
        input.addEventListener('input', updateOrderTotals);
    });
    updateOrderTotals();

    document.querySelectorAll('.ss-receiving-input').forEach(function(input) {
        input.addEventListener('input', function() {
            var row = this.closest('tr');
            if (!row) { return; }
            var current = parseInt(row.getAttribute('data-current'), 10) || 0;
            var received = parseInt(this.value, 10) || 0;
            var advised = parseInt(this.getAttribute('data-advised'), 10) || 0;
            var target = row.querySelector('.ss-new-stock');
            if (target) { target.textContent = current + received; }
            if (received < advised) {
                this.style.borderColor = '#d63638';
                this.style.background = '#fdf0f0';
            } else {
                this.style.borderColor = '';
                this.style.background = '';
            }
        });
    });

    document.querySelectorAll('.ss-copy-order').forEach(function(button) {
        button.addEventListener('click', async function() {
            var text = this.getAttribute('data-copy') || '';
            try {
                await navigator.clipboard.writeText(text);
                var original = this.textContent;
                this.textContent = sensiblestockAdmin.copied;
                setTimeout(function() { button.textContent = original; }, 1500);
            } catch (e) {
                window.prompt(sensiblestockAdmin.copyPrompt, text);
            }
        });
    });
});
