(function() {
    // Enforce modular scoping safety rules
    window.initAddLabSample = function() {
        const form = document.getElementById('addLabSampleForm');
        if (!form) return;

        form.removeEventListener('submit', window.submitLabSampleViaAjax);
        form.addEventListener('submit', window.submitLabSampleViaAjax);
    };

    window.submitLabSampleViaAjax = function(event) {
        event.preventDefault();

        const form = event.currentTarget;
        const submitBtn = document.getElementById('btnSubmitLabSample');
        const errorAlert = document.getElementById('addSampleErrorAlert');
        const successAlert = document.getElementById('addSampleSuccessAlert');

        errorAlert.style.display = 'none';
        successAlert.style.display = 'none';

        // Core Input Validation Check
        const sampleNameInput = document.getElementById('sample_name');
        if (!sampleNameInput || sampleNameInput.value.trim() === '') {
            errorAlert.innerHTML = '<i class="fa-solid fa-circle-exclamation"></i> Sample Name is required.';
            errorAlert.style.display = 'flex';
            sampleNameInput.focus();
            return;
        }

        // Prevent dual thread execution collisions
        if (submitBtn) {
            submitBtn.disabled = true;
            submitBtn.innerHTML = '<i class="fa-solid fa-spinner fa-spin"></i> Saving Sample...';
        }

        const formData = new FormData(form);
        const appRoot = window.rimaxAppRoot || '/rimax/application';

        fetch(appRoot + '/dripend/systemSetup/laboratory/processLabSamples.php', {
            method: 'POST',
            body: formData
        })
        .then(res => {
            if (!res.ok) {
                throw new Error('Network response encountered server-side error code: ' + res.status);
            }
            return res.json();
        })
        .then(res => {
            if (res.status === 'success') {
                successAlert.innerHTML = '<i class="fa-solid fa-circle-check"></i> ' + res.message;
                successAlert.style.display = 'flex';
                form.reset();
                
                // Return cleanly back to main table framework after brief confirmation delay
                setTimeout(() => {
                    if (typeof loadContent === 'function') {
                        loadContent('Laboratory Settings', 'laboratory/labSetup.php', 'Laboratory', 'system_setup', null, true);
                    }
                }, 1200);
            } else {
                errorAlert.innerHTML = '<i class="fa-solid fa-triangle-exclamation"></i> ' + res.message;
                errorAlert.style.display = 'flex';
                if (submitBtn) {
                    submitBtn.disabled = false;
                    submitBtn.innerHTML = '<i class="fa-solid fa-floppy-disk"></i> Save Sample Type';
                }
            }
        })
        .catch(err => {
            errorAlert.innerHTML = '<i class="fa-solid fa-wifi"></i> Connection failed: ' + err.message;
            errorAlert.style.display = 'flex';
            if (submitBtn) {
                submitBtn.disabled = false;
                submitBtn.innerHTML = '<i class="fa-solid fa-floppy-disk"></i> Save Sample Type';
            }
        });
    };
})();