<?php 
// Christo Lochenberg Developments | info@christodev.co.za | +27 61 523 9126
$appSettings = getSettings(); 

// Dynamic routing for the main logo based on role
$logoLink = URLROOT;
if (isLoggedIn()) {
    if (isAdmin()) {
        $logoLink .= '/admin/index';
    } elseif (isClient()) {
        $logoLink .= '/clientdashboard/index';
    } elseif (isPartner()) {
        $logoLink .= '/partnerDashboard/index';
    } else {
        $logoLink .= '/dashboard/index';
    }
}
?>
<?php if(isset($_SESSION['admin_impersonating'])): ?>
    <div class="bg-indigo-600 dark:bg-indigo-800 text-white text-center py-2 px-4 text-sm font-bold flex flex-col sm:flex-row justify-center items-center shadow-md relative z-[60]">
        <span class="mr-0 sm:mr-4 mb-2 sm:mb-0">You are currently impersonating: <?php echo $_SESSION['user_name']; ?></span>
        <form action="<?php echo URLROOT; ?>/admin/leaveImpersonation" method="POST" class="m-0 p-0">
            <button type="submit" class="bg-white dark:bg-gray-100 text-indigo-600 dark:text-indigo-800 hover:bg-gray-200 px-4 py-1.5 rounded shadow-sm text-xs transition-colors">Return to Admin</button>
        </form>
    </div>
<?php endif; ?>

<nav class="bg-white dark:bg-gray-800 shadow-sm z-50 relative border-b border-gray-200 dark:border-gray-700/70">
    
    <div class="<?php echo isLoggedIn() ? 'w-full px-4 md:px-8 py-4' : 'container mx-auto px-4 py-4'; ?> flex justify-between items-center relative z-50">
        
        <div class="flex items-center">
            <?php if(isLoggedIn()) : ?>
                <button id="mobile-menu-btn" class="mr-3 md:hidden text-gray-600 dark:text-gray-300 hover:text-primary dark:hover:text-primary focus:outline-none focus:ring-2 focus:ring-primary rounded-lg p-1 transition-colors">
                    <svg class="w-7 h-7" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16"></path>
                    </svg>
                </button>
            <?php endif; ?>

            <a href="<?php echo $logoLink; ?>" class="flex items-center group">
                <?php if(!empty($appSettings['logo_light']) && !empty($appSettings['logo_dark'])): ?>
                    <img src="<?php echo URLROOT; ?>/uploads/<?php echo $appSettings['logo_light']; ?>" alt="<?php echo $appSettings['app_name'] ?? SITENAME; ?> Logo" class="h-10 sm:h-14 block dark:hidden object-contain transform group-hover:scale-105 transition-transform duration-300">
                    <img src="<?php echo URLROOT; ?>/uploads/<?php echo $appSettings['logo_dark']; ?>" alt="<?php echo $appSettings['app_name'] ?? SITENAME; ?> Logo" class="h-10 sm:h-14 hidden dark:block object-contain transform group-hover:scale-105 transition-transform duration-300">
                <?php else: ?>
                    <span class="text-xl sm:text-2xl font-extrabold tracking-tight text-transparent bg-clip-text bg-gradient-to-r from-primary to-blue-600 hover:opacity-80 transition-all duration-300">
                        <?php echo $appSettings['app_name'] ?? SITENAME; ?>
                    </span>
                <?php endif; ?>
            </a>
        </div>
        
        <div class="flex items-center space-x-3 sm:space-x-4 md:space-x-6">
            
            <?php if(!isLoggedIn()) : ?>
                <div class="hidden md:flex items-center space-x-6">
                    <a href="<?php echo URLROOT; ?>" class="text-sm font-bold text-gray-600 hover:text-primary dark:text-gray-300 dark:hover:text-primary transition-colors">Home</a>
                    <a href="<?php echo URLROOT; ?>/pages/contact" class="text-sm font-bold text-gray-600 hover:text-primary dark:text-gray-300 dark:hover:text-primary transition-colors">Contact</a>
                    <?php if(isset($appSettings['allow_registration']) && $appSettings['allow_registration'] === '1') : ?>
                        <a href="<?php echo URLROOT; ?>/users/register" class="text-sm font-bold text-gray-600 hover:text-primary dark:text-gray-300 dark:hover:text-primary transition-colors">Register</a>
                    <?php endif; ?>
                    <a href="<?php echo URLROOT; ?>/users/login" class="text-sm font-bold text-gray-600 hover:text-primary dark:text-gray-300 dark:hover:text-primary transition-colors">Login</a>
                </div>

                <button id="theme-toggle" class="p-2.5 rounded-full bg-gray-50 dark:bg-gray-700 border border-gray-200 dark:border-gray-600 shadow-sm hover:shadow-md hover:scale-110 hover:-rotate-12 transition-all duration-300 focus:outline-none focus:ring-2 focus:ring-primary text-gray-600 dark:text-gray-300 group ml-2" aria-label="Toggle Theme">
                    <svg class="w-5 h-5 dark:hidden group-hover:text-primary transition-colors" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20.354 15.354A9 9 0 018.646 3.646 9.003 9.003 0 0012 21a9.003 9.003 0 008.354-5.646z"></path></svg>
                    <svg class="w-5 h-5 hidden dark:block group-hover:text-primary transition-colors" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 3v1m0 16v1m9-9h-1M4 12H3m15.364 6.364l-.707-.707M6.343 6.343l-.707-.707m12.728 0l-.707.707M6.343 17.657l-.707.707M16 12a4 4 0 11-8 0 4 4 0 018 0z"></path></svg>
                </button>

                <button id="public-mobile-btn" class="md:hidden text-gray-600 dark:text-gray-300 hover:text-primary focus:outline-none ml-2 transition-transform hover:scale-105">
                    <svg class="w-8 h-8" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16"></path></svg>
                </button>

            <?php else: ?>
                <!-- Desktop Global Search - FIXED ROUTE -->
                <form action="<?php echo URLROOT; ?>/search/index" method="GET" class="hidden lg:block relative mr-2">
                    <input type="text" name="q" placeholder="Global search..." class="w-64 pl-10 pr-4 py-2.5 rounded-full border border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-gray-900/50 text-sm focus:outline-none focus:ring-2 focus:ring-primary dark:text-white transition-all shadow-inner" required>
                    <svg class="w-4 h-4 absolute left-4 top-3 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"></path></svg>
                </form>

                <div class="hidden md:flex flex-col text-right mr-2">
                    <span class="text-sm font-bold text-gray-900 dark:text-white" id="live-time"><?php echo date('H:i:s'); ?></span>
                    <span class="text-xs text-gray-500 dark:text-gray-400 font-medium"><?php echo date('l, d F Y'); ?></span>
                </div>
                
                <button id="help-toggle-btn" class="p-2.5 rounded-full bg-gray-50 dark:bg-gray-700 border border-gray-200 dark:border-gray-600 shadow-sm hover:shadow-md hover:scale-110 hover:text-primary dark:hover:text-primary transition-all duration-300 focus:outline-none focus:ring-2 focus:ring-primary text-gray-600 dark:text-gray-300 mr-1" title="Page Help & Guide">
                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8.228 9c.549-1.165 2.03-2 3.772-2 2.21 0 4 1.343 4 3 0 1.4-1.278 2.575-3.006 2.907-.542.104-.994.54-.994 1.093m0 3h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
                </button>

                <button id="theme-toggle" class="p-2.5 rounded-full bg-gray-50 dark:bg-gray-700 border border-gray-200 dark:border-gray-600 shadow-sm hover:shadow-md hover:scale-110 hover:rotate-12 transition-all duration-300 focus:outline-none focus:ring-2 focus:ring-primary text-gray-600 dark:text-gray-300 group" aria-label="Toggle Theme">
                    <svg class="w-5 h-5 dark:hidden group-hover:text-primary transition-colors" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20.354 15.354A9 9 0 018.646 3.646 9.003 9.003 0 0012 21a9.003 9.003 0 008.354-5.646z"></path></svg>
                    <svg class="w-5 h-5 hidden dark:block group-hover:text-primary transition-colors" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 3v1m0 16v1m9-9h-1M4 12H3m15.364 6.364l-.707-.707M6.343 6.343l-.707-.707m12.728 0l-.707.707M6.343 17.657l-.707.707M16 12a4 4 0 11-8 0 4 4 0 018 0z"></path></svg>
                </button>
            <?php endif; ?>
        
        </div>
    </div>

    <?php if(!isLoggedIn()) : ?>
    <div id="public-mobile-menu" class="hidden absolute w-full bg-white dark:bg-gray-800 shadow-2xl border-t border-gray-100 dark:border-gray-700 z-40 top-full left-0 transition-all origin-top">
        <div class="px-4 pt-2 pb-6 space-y-2 flex flex-col">
            <a href="<?php echo URLROOT; ?>" class="block px-4 py-3 rounded-lg text-base font-bold text-gray-900 dark:text-white hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors">Home</a>
            <a href="<?php echo URLROOT; ?>/pages/contact" class="block px-4 py-3 rounded-lg text-base font-bold text-gray-900 dark:text-white hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors">Contact Us</a>
            <?php if(isset($appSettings['allow_registration']) && $appSettings['allow_registration'] === '1') : ?>
                <a href="<?php echo URLROOT; ?>/users/register" class="block px-4 py-3 rounded-lg text-base font-bold text-gray-900 dark:text-white hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors">Create Account</a>
            <?php endif; ?>
            <a href="<?php echo URLROOT; ?>/users/login" class="block px-4 py-3 rounded-lg text-base font-bold text-primary bg-primary/10 dark:bg-primary/20 mt-4 text-center transition-colors hover:bg-primary/20 dark:hover:bg-primary/30">Login to Portal</a>
        </div>
    </div>
    <?php endif; ?>

</nav>

<?php if(isLoggedIn()): ?>
<script>
    setInterval(function() {
        var timeElement = document.getElementById('live-time');
        if(timeElement) {
            var now = new Date();
            var hours = String(now.getHours()).padStart(2, '0');
            var minutes = String(now.getMinutes()).padStart(2, '0');
            var seconds = String(now.getSeconds()).padStart(2, '0');
            timeElement.innerHTML = hours + ':' + minutes + ':' + seconds;
        }
    }, 1000);
</script>
<?php endif; ?>