<?php
/**
 * Header Layout
 */

// Ensure BASE_URL is defined
if (!defined('BASE_URL')) {
    define('BASE_URL', 'https://nis-migrant.com.ng/nis_ams');
}

// Get CSRF token safely
$csrfToken = '';
if (class_exists('Security')) {
    try {
        $csrfToken = Security::csrfToken();
    } catch (Exception $e) {
        $csrfToken = '';
    }
}

// Get app name safely
$appName = 'NIS Asset Management System';
if (class_exists('Config')) {
    try {
        $appName = Config::get('app_name', 'NIS Asset Management System');
    } catch (Exception $e) {
        $appName = 'NIS Asset Management System';
    }
}

$pageTitle = $title ?? 'Dashboard';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="csrf-token" content="<?php echo htmlspecialchars($csrfToken); ?>">
    <title><?php echo htmlspecialchars($appName); ?> - <?php echo htmlspecialchars($pageTitle); ?></title>
    
    <!-- Favicon / URL Icon -->
    <link rel="icon" type="image/png" href="<?php echo BASE_URL; ?>/assets/images/nis-logo.png">
    <link rel="shortcut icon" href="<?php echo BASE_URL; ?>/assets/images/nis-logo.png">
    <link rel="apple-touch-icon" href="<?php echo BASE_URL; ?>/assets/images/nis-logo.png">
    
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    
    <!-- Chart.js -->
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    
    <!-- Main CSS -->
    <link rel="stylesheet" href="<?php echo BASE_URL; ?>/assets/css/app.css">
    
    <!-- Page specific CSS -->
    <?php if (isset($extra_css) && is_array($extra_css)): ?>
        <?php foreach ($extra_css as $css): ?>
            <link rel="stylesheet" href="<?php echo htmlspecialchars($css); ?>">
        <?php endforeach; ?>
    <?php endif; ?>
    
    <!-- Notification Styles -->
    <style>
    /* Notification styles */
    .notification-container {
        position: fixed;
        top: 20px;
        right: 20px;
        z-index: 9999;
        display: flex;
        flex-direction: column;
        gap: 10px;
        max-width: 400px;
    }

    .notification {
        background: white;
        border-radius: 10px;
        padding: 16px 20px;
        box-shadow: 0 5px 20px rgba(0,0,0,0.15);
        display: flex;
        align-items: center;
        gap: 12px;
        animation: slideIn 0.3s ease;
        border-left: 4px solid;
        position: relative;
        overflow: hidden;
    }

    .notification::after {
        content: '';
        position: absolute;
        bottom: 0;
        left: 0;
        height: 3px;
        width: 100%;
        animation: progress 5s linear forwards;
    }

    .notification.success {
        border-left-color: #27ae60;
        background: #f0f9f4;
    }

    .notification.success::after {
        background: #27ae60;
    }

    .notification.success i {
        color: #27ae60;
    }

    .notification.error {
        border-left-color: #e53e3e;
        background: #fef2f2;
    }

    .notification.error::after {
        background: #e53e3e;
    }

    .notification.error i {
        color: #e53e3e;
    }

    .notification.warning {
        border-left-color: #f39c12;
        background: #fff8e6;
    }

    .notification.warning::after {
        background: #f39c12;
    }

    .notification.warning i {
        color: #f39c12;
    }

    .notification.info {
        border-left-color: #3498db;
        background: #e8f4fd;
    }

    .notification.info::after {
        background: #3498db;
    }

    .notification.info i {
        color: #3498db;
    }

    .notification-icon {
        font-size: 1.5rem;
    }

    .notification-content {
        flex: 1;
    }

    .notification-title {
        font-weight: 700;
        margin-bottom: 4px;
        color: #2c3e50;
    }

    .notification-message {
        font-size: 0.9rem;
        color: #4a5568;
    }

    .notification-close {
        background: transparent;
        border: none;
        color: #999;
        cursor: pointer;
        font-size: 1.2rem;
        padding: 0 5px;
        transition: color 0.3s;
    }

    .notification-close:hover {
        color: #2c3e50;
    }

    @keyframes slideIn {
        from {
            transform: translateX(100%);
            opacity: 0;
        }
        to {
            transform: translateX(0);
            opacity: 1;
        }
    }

    @keyframes progress {
        from {
            width: 100%;
        }
        to {
            width: 0%;
        }
    }
    </style>
    
    <!-- Ensure overlay is hidden by default -->
    <style>
        .sidebar-overlay {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0,0,0,0.5);
            z-index: 998;
        }
        
        .sidebar {
            transition: transform 0.3s ease;
        }
        
        @media (max-width: 991px) {
            .sidebar {
                transform: translateX(-100%);
            }
            .sidebar.active {
                transform: translateX(0);
            }
        }
    </style>
</head>
<body>
    <!-- Header -->
    <header>
        <div class="header-container">
            <!-- Mobile Menu Toggle -->
            <button class="mobile-menu-toggle" id="mobileMenuToggle">
                <i class="fas fa-bars"></i>
            </button>
            
            <div class="logo-title-container">
                <img src="<?php echo BASE_URL; ?>/assets/images/nis-logo.png" alt="NIS Logo" class="header-logo" onerror="this.style.display='none'">
                <div class="title-container">
                    <h1>NIS Asset Management Portal</h1>
                    <h2>Works & Logistics</h2>
                </div>
            </div>
            
            <!-- User Menu -->
            <div class="admin-profile-container" style="position: relative;">
                <button class="admin-profile-btn" id="adminProfileBtn" style="background: linear-gradient(135deg, #2c3e50 0%, #27ae60 100%); color: white; border: none; border-radius: 6px; padding: 8px 12px; cursor: pointer; display: flex; align-items: center; gap: 8px; font-size: 0.85rem; font-weight: 600;">
                    <i class="fas fa-user-shield"></i>
                    <span><?php echo htmlspecialchars($_SESSION['full_name'] ?? 'Administrator'); ?></span>
                    <i class="fas fa-chevron-down"></i>
                </button>
                <div class="admin-profile-menu" id="adminProfileMenu" style="display: none; position: absolute; top: 100%; right: 0; background: white; border-radius: 8px; box-shadow: 0 5px 20px rgba(0,0,0,0.15); min-width: 220px; z-index: 9999; margin-top: 5px; border: 1px solid #e0e0e0;">
                    <div style="padding: 12px; background: linear-gradient(135deg, #2c3e50 0%, #27ae60 100%); color: white; border-bottom: 1px solid rgba(255,255,255,0.1);">
                        <div style="font-weight: 600; margin-bottom: 4px;"><?php echo htmlspecialchars($_SESSION['full_name'] ?? 'User'); ?></div>
                        <div style="font-size: 0.8rem; opacity: 0.9;"><?php echo htmlspecialchars(implode(', ', $_SESSION['roles'] ?? ['Staff'])); ?></div>
                    </div>
                    
                    <a href="<?php echo BASE_URL; ?>/views/users/profile.php" style="display: flex; align-items: center; gap: 10px; padding: 10px 12px; color: #333; text-decoration: none; border-left: 3px solid transparent; font-size: 0.9rem;">
                        <i class="fas fa-user-circle" style="width: 16px; color: #27ae60;"></i>
                        <span>My Profile</span>
                    </a>
                    
                    <a href="<?php echo BASE_URL; ?>/auth/change-password.php" style="display: flex; align-items: center; gap: 10px; padding: 10px 12px; color: #333; text-decoration: none; border-left: 3px solid transparent; font-size: 0.9rem;">
                        <i class="fas fa-key" style="width: 16px; color: #27ae60;"></i>
                        <span>Change Password</span>
                    </a>
                    
                    <?php if (isset($_SESSION['is_super_admin']) && $_SESSION['is_super_admin']): ?>
                        <div style="height: 1px; background: #e0e0e0; margin: 5px 0;"></div>
                        <a href="<?php echo BASE_URL; ?>/views/users/index.php" style="display: flex; align-items: center; gap: 10px; padding: 10px 12px; color: #333; text-decoration: none; border-left: 3px solid transparent; font-size: 0.9rem;">
                            <i class="fas fa-users-cog" style="width: 16px; color: #27ae60;"></i>
                            <span>User Management</span>
                        </a>
                        <a href="<?php echo BASE_URL; ?>/settings" style="display: flex; align-items: center; gap: 10px; padding: 10px 12px; color: #333; text-decoration: none; border-left: 3px solid transparent; font-size: 0.9rem;">
                            <i class="fas fa-cogs" style="width: 16px; color: #27ae60;"></i>
                            <span>System Settings</span>
                        </a>
                    <?php endif; ?>
                    
                    <div style="height: 1px; background: #e0e0e0; margin: 5px 0;"></div>
                    
                    <a href="<?php echo BASE_URL; ?>/logout.php" style="display: flex; align-items: center; gap: 10px; padding: 10px 12px; color: #e74c3c; text-decoration: none; border-left: 3px solid transparent; font-size: 0.9rem;">
                        <i class="fas fa-sign-out-alt" style="width: 16px; color: #e74c3c;"></i>
                        <span>Logout</span>
                    </a>
                </div>
            </div>
        </div>
    </header>
    
    <!-- Sidebar Overlay for Mobile -->
    <div class="sidebar-overlay" id="sidebarOverlay"></div>
    
    <!-- Simple JavaScript for Dropdown -->
    <script>
        // Simple dropdown toggle
        document.addEventListener('DOMContentLoaded', function() {
            console.log('DOM loaded - initializing dropdown');
            
            const profileBtn = document.getElementById('adminProfileBtn');
            const profileMenu = document.getElementById('adminProfileMenu');
            
            if (profileBtn && profileMenu) {
                console.log('Profile button found');
                
                // Set initial state
                profileMenu.style.display = 'none';
                
                // Toggle on click
                profileBtn.onclick = function(e) {
                    e.preventDefault();
                    e.stopPropagation();
                    
                    if (profileMenu.style.display === 'none' || profileMenu.style.display === '') {
                        profileMenu.style.display = 'block';
                        console.log('Menu opened');
                    } else {
                        profileMenu.style.display = 'none';
                        console.log('Menu closed');
                    }
                };
                
                // Close when clicking outside
                document.onclick = function(e) {
                    if (!profileBtn.contains(e.target) && !profileMenu.contains(e.target)) {
                        profileMenu.style.display = 'none';
                    }
                };
            } else {
                console.log('Profile button or menu not found');
            }
            
            // Mobile menu toggle (keep existing code)
            const mobileToggle = document.getElementById('mobileMenuToggle');
            const sidebar = document.querySelector('.sidebar');
            const overlay = document.getElementById('sidebarOverlay');
            
            if (mobileToggle && sidebar && overlay) {
                mobileToggle.onclick = function() {
                    if (sidebar.style.transform === 'translateX(0px)' || sidebar.classList.contains('active')) {
                        sidebar.style.transform = 'translateX(-100%)';
                        sidebar.classList.remove('active');
                        overlay.style.display = 'none';
                        mobileToggle.innerHTML = '<i class="fas fa-bars"></i>';
                    } else {
                        sidebar.style.transform = 'translateX(0)';
                        sidebar.classList.add('active');
                        overlay.style.display = 'block';
                        mobileToggle.innerHTML = '<i class="fas fa-times"></i>';
                    }
                };
                
                overlay.onclick = function() {
                    sidebar.style.transform = 'translateX(-100%)';
                    sidebar.classList.remove('active');
                    overlay.style.display = 'none';
                    mobileToggle.innerHTML = '<i class="fas fa-bars"></i>';
                };
            }
        });
    </script>