<?php
require_once 'config.php';
require_once 'db.php';
require_once 'functions.php';

// Get categories for navigation
$categories = getCategories($pdo);

// Get new order count for admin users
$new_orders_count = 0;
if (isLoggedIn() && isAdmin()) {
    // Get the last check time from session, default to a long time ago if not set
    $last_check_time = isset($_SESSION['last_order_check']) ? $_SESSION['last_order_check'] : '1970-01-01 00:00:00';
    
    // Count new orders
    $stmt = $pdo->prepare("SELECT COUNT(*) as count FROM orders WHERE order_date > ?");
    $stmt->execute([$last_check_time]);
    $new_orders_count = $stmt->fetch(PDO::FETCH_ASSOC)['count'];
}
?>

<!DOCTYPE html>
<html lang="ar" dir="ltr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="<?php echo SITE_URL; ?>/assets/css/style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
</head>
<body dir="<?php echo LANG_DIRECTION; ?>">
    <header class="main-header">
        <div class="container">
            <div class="header-brand">
                <div class="logo">
                    <h1><a href="<?php echo SITE_URL; ?>/index.php"><?php echo SITE_NAME; ?></a></h1>
                </div>
                
                <button class="mobile-menu-toggle" aria-label="Toggle navigation">
                    <i class="fas fa-bars"></i>
                </button>
            </div>
            
            <nav class="main-nav">
                <ul class="nav-menu">
                    <li><a href="<?php echo SITE_URL; ?>/index.php"><i class="fas fa-home"></i> الرئيسية</a></li>
                    <li class="dropdown">
                        <a href="#"><i class="fas fa-th-large"></i> التصنيفات <i class="fas fa-chevron-down"></i></a>
                        <ul class="dropdown-menu">
                            <?php foreach ($categories as $category): ?>
                                <li><a href="<?php echo SITE_URL; ?>/products/index.php?category=<?php echo urlencode($category['category']); ?>"><?php echo htmlspecialchars($category['category']); ?></a></li>
                            <?php endforeach; ?>
                        </ul>
                    </li>
                    <li><a href="<?php echo SITE_URL; ?>/products/index.php"><i class="fas fa-box"></i> المنتجات</a></li>
                    <?php if (isLoggedIn()): ?>
                        <li><a href="<?php echo SITE_URL; ?>/favorites.php"><i class="fas fa-heart"></i> المفضلة</a></li>
                        <li><a href="<?php echo SITE_URL; ?>/orders.php"><i class="fas fa-shopping-bag"></i> طلباتي</a></li>
                        <li><a href="<?php echo SITE_URL; ?>/profile.php"><i class="fas fa-user"></i> الملف الشخصي</a></li>
                        <li><a href="<?php echo SITE_URL; ?>/tickets/my_tickets.php"><i class="fas fa-headset"></i> الدعم</a></li>
                    <?php endif; ?>
                </ul>
            </nav>
            
            <div class="header-actions">
                <?php if (isLoggedIn()): ?>
                    <div class="user-menu">
                        <button class="user-menu-toggle">
                            <i class="fas fa-user-circle"></i> <?php echo htmlspecialchars($_SESSION['username']); ?>
                            <i class="fas fa-chevron-down"></i>
                        </button>
                        <ul class="user-dropdown">
                            <li><a href="<?php echo SITE_URL; ?>/profile.php"><i class="fas fa-user"></i> الملف الشخصي</a></li>
                            <li><a href="<?php echo SITE_URL; ?>/orders.php"><i class="fas fa-shopping-bag"></i> طلباتي</a></li>
                            <li><a href="<?php echo SITE_URL; ?>/favorites.php"><i class="fas fa-heart"></i> المفضلة</a></li>
                            <?php if (isAdmin()): ?>
                                <li><a href="<?php echo SITE_URL; ?>/admin/index.php"><i class="fas fa-cog"></i> لوحة التحكم</a></li>
                            <?php endif; ?>
                            <li class="divider"></li>
                            <li><a href="<?php echo SITE_URL; ?>/auth/login.php?action=logout"><i class="fas fa-sign-out-alt"></i> تسجيل الخروج</a></li>
                        </ul>
                    </div>
                <?php else: ?>
                    <a href="<?php echo SITE_URL; ?>/auth/login.php" class="btn btn-outline"><i class="fas fa-sign-in-alt"></i> تسجيل الدخول</a>
                    <a href="<?php echo SITE_URL; ?>/auth/register.php" class="btn btn-primary"><i class="fas fa-user-plus"></i> تسجيل</a>
                <?php endif; ?>
                
                <a href="<?php echo SITE_URL; ?>/cart/index.php" class="cart-icon">
                    <i class="fas fa-shopping-cart"></i>
                    <span class="cart-badge" id="cart-count">0</span>
                </a>
            </div>
        </div>
    </header>
    
    <script>
        // Pass user data to JavaScript
        window.userId = <?php echo json_encode(isLoggedIn() ? $_SESSION['user_id'] : null); ?>;
        window.siteUrl = '<?php echo SITE_URL; ?>';
        
        // Mobile menu toggle
        document.addEventListener('DOMContentLoaded', function() {
            const mobileMenuToggle = document.querySelector('.mobile-menu-toggle');
            const mainNav = document.querySelector('.main-nav');
            
            if (mobileMenuToggle) {
                mobileMenuToggle.addEventListener('click', function() {
                    mainNav.classList.toggle('active');
                });
            }
            
            // User menu toggle
            const userMenuToggle = document.querySelector('.user-menu-toggle');
            const userDropdown = document.querySelector('.user-dropdown');
            
            if (userMenuToggle && userDropdown) {
                userMenuToggle.addEventListener('click', function(e) {
                    e.stopPropagation();
                    userDropdown.classList.toggle('active');
                });
                
                // Close dropdown when clicking outside
                document.addEventListener('click', function(e) {
                    if (!userMenuToggle.contains(e.target) && !userDropdown.contains(e.target)) {
                        userDropdown.classList.remove('active');
                    }
                });
            }
            
            // Close mobile menu when clicking outside
            document.addEventListener('click', function(e) {
                if (mainNav && mobileMenuToggle && 
                    !mainNav.contains(e.target) && 
                    !mobileMenuToggle.contains(e.target) && 
                    mainNav.classList.contains('active')) {
                    mainNav.classList.remove('active');
                }
            });
        });
    </script>
    
    <main>