<?php
/**
 * Header Layout
 */

// Ensure BASE_URL is defined
if (!defined('BASE_URL')) {
    define('BASE_URL', 'https://niscoreapps.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';

// Store user data in variables to prevent session corruption
$currentUserFullName = $_SESSION['full_name'] ?? 'Administrator';
$currentUserRoles = isset($_SESSION['roles']) && is_array($_SESSION['roles']) ? $_SESSION['roles'] : ['Staff'];
$currentUserRolesDisplay = htmlspecialchars(implode(', ', $currentUserRoles));
$isSuperAdmin = isset($_SESSION['is_super_admin']) && $_SESSION['is_super_admin'] === true;
$currentUserId = $_SESSION['user_id'] ?? 0;
?>
<!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);
            }
        }
        
        /* Admin profile menu styles */
        .admin-profile-container {
            position: relative;
        }
        
        .admin-profile-btn {
            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;
            transition: all 0.3s ease;
        }
        
        .admin-profile-btn:hover {
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(39, 174, 96, 0.3);
        }
        
        .admin-profile-menu {
            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;
        }
        
        .admin-profile-menu.show {
            display: block;
        }
        
        .admin-menu-header {
            padding: 12px;
            background: linear-gradient(135deg, #2c3e50 0%, #27ae60 100%);
            color: white;
            border-bottom: 1px solid rgba(255,255,255,0.1);
            border-radius: 8px 8px 0 0;
        }
        
        .admin-menu-header .user-name {
            font-weight: 600;
            margin-bottom: 4px;
        }
        
        .admin-menu-header .user-roles {
            font-size: 0.75rem;
            opacity: 0.9;
        }
        
        .admin-menu-item {
            display: flex;
            align-items: center;
            gap: 10px;
            padding: 10px 12px;
            color: #333;
            text-decoration: none;
            border-left: 3px solid transparent;
            font-size: 0.85rem;
            transition: all 0.3s ease;
        }
        
        .admin-menu-item:hover {
            background: #f5f5f5;
            border-left-color: #27ae60;
            color: #27ae60;
        }
        
        .admin-menu-item i {
            width: 18px;
            color: #27ae60;
        }
        
        .admin-menu-item.logout {
            color: #e74c3c;
        }
        
        .admin-menu-item.logout i {
            color: #e74c3c;
        }
        
        .admin-menu-divider {
            height: 1px;
            background: #e0e0e0;
            margin: 5px 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 System</h1>
                    <h2>Works & Logistics</h2>
                </div>
            </div>
            
            <!-- User Menu -->
            <div class="admin-profile-container">
                <button class="admin-profile-btn" id="adminProfileBtn">
                    <i class="fas fa-user-shield"></i>
                    <span><?php echo htmlspecialchars($currentUserFullName); ?></span>
                    <i class="fas fa-chevron-down"></i>
                </button>
                <div class="admin-profile-menu" id="adminProfileMenu">
                    <div class="admin-menu-header">
                        <div class="user-name"><?php echo htmlspecialchars($currentUserFullName); ?></div>
                        <div class="user-roles"><?php echo $currentUserRolesDisplay; ?></div>
                    </div>
                    
                    <a href="<?php echo BASE_URL; ?>/users/profile" class="admin-menu-item">
                        <i class="fas fa-user-circle"></i>
                        <span>My Profile</span>
                    </a>
                    
                    <a href="<?php echo BASE_URL; ?>/auth/change-password.php" class="admin-menu-item">
                        <i class="fas fa-key"></i>
                        <span>Change Password</span>
                    </a>
                    
                    <?php if ($isSuperAdmin): ?>
                        <div class="admin-menu-divider"></div>
                        <a href="<?php echo BASE_URL; ?>/users" class="admin-menu-item">
                            <i class="fas fa-users-cog"></i>
                            <span>User Management</span>
                        </a>
                        <a href="<?php echo BASE_URL; ?>/settings" class="admin-menu-item">
                            <i class="fas fa-cogs"></i>
                            <span>System Settings</span>
                        </a>
                    <?php endif; ?>
                    
                    <div class="admin-menu-divider"></div>
                    
                    <a href="<?php echo BASE_URL; ?>/auth/logout" class="admin-menu-item logout">
                        <i class="fas fa-sign-out-alt"></i>
                        <span>Logout</span>
                    </a>
                </div>
            </div>
        </div>
    </header>
    
    <!-- Sidebar Overlay for Mobile -->
    <div class="sidebar-overlay" id="sidebarOverlay"></div>
    
    <!-- JavaScript for Dropdown -->
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // Profile dropdown
            const profileBtn = document.getElementById('adminProfileBtn');
            const profileMenu = document.getElementById('adminProfileMenu');
            
            if (profileBtn && profileMenu) {
                // Toggle on click
                profileBtn.addEventListener('click', function(e) {
                    e.preventDefault();
                    e.stopPropagation();
                    profileMenu.classList.toggle('show');
                });
                
                // Close when clicking outside
                document.addEventListener('click', function(e) {
                    if (!profileBtn.contains(e.target) && !profileMenu.contains(e.target)) {
                        profileMenu.classList.remove('show');
                    }
                });
            }
            
            // Mobile menu toggle
            const mobileToggle = document.getElementById('mobileMenuToggle');
            const sidebar = document.querySelector('.sidebar');
            const overlay = document.getElementById('sidebarOverlay');
            
            if (mobileToggle && sidebar && overlay) {
                mobileToggle.addEventListener('click', function(e) {
                    e.stopPropagation();
                    sidebar.classList.toggle('active');
                    overlay.classList.toggle('active');
                    
                    if (sidebar.classList.contains('active')) {
                        mobileToggle.innerHTML = '<i class="fas fa-times"></i>';
                    } else {
                        mobileToggle.innerHTML = '<i class="fas fa-bars"></i>';
                    }
                });
                
                overlay.addEventListener('click', function() {
                    sidebar.classList.remove('active');
                    overlay.classList.remove('active');
                    mobileToggle.innerHTML = '<i class="fas fa-bars"></i>';
                });
            }
        });
    </script>