window.initAddProviderUI = function() {
    // Initialization logic if any dynamic elements need binding in the future
};

window.saveNewProvider = function(btn) {
    const form = document.getElementById('addProviderForm');
    const fd = new FormData(form);
    
    // Explicitly grab the toggle state
    fd.set('is_active', form.elements['is_active'].checked ? '1' : '0');

    // Validation
    if (!fd.get('insurance_name').trim()) {
        window.showProvNotification('error', 'Please enter the name of the Insurance Provider.');
        return;
    }

    const origHTML = btn.innerHTML;
    btn.innerHTML = '<i class="fa-solid fa-spinner fa-spin"></i> Saving Provider...';
    btn.disabled = true;

    // Send to backend
    fetch('../../dripend/systemSetup/insurance/processCorporateInsuranceProvider.php', {
        method: 'POST',
        body: fd
    })
    .then(async response => {
        const text = await response.text();
        try {
            const data = JSON.parse(text);
            if(data.status === 'success') {
                window.showProvNotification('success', data.message);
                btn.innerHTML = '<i class="fa-solid fa-check"></i> Saved';
                btn.style.backgroundColor = '#16a34a';
                
                // Clear the form after success
                setTimeout(() => {
                    btn.innerHTML = origHTML;
                    btn.disabled = false;
                    btn.style.backgroundColor = '';
                    form.reset();
                    // Keep it active by default
                    form.elements['is_active'].checked = true;
                }, 2500);
            } else {
                window.showProvNotification('error', data.message);
                btn.innerHTML = origHTML;
                btn.disabled = false;
            }
        } catch (e) {
            console.error("Server Error:", text);
            window.showProvNotification('error', 'Server returned invalid JSON. Check console.');
            btn.innerHTML = origHTML;
            btn.disabled = false;
        }
    })
    .catch(err => {
        window.showProvNotification('error', 'Network error. Cannot reach server.');
        btn.innerHTML = origHTML;
        btn.disabled = false;
    });
};

window.showProvNotification = function(type, message) {
    const banner = document.getElementById('provNotificationBanner');
    if(!banner) return;
    
    banner.style.display = 'flex';
    if (type === 'success') {
        banner.style.backgroundColor = '#f0fdf4';
        banner.style.color = '#166534';
        banner.style.border = '1px solid #bbf7d0';
        banner.innerHTML = '<i class="fa-solid fa-circle-check"></i> ' + message;
    } else {
        banner.style.backgroundColor = '#fef2f2';
        banner.style.color = '#991b1b';
        banner.style.border = '1px solid #fecaca';
        banner.innerHTML = '<i class="fa-solid fa-triangle-exclamation"></i> <strong>Error:</strong> ' + message;
    }
    window.scrollTo({top: 0, behavior: 'smooth'});
    setTimeout(() => { banner.style.display = 'none'; }, 6000);
};