<?php
// clear_cache.php
// A utility to forcefully clear client-side caches (Service Worker, LocalStorage, etc.)
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>تحديث النظام</title>
    <style>
        body { font-family: Tahoma, Arial, sans-serif; display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh; background-color: #f8f9fa; color: #333; margin: 0; }
        .box { background: white; padding: 30px; border-radius: 10px; box-shadow: 0 4px 15px rgba(0,0,0,0.1); text-align: center; }
        .spinner { border: 4px solid #f3f3f3; border-top: 4px solid #3498db; border-radius: 50%; width: 40px; height: 40px; animation: spin 1s linear infinite; margin: 20px auto; }
        @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
    </style>
</head>
<body>
    <div class="box">
        <h2 style="color: #27ae60;">جاري تحديث ومسح الذاكرة القديمة...</h2>
        <div class="spinner"></div>
        <p>الرجاء الانتظار ثانية واحدة وسيتم تحويلك للنظام.</p>
    </div>

    <script>
        // 1. Unregister all Service Workers
        if ('serviceWorker' in navigator) {
            navigator.serviceWorker.getRegistrations().then(function(registrations) {
                for(let registration of registrations) {
                    registration.unregister();
                }
            });
        }

        // 2. Clear Caches API
        if ('caches' in window) {
            caches.keys().then(function(names) {
                for (let name of names) {
                    caches.delete(name);
                }
            });
        }

        // 3. Optional: we DO NOT clear IndexedDB here to avoid losing offline data,
        // but we WILL clear the erp_db_versions so it forces a re-sync.
        localStorage.removeItem('erp_db_versions');
        
        // 4. Redirect with a timestamp to bust network cache
        setTimeout(() => {
            window.location.href = 'index.html?t=' + new Date().getTime();
        }, 1500);
    </script>
</body>
</html>
