<?php
/**
 * FAB (Floating Action Button) Navigation
 * Bottom Navigation für Mobile
 */
?>

<style>
/* FAB Container - nur auf Mobile */
.fab-container {
    display: none;
}

@media (max-width: 768px) {
    .fab-container {
        display: block;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: 9999;
    }
}

/* Bottom Navigation Bar */
.fab-bottom-nav {
    background: white;
    border-top: 1px solid #e0e0e0;
    box-shadow: 0 -4px 12px rgba(0,0,0,0.08);
    display: flex;
    justify-content: space-around;
    align-items: center;
    padding: 8px 0 calc(8px + env(safe-area-inset-bottom));
    height: calc(60px + env(safe-area-inset-bottom));
}

.fab-nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    padding: 8px 12px;
    text-decoration: none;
    color: #666;
    font-size: 11px;
    font-weight: 500;
    transition: all 0.2s;
    cursor: pointer;
    border: none;
    background: none;
    min-width: 60px;
}

.fab-nav-item.active {
    color: #667eea;
}

.fab-nav-item:active {
    transform: scale(0.9);
}

.fab-nav-icon {
    font-size: 24px;
    line-height: 1;
}

.fab-nav-label {
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* Main FAB Button */
.fab-main {
    position: fixed;
    bottom: calc(70px + env(safe-area-inset-bottom));
    right: 20px;
    z-index: 10000;
}

.fab-button {
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border: none;
    font-size: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.fab-button:active {
    transform: scale(0.9);
}

.fab-button.rotate {
    transform: rotate(45deg);
}

/* FAB Menu */
.fab-menu {
    position: absolute;
    bottom: 70px;
    right: 0;
    display: none;
    flex-direction: column;
    gap: 12px;
    opacity: 0;
    transform: translateY(20px);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.fab-menu.active {
    display: flex;
    opacity: 1;
    transform: translateY(0);
}

.fab-menu-item {
    display: flex;
    align-items: center;
    gap: 12px;
    background: white;
    border-radius: 28px;
    padding: 12px 16px;
    text-decoration: none;
    color: #333;
    font-size: 14px;
    font-weight: 600;
    box-shadow: 0 2px 8px rgba(0,0,0,0.15);
    white-space: nowrap;
    transition: all 0.2s;
}

.fab-menu-item:active {
    transform: scale(0.95);
}

.fab-menu-item-icon {
    font-size: 20px;
    width: 24px;
    text-align: center;
}

/* Backdrop */
.fab-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.3);
    backdrop-filter: blur(2px);
    z-index: 9998;
    display: none;
    opacity: 0;
    transition: opacity 0.3s;
}

.fab-backdrop.active {
    display: block;
    opacity: 1;
}

/* Search Overlay */
.fab-search-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: white;
    z-index: 10001;
    display: none;
    padding: 16px;
    padding-top: calc(16px + env(safe-area-inset-top));
}

.fab-search-overlay.active {
    display: block;
}

.fab-search-header {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 16px;
}

.fab-search-input {
    flex: 1;
    padding: 12px 16px;
    border: 2px solid #e0e0e0;
    border-radius: 12px;
    font-size: 16px;
}

.fab-search-input:focus {
    outline: none;
    border-color: #667eea;
}

.fab-search-close {
    width: 44px;
    height: 44px;
    border: none;
    background: #f0f0f0;
    border-radius: 50%;
    font-size: 20px;
    cursor: pointer;
}
</style>

<!-- FAB Container -->
<div class="fab-container">
    <!-- Bottom Navigation -->
    <nav class="fab-bottom-nav">
        <a href="index.php" class="fab-nav-item active">
            <div class="fab-nav-icon">🏠</div>
            <div class="fab-nav-label">Start</div>
        </a>
        
        <button type="button" class="fab-nav-item" id="fabSearchBtn">
            <div class="fab-nav-icon">🔍</div>
            <div class="fab-nav-label">Suche</div>
        </button>
        
        <button type="button" class="fab-nav-item" id="fabFilterBtn">
            <div class="fab-nav-icon">⚙️</div>
            <div class="fab-nav-label">Filter</div>
        </button>
        
        <?php if (canEdit()): ?>
        <a href="backend/index.php" class="fab-nav-item">
            <div class="fab-nav-icon">👤</div>
            <div class="fab-nav-label">Admin</div>
        </a>
        <?php endif; ?>
    </nav>
    
    <!-- Main FAB Button -->
    <div class="fab-main">
        <button type="button" class="fab-button" id="fabMainBtn">
            ➕
        </button>
        
        <!-- FAB Menu -->
        <div class="fab-menu" id="fabMenu">
            <a href="add.php" class="fab-menu-item">
                <span class="fab-menu-item-icon">➕</span>
                Neuer Eintrag
            </a>
            
            <?php if (canEdit()): ?>
            <a href="backend/categories.php" class="fab-menu-item">
                <span class="fab-menu-item-icon">📦</span>
                Kategorien
            </a>
            
            <a href="backend/locations.php" class="fab-menu-item">
                <span class="fab-menu-item-icon">📍</span>
                Orte
            </a>
            
            <a href="export_csv.php" class="fab-menu-item">
                <span class="fab-menu-item-icon">📤</span>
                CSV Export
            </a>
            
            <a href="export_pdf.php" class="fab-menu-item">
                <span class="fab-menu-item-icon">📄</span>
                PDF Export
            </a>
            <?php endif; ?>
        </div>
    </div>
    
    <!-- Backdrop -->
    <div class="fab-backdrop" id="fabBackdrop"></div>
</div>

<!-- Search Overlay -->
<div class="fab-search-overlay" id="fabSearchOverlay">
    <div class="fab-search-header">
        <label for="fabSearchInput" class="sr-only"><?php echo t('filter_placeholder_search'); ?></label>
        <input type="text" 
               class="fab-search-input" 
               id="fabSearchInput"
               placeholder="<?php echo htmlspecialchars(t('filter_placeholder_search')); ?>"
               aria-label="<?php echo htmlspecialchars(t('filter_placeholder_search')); ?>"
               autocomplete="off">
        <button type="button" class="fab-search-close" id="fabSearchClose">✕</button>
    </div>
    <div id="fabSearchResults"></div>
</div>

<script>
// FAB Navigation JavaScript
document.addEventListener('DOMContentLoaded', function() {
    if (window.innerWidth > 768) return; // Nur auf Mobile
    
    const fabMainBtn = document.getElementById('fabMainBtn');
    const fabMenu = document.getElementById('fabMenu');
    const fabBackdrop = document.getElementById('fabBackdrop');
    const fabSearchBtn = document.getElementById('fabSearchBtn');
    const fabSearchOverlay = document.getElementById('fabSearchOverlay');
    const fabSearchClose = document.getElementById('fabSearchClose');
    const fabSearchInput = document.getElementById('fabSearchInput');
    const fabFilterBtn = document.getElementById('fabFilterBtn');
    
    // Main FAB Toggle
    if (fabMainBtn) {
        fabMainBtn.addEventListener('click', function() {
            const isOpen = fabMenu.classList.contains('active');
            
            if (isOpen) {
                closeFabMenu();
            } else {
                openFabMenu();
            }
        });
    }
    
    // Backdrop Click
    if (fabBackdrop) {
        fabBackdrop.addEventListener('click', closeFabMenu);
    }
    
    function openFabMenu() {
        fabMenu.classList.add('active');
        fabBackdrop.classList.add('active');
        fabMainBtn.classList.add('rotate');
        fabMainBtn.textContent = '✕';
    }
    
    function closeFabMenu() {
        fabMenu.classList.remove('active');
        fabBackdrop.classList.remove('active');
        fabMainBtn.classList.remove('rotate');
        fabMainBtn.textContent = '➕';
    }
    
    // Search Button
    if (fabSearchBtn) {
        fabSearchBtn.addEventListener('click', function() {
            fabSearchOverlay.classList.add('active');
            setTimeout(() => fabSearchInput.focus(), 100);
        });
    }
    
    // Search Close
    if (fabSearchClose) {
        fabSearchClose.addEventListener('click', function() {
            fabSearchOverlay.classList.remove('active');
            fabSearchInput.value = '';
        });
    }
    
    // Search Input - nutze existierende Suche
    if (fabSearchInput) {
        fabSearchInput.addEventListener('input', function(e) {
            const searchInput = document.querySelector('input[name="search"]');
            if (searchInput) {
                searchInput.value = e.target.value;
            }
        });
        
        fabSearchInput.addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                const searchForm = document.querySelector('form[method="GET"]');
                if (searchForm) {
                    searchForm.submit();
                }
            }
        });
    }
    
    // Filter Button - öffne Column Manager
    if (fabFilterBtn) {
        fabFilterBtn.addEventListener('click', function() {
            const columnDropdown = document.getElementById('columnsSelectorDropdown');
            if (columnDropdown) {
                columnDropdown.classList.toggle('active');
                columnDropdown.style.display = columnDropdown.style.display === 'block' ? 'none' : 'block';
            }
        });
    }
    
    // Schließe bei ESC
    document.addEventListener('keydown', function(e) {
        if (e.key === 'Escape') {
            closeFabMenu();
            fabSearchOverlay.classList.remove('active');
        }
    });
});
</script>
