<?php
require_once __DIR__ . '/../config/database.php';
$settings    = getAllSettings($pdo);
$siteName    = $settings['site_name'] ?? 'বাংলা শপ';
$menus       = $pdo->query("SELECT * FROM menus ORDER BY position ASC")->fetchAll();
$currentPage = basename($_SERVER['PHP_SELF']);
$cartCount   = 0;
if (isset($_SESSION['checkout_items'])) {
    foreach ($_SESSION['checkout_items'] as $ci) $cartCount += (int)$ci['quantity'];
}
?>
<!DOCTYPE html>
<html lang="bn">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="<?= e($siteName) ?> — বাংলাদেশের সেরা অনলাইন শপিং">
<meta name="theme-color" content="#1A2A4F">
<title><?= e($siteName) ?></title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link href="https://fonts.googleapis.com/css2?family=Hind+Siliguri:wght@300;400;500;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="<?= BASE_URL ?>/assets/css/style.css?v=4">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">

<?php if(($settings['fb_pixel_enabled']??'0')==='1' && !empty($settings['fb_pixel_id'])): ?>
<!-- Facebook Pixel -->
<script>
!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,document,'script','https://connect.facebook.net/en_US/fbevents.js');
fbq('init','<?= e($settings['fb_pixel_id']) ?>');
fbq('track','PageView');
</script>
<noscript><img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=<?= e($settings['fb_pixel_id']) ?>&ev=PageView&noscript=1"/></noscript>
<?php endif; ?>
</head>
<body>
<div class="top-bar">
    <div class="container">
        <span><i class="fas fa-phone"></i> <?= e($settings['contact_phone'] ?? '') ?></span>
        <span><i class="fas fa-envelope"></i> <?= e($settings['contact_email'] ?? '') ?></span>
    </div>
</div>
<header class="main-header">
    <div class="container header-inner">
        <a href="<?= BASE_URL ?>/index.php" class="logo">
            <?php if(!empty($settings['site_logo'])): ?>
                <img src="<?= UPLOADS_URL.e($settings['site_logo']) ?>" alt="<?= e($siteName) ?>">
            <?php else: ?>
                <span class="logo-text"><?= e($siteName) ?></span>
            <?php endif; ?>
        </a>
        <nav class="main-nav" id="mainNav">
            <?php foreach($menus as $menu): ?>
                <a href="<?= e($menu['link']) ?>" class="nav-link <?= $currentPage===basename($menu['link'])?'active':'' ?>"><?= e($menu['name']) ?></a>
            <?php endforeach; ?>
        </nav>
        <div class="header-actions">
            <?php if(isLoggedIn()): ?>
                <?php if(isAdmin()): ?><a href="<?= BASE_URL ?>/admin/index.php" class="user-btn" title="অ্যাডমিন"><i class="fas fa-user-shield"></i></a>
                <?php elseif(isInvestor()): ?><a href="<?= BASE_URL ?>/investor-dashboard.php" class="user-btn"><i class="fas fa-chart-line"></i></a>
                <?php else: ?><a href="<?= BASE_URL ?>/user-dashboard.php" class="user-btn"><i class="fas fa-user"></i></a><?php endif; ?>
                <a href="<?= BASE_URL ?>/logout.php" class="user-btn logout-btn"><i class="fas fa-sign-out-alt"></i></a>
            <?php else: ?>
                <a href="<?= BASE_URL ?>/login.php" class="user-btn"><i class="fas fa-user"></i></a>
            <?php endif; ?>
            <a href="<?= BASE_URL ?>/checkout.php" class="cart-btn">
                <i class="fas fa-shopping-bag"></i>
                <?php if($cartCount>0): ?><span class="cart-count"><?= $cartCount ?></span><?php endif; ?>
            </a>
            <button class="hamburger" id="hamburger" aria-label="মেনু"><span></span><span></span><span></span></button>
        </div>
    </div>
</header>
