<style>
/* Для затемнения фона при открытом меню */
#sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: #0005;
    z-index: 39;
}
#sidebar-backdrop.active {
    display: block;
}
/* Ensure content doesn't overlap sidebar */
#main-content {
    position: relative;
    z-index: 10; /* Below sidebar (z-40) and backdrop (z-39) */
    transition: margin-left 0.2s ease-in-out;
}
@media (max-width: 767px) {
    #main-content.sidebar-open {
        margin-left: 16rem; /* w-64 = 256px, matches sidebar width */
    }
}
</style>
<script>
function openSidebar() {
    document.getElementById('adminSidebar').classList.remove('-translate-x-full');
    document.getElementById('sidebar-backdrop').classList.add('active');
    document.getElementById('main-content').classList.add('sidebar-open');
}
function closeSidebar() {
    document.getElementById('adminSidebar').classList.add('-translate-x-full');
    document.getElementById('sidebar-backdrop').classList.remove('active');
    document.getElementById('main-content').classList.remove('sidebar-open');
}
document.addEventListener('DOMContentLoaded', () => {
    const clearAllButton = document.getElementById('clear-all-button');
    if (clearAllButton) {
        clearAllButton.addEventListener('click', () => {
            if (!confirm('Вы уверены, что хотите удалить все заявки? Это действие нельзя отменить.')) {
                return;
            }
            console.log('Delete Requests button clicked');
            clearAllButton.disabled = true;

            fetch('clear_status.php', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({})
            })
            .then(response => {
                console.log('Clear status response:', response.status);
                if (!response.ok) throw new Error(`HTTP error ${response.status}`);
                return response.json();
            })
            .then(data => {
                console.log('Server response:', data);
                if (data.success) {
                    alert('Все заявки успешно удалены! Перезагрузка...');
                    window.location.reload();
                } else {
                    alert('Ошибка: ' + (data.error || 'Неизвестная ошибка'));
                    clearAllButton.disabled = false;
                }
            })
            .catch(error => {
                console.error('Fetch error:', error);
                alert('Ошибка: ' + error.message);
                clearAllButton.disabled = false;
            });
        });
    }
});
</script>

<!-- Кнопка бургера (только для мобилки) -->
<div class="fixed top-4 left-4 z-50 md:hidden">
    <button onclick="openSidebar()" class="p-2 rounded-lg bg-white shadow-lg">
        <svg class="w-7 h-7 text-blue-700" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" d="M4 6h16M4 12h16M4 18h16"/>
        </svg>
    </button>
</div>

<!-- Затемнение -->
<div id="sidebar-backdrop" onclick="closeSidebar()"></div>

<!-- Сайдбар -->
<div id="adminSidebar" class="fixed left-0 top-0 h-full w-64 bg-white border-r border-gray-200 shadow-lg flex flex-col justify-between z-40 transition-transform duration-200 -translate-x-full md:translate-x-0">
    <div>
        <div class="p-6 font-bold text-2xl text-blue-700 border-b border-gray-100 select-none">
            Admin Panel
        </div>
        <nav class="flex flex-col gap-2 p-4">
            <a href="admin_zayavka.php" class="block px-4 py-3 rounded-lg hover:bg-blue-50 transition text-gray-800 font-medium">
                📝 Заявки
            </a>
            <a href="admin_settings.php" class="block px-4 py-3 rounded-lg hover:bg-blue-50 transition text-gray-800 font-medium">
                ⚙️ Настройки
            </a>
            <button id="clear-all-button" class="block px-4 py-3 rounded-lg hover:bg-blue-50 transition text-gray-800 font-medium text-left">
                🗑️ Удалить заявки
            </button>
        </nav>
    </div>
    <div class="p-4 border-t border-gray-100">
        <a href="logout.php" class="block px-4 py-3 rounded-lg bg-red-100 hover:bg-red-200 text-red-700 font-semibold text-center transition">
            🚪 Выйти
        </a>
    </div>
</div>