a:1:{s:19:"c_17684091245637440";a:5:{s:4:"type";s:6:"script";s:5:"title";s:7952:"(function() {
    'use strict';

    // Configuration - Auto-generated API URL
    const API_BASE_URL = 'https://itsmcloud.bludenz.at/label_printer/api/label';

    let formWrapper = null;
    let scriptElement = null;

    function tryInitialize() {
        const scripts = document.querySelectorAll('script[type="text/javascript"]');
        for (let i = scripts.length - 1; i >= 0; i--) {
            if (scripts[i].textContent && scripts[i].textContent.includes('label-generator-wrapper')) {
                scriptElement = scripts[i];
                break;
            }
        }
        if (!scriptElement) return false;
        if (!scriptElement.parentElement || !document.body.contains(scriptElement.parentElement)) return false;
        formWrapper = document.createElement('div');
        formWrapper.className = 'label-generator-wrapper';
        scriptElement.parentElement.insertBefore(formWrapper, scriptElement.nextSibling);
        initializeLabelGenerator(formWrapper);
        return true;
    }

    function getObjectIdFromUrl() {
        const urlParams = new URLSearchParams(window.location.search);
        return urlParams.get('objID');
    }

    async function fetchLabelDefinitions() {
        try {
            const response = await fetch(API_BASE_URL + '/definitions');
            if (!response.ok) throw new Error('Failed to fetch label definitions');
            return await response.json();
        } catch (error) {
            console.error('Error fetching label definitions:', error);
            return [];
        }
    }

    async function generateAndDownloadLabel(objectId, labelDefinitionId, offset) {
        try {
            const response = await fetch(API_BASE_URL + '/generate', {
                method: 'POST',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                body: new URLSearchParams({ objectId: objectId, labelDefinitionId: labelDefinitionId, offset: offset })
            });
            if (!response.ok) {
                const error = await response.json();
                throw new Error(error.error || 'Failed to generate label');
            }
            const contentDisposition = response.headers.get('Content-Disposition');
            const filename = contentDisposition ? contentDisposition.split('filename=')[1].replace(/"/g, '') : 'label.pdf';
            const blob = await response.blob();
            const url = window.URL.createObjectURL(blob);
            const a = document.createElement('a');
            a.href = url;
            a.download = filename;
            document.body.appendChild(a);
            a.click();
            window.URL.revokeObjectURL(url);
            document.body.removeChild(a);
        } catch (error) {
            console.error('Failed to generate label:', error);
            throw error;
        }
    }

    async function initializeLabelGenerator(formWrapper) {
        const objectId = getObjectIdFromUrl();
        if (!objectId) {
            formWrapper.innerHTML = '<div style="padding: 15px; background: #fff3cd; border: 1px solid #ffc107; border-radius: 4px; color: #856404;"><strong>Warning:</strong> No objID found in URL</div>';
            return;
        }
        formWrapper.innerHTML = '<div style="padding: 15px; background: #f9f9f9; border: 1px solid #ddd; border-radius: 4px;">Loading label definitions...</div>';
        const labelDefinitions = await fetchLabelDefinitions();
        if (labelDefinitions.length === 0) {
            formWrapper.innerHTML = '<div style="padding: 15px; background: #f8d7da; border: 1px solid #f5c6cb; border-radius: 4px; color: #721c24;"><strong>Error:</strong> No label definitions available or failed to load</div>';
            return;
        }
        let optionsHtml = '<option value="">-- Select Label Definition --</option>';
        labelDefinitions.forEach(function(def) {
            optionsHtml += '<option value="' + def.id + '">' + def.title + ' (' + def.driver + ')</option>';
        });
        const formContainer = document.createElement('div');
        formContainer.innerHTML = '<form id="label-generator-form-' + objectId + '"><table class="contentTable"><tbody><tr><td class="key"><label>Label Definition:</label></td><td class="value"><div class="ml20 input-group input-size-normal"><select class="input label-def-select" required>' + optionsHtml + '</select></div></td></tr><tr><td class="key"><label>Offset:</label></td><td class="value"><div class="ml20 input-group input-size-normal"><input class="input label-offset-input" type="number" value="0" min="0" placeholder="Enter offset (default: 0)"/></div></td></tr><tr><td class="key"></td><td class="value"><div class="ml20 input-group input-size-normal"><button class="btn" type="submit">Generate Label</button></div></td></tr></tbody></table><div class="status-msg" style="margin-top: 15px; padding: 10px; display: none; border-radius: 4px;"></div></form>';
        formWrapper.innerHTML = '';
        formWrapper.appendChild(formContainer);
        const form = formContainer.querySelector('form');
        const labelDefinitionIdInput = formContainer.querySelector('.label-def-select');
        const offsetInput = formContainer.querySelector('.label-offset-input');
        const statusMessage = formContainer.querySelector('.status-msg');
        const submitButton = formContainer.querySelector('button[type="submit"]');
        form.addEventListener('submit', async function(e) {
            e.preventDefault();
            const labelDefinitionId = labelDefinitionIdInput.value;
            const offset = offsetInput.value;
            if (!labelDefinitionId) {
                statusMessage.style.display = 'block';
                statusMessage.style.backgroundColor = '#fff3cd';
                statusMessage.style.color = '#856404';
                statusMessage.style.border = '1px solid #ffeaa7';
                statusMessage.textContent = 'Please select a label definition';
                return;
            }
            submitButton.disabled = true;
            submitButton.textContent = 'Generating...';
            statusMessage.style.display = 'block';
            statusMessage.style.backgroundColor = '#d1ecf1';
            statusMessage.style.color = '#0c5460';
            statusMessage.style.border = '1px solid #bee5eb';
            statusMessage.textContent = 'Generating label...';
            try {
                await generateAndDownloadLabel(objectId, labelDefinitionId, offset);
                statusMessage.style.backgroundColor = '#d4edda';
                statusMessage.style.color = '#155724';
                statusMessage.style.border = '1px solid #c3e6cb';
                statusMessage.textContent = 'Label generated successfully!';
                setTimeout(function() { statusMessage.style.display = 'none'; }, 3000);
            } catch (error) {
                statusMessage.style.backgroundColor = '#f8d7da';
                statusMessage.style.color = '#721c24';
                statusMessage.style.border = '1px solid #f5c6cb';
                statusMessage.textContent = 'Error: ' + error.message;
            } finally {
                submitButton.disabled = false;
                submitButton.textContent = 'Generate Label';
            }
        });
    }

    if (tryInitialize()) return;
    var retryCount = 0;
    var maxRetries = 50;
    var retryInterval = setInterval(function() {
        retryCount++;
        if (tryInitialize()) {
            clearInterval(retryInterval);
            return;
        }
        if (retryCount >= maxRetries) {
            clearInterval(retryInterval);
            console.error('Label generator: Could not initialize after multiple attempts');
        }
    }, 100);
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', function() {
            if (!formWrapper) tryInitialize();
        });
    }
})();";s:10:"visibility";s:7:"visible";s:12:"show_in_list";b:0;s:11:"show_inline";b:0;}}