<?php
/**
 * Common Header
 * Dozar & Tripper Management System
 */
$currentUser = getCurrentUser();

// Get current page for active states
$currentPage = basename($_SERVER['PHP_SELF']);
$currentPath = $_SERVER['PHP_SELF'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo t('app_name', APP_NAME); ?> - <?php echo t('kathmandu_municipality', 'Kathmandu Municipality'); ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <style>
        :root {
            --primary-color: #2E7D32;
            --secondary-color: #1565C0;
            --accent-color: #FF6F00;
            --bg-color: #F5F5F5;
            --text-color: #212121;
            --sidebar-width: 250px;
        }
        body {
            background-color: var(--bg-color);
            color: var(--text-color);
        }
        .sidebar {
            min-height: 100vh;
            background: linear-gradient(180deg, #1B5E20 0%, #2E7D32 100%);
            box-shadow: 2px 0 5px rgba(0,0,0,0.1);
            width: var(--sidebar-width);
            position: fixed;
            top: 0;
            left: 0;
            z-index: 1000;
            transition: transform 0.3s ease-in-out;
        }
        .sidebar a {
            color: white;
            padding: 12px 20px;
            display: block;
            text-decoration: none;
            border-radius: 5px;
            margin: 5px 10px;
            transition: all 0.3s;
        }
        .sidebar a:hover, .sidebar a.active {
            background-color: rgba(255,255,255,0.2);
        }
        .sidebar a i {
            margin-right: 10px;
            width: 20px;
        }
        .main-content {
            margin-left: var(--sidebar-width);
            transition: margin-left 0.3s ease-in-out;
        }
        .navbar {
            background: white;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        /* Mobile Toggle Button */
        .menu-toggle {
            display: none;
            background: none;
            border: none;
            font-size: 1.5rem;
            cursor: pointer;
            color: var(--text-color);
            padding: 0.5rem;
        }
        /* Overlay for mobile */
        .sidebar-overlay {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.5);
            z-index: 999;
            opacity: 0;
            transition: opacity 0.3s ease;
        }
        .sidebar-overlay.active {
            display: block;
            opacity: 1;
        }
        /* Responsive Styles */
        @media (max-width: 991.98px) {
            .sidebar {
                transform: translateX(-100%);
            }
            .sidebar.show {
                transform: translateX(0);
            }
            .main-content {
                margin-left: 0;
            }
            .menu-toggle {
                display: block;
            }
            .sidebar-close {
                display: block;
                position: absolute;
                top: 10px;
                right: 10px;
                background: none;
                border: none;
                color: white;
                font-size: 1.5rem;
                cursor: pointer;
                padding: 5px 10px;
            }
        }
        @media (min-width: 992px) {
            .sidebar-close {
                display: none;
            }
        }
        .card {
            border: none;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .btn-primary {
            background-color: var(--primary-color);
            border-color: var(--primary-color);
        }
        .btn-primary:hover {
            background-color: #1B5E20;
            border-color: #1B5E20;
        }
        .alert-success { background-color: #D4EDDA; border-color: #C3E6CB; color: #155724; }
        .alert-danger { background-color: #F8D7DA; border-color: #F5C6CB; color: #721C24; }
        .alert-warning { background-color: #FFF3CD; border-color: #FFEEBA; color: #856404; }
        .alert-info { background-color: #D1ECF1; border-color: #BEE5EB; color: #0C5460; }
        .table thead {
            background-color: var(--primary-color);
            color: white;
        }
        
        @media print {
            .no-print { display: none !important; }
            .sidebar { display: none !important; }
            .navbar { display: none !important; }
            body { background: white !important; }
            .card { box-shadow: none !important; }
        }
    </style>
</head>
<body>
    <?php if (isLoggedIn()): ?>
    
    <!-- Sidebar Overlay -->
    <div class="sidebar-overlay" id="sidebarOverlay"></div>
    
    <!-- Desktop Layout -->
    <div class="container-fluid p-0">
        <div class="row g-0">
            <!-- Sidebar -->
            <div class="sidebar p-0" id="sidebar">
                <button class="sidebar-close" id="sidebarClose" aria-label="Close menu">
                    <i class="fas fa-times"></i>
                </button>
                <div class="text-center py-4">
                    <h5 class="text-white mb-0"><?php echo t('app_name', APP_NAME); ?></h5>
                    <small class="text-white-50"><?php echo t('kathmandu_municipality', 'Kathmandu Municipality'); ?></small>
                </div>
                <nav class="nav flex-column">
                    <a href="<?php echo SITE_URL; ?>/dashboard.php" class="<?php echo $currentPage == 'dashboard.php' ? 'active' : ''; ?>">
                        <i class="fas fa-home"></i> <?php echo t('nav_dashboard', 'Dashboard'); ?>
                    </a>
                    
                    <?php if (hasRole('admin') || hasAccess('dozar_only') || hasAccess('all_modules')): ?>
                    <a href="<?php echo SITE_URL; ?>/dozar/index.php" class="<?php echo strpos($currentPath, '/dozar/') !== false ? 'active' : ''; ?>">
                        <i class="fas fa-truck-monster"></i> <?php echo t('nav_dozar', 'Dozar'); ?>
                    </a>
                    <?php endif; ?>
                    
                    <?php if (hasRole('admin') || hasAccess('tripper_loading_only') || hasAccess('tripper_unloading_only') || hasAccess('all_modules')): ?>
                    <a href="<?php echo SITE_URL; ?>/tripper/index.php" class="<?php echo strpos($currentPath, '/tripper/') !== false ? 'active' : ''; ?>">
                        <i class="fas fa-truck-loading"></i> <?php echo t('nav_tripper', 'Tripper'); ?>
                    </a>
                    <?php endif; ?>
                    
                    <?php if (hasRole('admin')): ?>
                    <a href="<?php echo SITE_URL; ?>/projects/index.php" class="<?php echo strpos($currentPath, '/projects/') !== false ? 'active' : ''; ?>">
                        <i class="fas fa-map-marker-alt"></i> <?php echo t('nav_projects', 'Projects'); ?>
                    </a>
                    <a href="<?php echo SITE_URL; ?>/vehicles/index.php" class="<?php echo strpos($currentPath, '/vehicles/') !== false ? 'active' : ''; ?>">
                        <i class="fas fa-car"></i> <?php echo t('nav_vehicles', 'Vehicles'); ?>
                    </a>
                    <a href="<?php echo SITE_URL; ?>/drivers/index.php" class="<?php echo strpos($currentPath, '/drivers/') !== false ? 'active' : ''; ?>">
                        <i class="fas fa-id-card"></i> <?php echo t('nav_drivers', 'Drivers'); ?>
                    </a>
                    <a href="<?php echo SITE_URL; ?>/deployed/index.php" class="<?php echo strpos($currentPath, '/deployed/') !== false ? 'active' : ''; ?>">
                        <i class="fas fa-building"></i> <?php echo t('nav_deployed', 'Deployed From'); ?>
                    </a>
                    <a href="<?php echo SITE_URL; ?>/groups/index.php" class="<?php echo strpos($currentPath, '/groups/') !== false ? 'active' : ''; ?>">
                        <i class="fas fa-users"></i> <?php echo t('nav_groups', 'Groups'); ?>
                    </a>
                    <a href="<?php echo SITE_URL; ?>/users/index.php" class="<?php echo strpos($currentPath, '/users/') !== false ? 'active' : ''; ?>">
                        <i class="fas fa-user-cog"></i> <?php echo t('nav_users', 'Users'); ?>
                    </a>
                    <?php endif; ?>
                    
                    <?php if (hasRole('admin') || hasRole('viewer')): ?>
                    <a href="<?php echo SITE_URL; ?>/reports/index.php" class="<?php echo strpos($currentPath, '/reports/') !== false ? 'active' : ''; ?>">
                        <i class="fas fa-chart-bar"></i> <?php echo t('nav_reports', 'Reports'); ?>
                    </a>
                    <?php endif; ?>

                    <?php if (hasRole('admin')): ?>
                    <a href="<?php echo SITE_URL; ?>/activity/index.php" class="<?php echo strpos($currentPath, '/activity/') !== false ? 'active' : ''; ?>">
                        <i class="fas fa-history"></i> Activity Log
                    </a>
                    <?php endif; ?>

                    <a href="<?php echo SITE_URL; ?>/logout.php">
                        <i class="fas fa-sign-out-alt"></i> <?php echo t('nav_logout', 'Logout'); ?>
                    </a>
                </nav>
            </div>
            
            <!-- Main Content -->
            <div class="main-content col-md-10">
                <nav class="navbar navbar-expand-lg navbar-light bg-white">
                    <div class="container-fluid">
                        <button class="menu-toggle me-2" id="menuToggle" aria-label="Toggle menu">
                            <i class="fas fa-bars"></i>
                        </button>
                        <span class="navbar-brand mb-0 h1"><?php echo t('app_name', APP_NAME); ?></span>
                        <div class="d-flex align-items-center">
                            <?php
                            // Notification bell
                            if (file_exists(__DIR__ . '/auto_alerts.php')) {
                                require_once __DIR__ . '/auto_alerts.php';
                                echo getNotificationBell(getPDO());
                            }
                            ?>
                            <span class="me-3">
                                <i class="fas fa-user"></i> <?php echo $currentUser['full_name']; ?>
                                <span class="badge bg-primary"><?php echo ucfirst($currentUser['role']); ?></span>
                            </span>
                            <div class="btn-group" role="group">
                                <a href="?lang=ne" class="btn btn-sm btn-outline-secondary <?php echo CURRENT_LANGUAGE === 'ne' ? 'active' : ''; ?>">नेपाली</a>
                                <a href="?lang=en" class="btn btn-sm btn-outline-secondary <?php echo CURRENT_LANGUAGE === 'en' ? 'active' : ''; ?>">EN</a>
                            </div>
                        </div>
                    </div>
                </nav>
                <div class="p-4">
    <?php endif; ?>
