
// 1. Function to Load data from Database
function loadSettingSchool() {
    fetch("api/settings/get_settings.php")
        .then(res => res.json())
        .then(data => {
            if (data.status === true) {
                const s = data.data;
                // Update Dashboard Text
                document.getElementById('display_school_name').innerText = s.school_name;
                document.getElementById('display_address').innerText = s.school_address;
                document.getElementById('display_phone_number').innerText = s.phone_number;
                document.getElementById('display_school_email').innerText = s.email;

                document.getElementById('display_school_logo').src = 'uploads/settings/' + s.school_logo;

                // Pre-fill Modal Inputs
                document.getElementById('set_school_name').value = s.school_name || '';
                document.getElementById('set_email').value = s.email || '';
                document.getElementById('set_phone_number').value = s.phone_number || '';
                document.getElementById('set_address').value = s.school_address || '';
            }
        });
}

// 2. Function to Open Modal
function openSettingsModal() {
    const modalEl = document.getElementById('schoolSettingsModal');
    const modal = new bootstrap.Modal(modalEl);
    modal.show();
}

// 3. Form Submission
document.getElementById('schoolSettingsForm').addEventListener('submit', function(e) {
    e.preventDefault();
    const formData = new FormData(this);

    fetch("api/settings/update_settings.php", {
        method: "POST",
        body: formData
    })
    .then(res => res.json())
    .then(data => {
        if (data.status === true) {
            alert("Updated!");
            loadSettingSchool(); // Refresh display
            bootstrap.Modal.getInstance(document.getElementById('schoolSettingsModal')).hide();
        }
    });
});

// Run on page load
document.addEventListener('DOMContentLoaded', loadSettingSchool);

 

function injectSchoolBranding() {
    // We wrap everything in a try-catch to ignore browser extension errors
    try {
        fetch("api/settings/get_settings.php")
            .then(res => {
                if (!res.ok) throw new Error("Network response was not ok");
                return res.json();
            })
            .then(data => {
                if (data.status === true) {
                    const s = data.data;
                    const path = 'uploads/settings/';

                    // Use IDs if single receipt, or classes if multiple copies
                    const updateElement = (id, value, isImg = false) => {
                        const el = document.getElementById(id);
                        if (el) {
                            if (isImg) {
                                el.src = value ? path + value : '';
                                el.style.display = value ? 'inline-block' : 'none';
                            } else {
                                el.innerText = value || '';
                            }
                        }
                    };

                    updateElement('header_school_name', s.school_name);
                    updateElement('header_school_address', s.school_address);
                    updateElement('header_school_phone', s.phone_number);
                    updateElement('header_school_email', s.email);
                    updateElement('header_school_logo', s.school_logo, true);
                    updateElement('header_principal_sig', s.principal_signature, true);
                }
            })
            .catch(err => console.warn("Branding failed but page will continue:", err));
    } catch (e) {
        console.error("General JS Error:", e);
    }
}

document.addEventListener('DOMContentLoaded', injectSchoolBranding);