window.initMapCorporateUI = function() {
    // Bind the Copay Toggle
    const copayToggle = document.getElementById('copayToggle');
    const copaySection = document.getElementById('copaySection');
    
    if (copayToggle && copaySection) {
        copaySection.style.display = copayToggle.checked ? 'flex' : 'none';
        copayToggle.addEventListener('change', function() {
            copaySection.style.display = this.checked ? 'flex' : 'none';
        });
    }
};

window.saveCorporateMapping = function(btn) {
    const form = document.getElementById('mapCorporateForm');
    const fd = new FormData(form);
    
    // Explicitly grab checkbox state
    fd.set('is_copay_required', form.elements['is_copay_required'].checked ? '1' : '0');

    // Validation
    if (!fd.get('insurance_type_id') || !fd.get('service_id')) {
        window.showMapNotification('error', 'Please select both a Provider and a Procedure.');
        return;
    }

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

    // Send to backend
    fetch('../../dripend/systemSetup/insurance/processMapCorporateInsurance.php', {
        method: 'POST',
        body: fd
    })
    .then(async response => {
        const text = await response.text();
        try {
            const data = JSON.parse(text);
            if(data.status === 'success') {
                window.showMapNotification('success', data.message);
                btn.innerHTML = '<i class="fa-solid fa-check"></i> Saved';
                btn.style.backgroundColor = '#16a34a';
                
                // Reset form (keep provider selected for rapid data entry)
                setTimeout(() => {
                    btn.innerHTML = origHTML;
                    btn.disabled = false;
                    btn.style.backgroundColor = '';
                    form.elements['tariff_amount'].value = '';
                    form.elements['copay_amount_adult'].value = '';
                    form.elements['copay_amount_child'].value = '';
                }, 2000);
            } else {
                window.showMapNotification('error', data.message);
                btn.innerHTML = origHTML;
                btn.disabled = false;
            }
        } catch (e) {
            console.error("Server Error:", text);
            window.showMapNotification('error', 'Server returned invalid JSON. Check console.');
            btn.innerHTML = origHTML;
            btn.disabled = false;
        }
    })
    .catch(err => {
        window.showMapNotification('error', 'Network error. Cannot reach server.');
        btn.innerHTML = origHTML;
        btn.disabled = false;
    });
};

window.showMapNotification = function(type, message) {
    const banner = document.getElementById('mapNotificationBanner');
    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);
};