// Cache management for admin.js (only for role 1)
function clearJsCache(){
    if(window.userRole!==0 && window.userRole!==1){ showToast('Nur Rang 1 kann Cache leeren', 'error'); return; }
    const btn = document.getElementById('clearCacheBtn');
    if(btn){ btn.disabled = true; btn.innerHTML = '<i class="fa fa-spinner fa-spin"></i> Leere Cache...'; }
    fetch('?action=clearCache').then(r=>r.json()).then(j=>{
        if(j && j.success){
            showToast('Cache geleert — Seite wird neu geladen...', 'success');
            setTimeout(()=>{ location.reload(true); }, 1000);
        } else {
            showToast('Cache leeren fehlgeschlagen: ' + (j.error || 'unknown'), 'error');
            if(btn){ btn.disabled = false; btn.innerHTML = '<i class="fa fa-sync-alt"></i> JS-Cache leeren'; }
        }
    }).catch(e=>{
        showToast('Netzwerkfehler: '+e.message, 'error');
        if(btn){ btn.disabled = false; btn.innerHTML = '<i class="fa fa-sync-alt"></i> JS-Cache leeren'; }
    });
}

// Initialize cache clear button
document.addEventListener('DOMContentLoaded', ()=>{
    const clearCacheBtn = document.getElementById('clearCacheBtn');
    if(clearCacheBtn){ clearCacheBtn.addEventListener('click', clearJsCache); }
    
    const clearCacheBtnHeader = document.getElementById('clearCacheBtnHeader');
    if(clearCacheBtnHeader){ clearCacheBtnHeader.addEventListener('click', clearJsCache); }
});
