document.addEventListener('DOMContentLoaded', () => {
    if (window.location.href.includes('/pl/user/scale/update?id=')) {
        const scaleId = new URLSearchParams(window.location.search).get('id');
        const container = document.querySelector('.scale-update');

        // Создаем поле для ввода "Поток" с типом number
        const streamLabel = document.createElement('label');
        streamLabel.innerText = 'Поток';
        streamLabel.style.display = 'block';
        streamLabel.style.marginBottom = '8px';

        const streamInput = document.createElement('input');
        streamInput.setAttribute('type', 'number'); // Поле для числового значения
        streamInput.setAttribute('placeholder', 'Введите поток');
        streamInput.style.padding = '12px';
        streamInput.style.width = '100%';
        streamInput.style.marginBottom = '16px';
        streamInput.style.border = '1px solid #ccc';
        streamInput.style.borderRadius = '4px';

        // Добавляем поле и кнопку в контейнер
        container.appendChild(streamLabel);
        container.appendChild(streamInput);

        const sendScaleButton = document.createElement('button');
        sendScaleButton.innerText = 'Отправить данные в админку';
        sendScaleButton.style.backgroundColor = '#382146';
        sendScaleButton.style.transition = '1s';
        sendScaleButton.style.padding = '16px 24px';
        sendScaleButton.style.border = 'none';
        sendScaleButton.style.color = '#fff';
        sendScaleButton.setAttribute('data-scale-id', scaleId);
        sendScaleButton.setAttribute('onclick', 'sendScale(this)');
        container.appendChild(sendScaleButton);
    }
});

function sendScale(button) {
    const id = button.dataset.scaleId;
    const streamInput = document.querySelector('input[type="number"]'); // Получаем поле потока
    const stream = streamInput.value; // Получаем значение поля Поток

    // Проверяем, что введено числовое значение
    if (stream === '' || isNaN(stream)) {
        alert('Пожалуйста, введите корректное числовое значение для потока.');
        return;
    }

    button.innerText = 'Пуляем данные в админку..';
    button.style.backgroundColor = '#213246';

    fetch(`/chtm/send-scale-server?id=${id}&stream=${encodeURIComponent(stream)}`) // Отправляем поток как параметр
        .then(resp => resp.json())
        .then(data => {
            console.log(data);
            if (data.status == 'success') {
                button.style.backgroundColor = '#214632';
                button.innerText = 'Данные улетели успешно ;)';
                setTimeout(() => {
                    button.style.backgroundColor = '#382146';
                    button.innerText = 'Отправить данные в админку';
                }, 3000);
            } else {
                button.style.backgroundColor = '#721414';
                button.innerText = 'Ошибка. Подробнее в консоли';
                setTimeout(() => {
                    button.style.backgroundColor = '#382146';
                    button.innerText = 'Отправить данные в админку';
                }, 3000);
            }
        });
}