<?php
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}
require_once __DIR__ . '/../config/db.php';

// Calculate cart count
$cart_count = 0;
if (isset($_SESSION['cart']) && is_array($_SESSION['cart'])) {
    foreach ($_SESSION['cart'] as $qty) {
        $cart_count += $qty;
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MARTLUX | Wear Your Confidence</title>
    <!-- Bootstrap 5 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <!-- Custom CSS -->
    <link rel="stylesheet" href="assets/css/style.css">
    <style>
        .navbar-brand:hover {
            opacity: 0.9;
        }
        .nav-desktop-link:hover {
            color: var(--accent-gold) !important;
            transition: color 0.3s ease;
        }
        .cart-badge {
            background-color: var(--accent-gold);
            color: #000;
            font-size: 0.6rem;
            padding: 3px 6px;
            font-weight: 700;
            border-radius: 50%;
        }
        .nav-icon-btn {
            color: var(--text-gray);
            transition: all 0.3s ease;
            font-size: 1rem;
            text-decoration: none;
            position: relative;
            width: 38px;
            height: 38px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 50%;
            background: transparent;
        }
        .nav-icon-btn:hover {
            color: var(--accent-gold);
            background: rgba(var(--accent-gold-rgb), 0.1);
            transform: scale(1.05);
        }
        .dropdown-menu {
            background-color: rgba(21, 21, 21, 0.98) !important;
            backdrop-filter: blur(15px);
            -webkit-backdrop-filter: blur(15px);
        }
        .dropdown-item {
            color: var(--text-white) !important;
            transition: all 0.2s ease;
        }
        .dropdown-item:hover {
            color: var(--accent-gold) !important;
            background-color: rgba(var(--accent-gold-rgb), 0.08) !important;
        }
        .dropdown-toggle::after {
            display: none !important;
        }

        /* ==================== */
        /* ANIMATED HAMBURGER   */
        /* ==================== */
        .hamburger-btn {
            background: none;
            border: none;
            cursor: pointer;
            padding: 8px;
            display: flex;
            flex-direction: column;
            gap: 5px;
            transition: all 0.3s ease;
        }
        .hamburger-btn:hover {
            transform: scale(1.1);
        }
        .hamburger-btn span {
            display: block;
            width: 22px;
            height: 2px;
            background-color: var(--text-white);
            border-radius: 2px;
            transition: all 0.35s cubic-bezier(0.68, -0.55, 0.265, 1.55);
        }
        .hamburger-btn span:nth-child(2) {
            width: 16px;
        }
        .hamburger-btn:hover span {
            background-color: var(--accent-gold);
        }
        .hamburger-btn:hover span:nth-child(2) {
            width: 22px;
        }

        /* ==================== */
        /* MOBILE RESPONSIVE    */
        /* ==================== */
        @media (max-width: 768px) {
            .navbar {
                padding: 0.6rem 0.8rem !important;
            }
            .navbar-logo {
                height: 100px !important;
                width: 100px !important;
            }
            .nav-icon-btn {
                font-size: 0.9rem;
                width: 34px;
                height: 34px;
            }
            .brand-text-mobile {
                display: none !important;
            }
            .header-spacer {
                height: 70px !important;
            }
        }

        @media (max-width: 480px) {
            .navbar {
                padding: 0.5rem 0.5rem !important;
            }
            .navbar-logo {
                height: 100px !important;
                width: 100px !important;
            }
            .nav-icon-btn {
                font-size: 0.85rem;
                width: 32px;
                height: 32px;
            }
            .hamburger-btn {
                padding: 6px;
            }
            .hamburger-btn span {
                width: 18px;
                height: 1.5px;
                gap: 4px;
            }
        }

        /* ======================== */
        /* DRAWER CATEGORY CARDS    */
        /* ======================== */
        .drawer-cat-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 10px;
            margin-top: 12px;
        }
        .drawer-cat-card {
            background: rgba(var(--accent-brown-rgb), 0.2);
            border: 1px solid rgba(var(--accent-gold-rgb), 0.1);
            border-radius: 14px;
            padding: 16px 12px;
            text-align: center;
            text-decoration: none;
            color: var(--text-white);
            transition: all 0.3s ease;
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 8px;
        }
        .drawer-cat-card:hover {
            background: rgba(var(--accent-gold-rgb), 0.12);
            border-color: rgba(var(--accent-gold-rgb), 0.4);
            transform: translateY(-3px);
            color: var(--accent-gold);
        }
        .drawer-cat-card i {
            font-size: 1.3rem;
            color: var(--accent-gold);
            transition: all 0.3s ease;
        }
        .drawer-cat-card:hover i {
            transform: scale(1.15);
        }
        .drawer-cat-card span {
            font-size: 0.65rem;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: 1.5px;
        }
    </style>
</head>
<body>

<!-- Navigation Header -->
<nav class="navbar navbar-expand-lg navbar-dark fixed-top">
    <div class="container d-flex align-items-center justify-content-between">
        <!-- Left: Animated Hamburger Toggle (mobile only) -->
        <div class="d-flex align-items-center d-lg-none">
            <button class="hamburger-btn" type="button" data-bs-toggle="offcanvas" data-bs-target="#sidebarDrawer" aria-controls="sidebarDrawer" title="Open Menu">
                <span></span>
                <span></span>
                <span></span>
            </button>
        </div>

        <!-- Left: Brand Logo -->
        <div class="d-flex align-items-center">
            <a class="navbar-brand m-0 d-flex align-items-center gap-2" href="index.php" style="text-decoration: none;">
                <img src="assets/images/logo.png" alt="MARTLUX Logo" class="navbar-logo">
                <span class="brand-text-mobile" style="font-family: var(--font-heading); font-size: 1.1rem; font-weight: 600; letter-spacing: 3px; color: var(--text-white); text-transform: uppercase;">MARTLUX</span>
            </a>
        </div>

        <!-- Desktop Navigation Links (hidden on mobile) -->
        <div class="d-none d-lg-flex align-items-center gap-3 mx-3">
            <a href="index.php" class="nav-desktop-link text-white text-uppercase text-decoration-none small fw-bold" style="letter-spacing: 1px; font-size: 0.75rem;">Home</a>
            <div class="dropdown">
                <a href="#" class="nav-desktop-link text-white text-uppercase text-decoration-none small fw-bold dropdown-toggle" style="letter-spacing: 1px; font-size: 0.75rem;" data-bs-toggle="dropdown">Categories</a>
                <ul class="dropdown-menu dropdown-menu-dark border-secondary py-2">
                    <?php
                    try {
                        $cat_stmt = $pdo->query("SELECT name, slug FROM categories ORDER BY name");
                        while ($cat_row = $cat_stmt->fetch()):
                    ?>
                        <li><a class="dropdown-item text-uppercase small py-2" href="shop.php?category=<?php echo urlencode($cat_row['slug']); ?>"><?php echo htmlspecialchars($cat_row['name']); ?></a></li>
                    <?php
                        endwhile;
                    } catch (Exception $e) {}
                    ?>
                </ul>
            </div>
            <a href="shop.php" class="nav-desktop-link text-white text-uppercase text-decoration-none small fw-bold" style="letter-spacing: 1px; font-size: 0.75rem;">Shop All</a>
            <a href="about.php" class="nav-desktop-link text-white text-uppercase text-decoration-none small fw-bold" style="letter-spacing: 1px; font-size: 0.75rem;">About</a>
            <a href="contact.php" class="nav-desktop-link text-white text-uppercase text-decoration-none small fw-bold" style="letter-spacing: 1px; font-size: 0.75rem;">Contact</a>
        </div>

        <!-- Right: Action Controls -->
        <div class="d-flex align-items-center justify-content-end nav-icons-group" style="gap: 6px;">
            <!-- Search -->
            <a href="shop.php" class="nav-icon-btn" title="Search catalog">
                <i class="fa-solid fa-magnifying-glass"></i>
            </a>

            <!-- Wishlist -->
            <a href="wishlist.php" class="nav-icon-btn" title="Wishlist">
                <i class="fa-regular fa-heart"></i>
                <?php 
                $wishlist_count = 0;
                if (isset($_SESSION['customer_id'])) {
                    try {
                        $stmt_w = $pdo->prepare("SELECT COUNT(*) FROM wishlist WHERE user_id = ?");
                        $stmt_w->execute([$_SESSION['customer_id']]);
                        $wishlist_count = $stmt_w->fetchColumn();
                    } catch (Exception $e) {
                        $wishlist_count = 0;
                    }
                } elseif (isset($_SESSION['wishlist'])) {
                    $wishlist_count = count($_SESSION['wishlist']);
                }
                if ($wishlist_count > 0): 
                ?>
                    <span class="position-absolute top-0 start-100 translate-middle badge bg-warning text-black rounded-circle" style="font-size: 0.5rem; padding: 2px 5px;">
                        <?php echo $wishlist_count; ?>
                    </span>
                <?php endif; ?>
            </a>

            <!-- Cart Bag -->
            <a href="cart.php" class="nav-icon-btn text-white" title="View Cart">
                <i class="fa-solid fa-bag-shopping"></i>
                <?php if ($cart_count > 0): ?>
                    <span class="position-absolute top-0 start-100 translate-middle badge cart-badge">
                        <?php echo $cart_count; ?>
                    </span>
                <?php endif; ?>
            </a>

            <!-- Account User Dropdown -->
            <?php if (isset($_SESSION['customer_id'])): ?>
                <div class="dropdown">
                    <a href="#" class="nav-icon-btn dropdown-toggle" id="customerDropdown" data-bs-toggle="dropdown" aria-expanded="false" title="My Account" style="text-decoration: none;">
                        <i class="fa-regular fa-user"></i>
                    </a>
                    <ul class="dropdown-menu dropdown-menu-end border border-secondary text-uppercase py-2" aria-labelledby="customerDropdown" style="font-size: 0.65rem; letter-spacing: 1px; min-width: 160px; margin-top: 10px; box-shadow: 0 10px 20px rgba(0,0,0,0.5);">
                        <li class="px-3 py-2 text-warning fw-bold" style="font-size: 0.6rem; letter-spacing: 1.5px;">
                            <i class="fa-solid fa-crown me-1"></i>
                            <?php 
                            $first_name = explode(' ', trim($_SESSION['customer_name']))[0];
                            echo htmlspecialchars($first_name); 
                            ?>
                        </li>
                        <li><hr class="dropdown-divider border-secondary my-1"></li>
                        <li><a class="dropdown-item py-2" href="wishlist.php"><i class="fa-regular fa-heart me-2 text-warning"></i> Wishlist</a></li>
                        <li><a class="dropdown-item py-2" href="cart.php"><i class="fa-solid fa-bag-shopping me-2 text-warning"></i> Cart</a></li>
                        <li><hr class="dropdown-divider border-secondary my-1"></li>
                        <li><a class="dropdown-item text-warning py-2" href="logout.php"><i class="fa-solid fa-sign-out-alt me-2"></i> Sign Out</a></li>
                    </ul>
                </div>
            <?php else: ?>
                <a href="login.php" class="nav-icon-btn" title="Sign In">
                    <i class="fa-regular fa-user"></i>
                </a>
            <?php endif; ?>
        </div>
    </div>
</nav>

<!-- Sidebar Offcanvas Menu Drawer -->
<div class="offcanvas offcanvas-start offcanvas-luxury" tabindex="-1" id="sidebarDrawer" aria-labelledby="sidebarDrawerLabel">
    <div class="offcanvas-header border-bottom py-4" style="border-bottom-color: rgba(var(--accent-gold-rgb), 0.15) !important;">
        <div class="w-100 text-center position-relative">
            <img src="assets/images/logo.png" alt="MARTLUX Logo" style="height: 50px; width: 50px; object-fit: contain; border-radius: 50%; border: 1px solid rgba(var(--accent-gold-rgb), 0.25);">
            <h5 class="text-uppercase mt-2 mb-0" style="font-size: 0.8rem; letter-spacing: 3px; font-family: var(--font-heading); color: var(--accent-gold);">Explore</h5>
            <button type="button" class="btn-close btn-close-white position-absolute top-0 end-0" data-bs-dismiss="offcanvas" aria-label="Close"></button>
        </div>
    </div>
    <div class="offcanvas-body px-4 py-3">
        <!-- Main Navigation -->
        <nav class="d-flex flex-column">
            <a href="index.php" class="drawer-nav-link">Home <i class="fa-solid fa-chevron-right"></i></a>
            <a href="shop.php" class="drawer-nav-link">Shop All <i class="fa-solid fa-chevron-right"></i></a>
            <a href="about.php" class="drawer-nav-link">About <i class="fa-solid fa-chevron-right"></i></a>
            <a href="contact.php" class="drawer-nav-link">Contact <i class="fa-solid fa-chevron-right"></i></a>
        </nav>
        
        <!-- Category Cards Grid -->
        <div class="mt-4 pt-3 border-top" style="border-top-color: rgba(var(--accent-gold-rgb), 0.15) !important;">
            <p class="text-uppercase fw-bold mb-2" style="font-size: 0.6rem; letter-spacing: 2px; color: var(--accent-gold);">Shop by Category</p>
            <div class="drawer-cat-grid">
                <a href="shop.php?category=shirts" class="drawer-cat-card">
                    <i class="fa-solid fa-shirt"></i>
                    <span>Shirts</span>
                </a>
                <a href="shop.php?category=t-shirts" class="drawer-cat-card">
                    <i class="fa-solid fa-vest"></i>
                    <span>T-Shirts</span>
                </a>
                <a href="shop.php?category=underwear" class="drawer-cat-card">
                    <i class="fa-solid fa-layer-group"></i>
                    <span>Underwear</span>
                </a>
                <a href="shop.php?category=underskirts" class="drawer-cat-card">
                    <i class="fa-solid fa-wand-magic-sparkles"></i>
                    <span>Underskirts</span>
                </a>
                <a href="shop.php?category=mattress-covers" class="drawer-cat-card" style="grid-column: span 2;">
                    <i class="fa-solid fa-bed"></i>
                    <span>Mattress Covers</span>
                </a>
            </div>
        </div>
        
        <!-- Contact Info -->
        <div class="mt-4 pt-3 border-top text-center small" style="border-top-color: rgba(var(--accent-gold-rgb), 0.15) !important;">
            <p class="mb-1" style="color: var(--text-gray); font-size: 0.75rem;"><i class="fa-solid fa-location-dot me-2" style="color: var(--accent-gold);"></i> No.10/4, Menikhinna, Kandy</p>
            <p class="mb-3" style="color: var(--text-gray); font-size: 0.75rem;"><i class="fa-solid fa-phone me-2" style="color: var(--accent-gold);"></i> 076 414 6054</p>
            <div class="d-flex justify-content-center gap-3 mt-3">
                <a href="#" class="text-white hover-gold fs-5"><i class="fa-brands fa-instagram"></i></a>
                <a href="#" class="text-white hover-gold fs-5"><i class="fa-brands fa-facebook-f"></i></a>
                <a href="#" class="text-white hover-gold fs-5"><i class="fa-brands fa-whatsapp"></i></a>
            </div>
        </div>
    </div>
</div>

<div class="header-spacer" style="height: 80px;"></div>
