<?php
ob_start();
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/db.php';
require_once __DIR__ . '/includes/functions.php';
require_once __DIR__ . '/includes/Language.php';

/* --------------------------------------------------------------------------
   Language bootstrap.
   The Language constructor handles ?lang=xx itself (validates the code, saves
   it to the session and redirects back to the same URL keeping the other query
   parameters). There must NOT be a second handler here.
   -------------------------------------------------------------------------- */
$lang         = new Language($db);
$current_lang = $lang->getCurrentLanguage();
$languages    = $lang->getLanguages();

/** Switch-language URL that keeps the current page and its query parameters. */
function langSwitchUrl($code) {
    $parts = parse_url($_SERVER['REQUEST_URI'] ?? '');
    $path  = $parts['path'] ?? '';
    $q     = [];
    if (!empty($parts['query'])) { parse_str($parts['query'], $q); }
    $q['lang'] = $code;
    return htmlspecialchars($path . '?' . http_build_query($q), ENT_QUOTES, 'UTF-8');
}

/** Cache-buster so a changed css/js file is never served from browser cache. */
function assetUrl($rel) {
    $abs = __DIR__ . '/' . ltrim($rel, '/');
    $v   = @filemtime($abs);
    return htmlspecialchars($rel . ($v ? '?v=' . $v : ''), ENT_QUOTES, 'UTF-8');
}

$company_name    = getSetting('company_name', 'WBMCZ');
$company_logo    = getSetting('company_logo', 'assets/images/logo.png');
$company_address = getSetting('company_address', 'Bhangar, South 24 Parganas, West Bengal');
$company_phone   = getSetting('company_phone', '+91 9382064816');
$company_email   = getSetting('company_email', 'info@wbmcz.com');
$office_time     = getSetting('office_time', '10:00 AM - 06:00 PM (Mon-Sat)');
$footer_text     = getSetting('footer_text', 'WBMCZ একটি বিশ্বস্ত ডিজিটাল সার্ভিস প্ল্যাটফর্ম। আমরা সারা ভারতে রিটেইলার এবং ডিস্ট্রিবিউটরদের সেরা সার্ভিস এবং সর্বোচ্চ কমিশন প্রদান করি।');
$copyright_text  = getSetting('copyright_text', '© 2026 WBMCZ. All Rights Reserved.');

$facebook_url         = getSetting('facebook_url', 'https://www.facebook.com/share/1JFwcCXGBS/');
$youtube_url          = getSetting('youtube_url', 'https://youtube.com/@wbmczservices?si=fJIDyu8rZuSyePRT');
$whatsapp_channel_url = getSetting('whatsapp_channel_url', 'https://whatsapp.com/channel/0029VbBA9heISTkHHiKuPr1w');

$__page   = basename($_SERVER['PHP_SELF'] ?? '');
$is_home  = ($__page === 'index.php' || $__page === '');
$is_authed = isLoggedIn();

/** Where the logged-in user's panel lives. */
function userDashboardLink() {
    if (isset($_SESSION['user_type']) && $_SESSION['user_type'] === 'admin')  { return 'admin/index.php'; }
    if (isset($_SESSION['user_type']) && $_SESSION['user_type'] === 'seller') { return 'seller/index.php'; }
    return 'dashboard.php';
}
$header_dashboard_link = $is_authed ? userDashboardLink() : 'login.php';

/* Logged-in users get sent to their panel from index.php, so in-page anchors
   need ?home=1 to stay on the landing page. */
$home_url = $is_authed ? 'index.php?home=1' : 'index.php';
$anchor   = function ($hash) use ($is_authed) {
    return ($is_authed ? 'index.php?home=1' : 'index.php') . $hash;
};
?>
<!DOCTYPE html>
<html lang="<?php echo esc($current_lang); ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="theme-color" content="#0A1733">
    <meta name="description" content="<?php echo esc($company_name); ?> — <?php echo esc($lang->translate('digital_service_platform', 'Digital Service Platform')); ?>">
    <title><?php echo esc($company_name); ?> &middot; <?php echo esc($lang->translate('digital_service_platform', 'Digital Service Platform')); ?></title>

    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <!-- Latin display + body, and real Bengali / Devanagari faces so Indic text
         never falls back to the browser default (which renders badly). -->
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@300;400;500;600;700;800&family=Playfair+Display:wght@600;700;800;900&display=swap" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Hind+Siliguri:wght@400;500;600;700&family=Noto+Sans+Bengali:wght@400;500;600;700;800&family=Baloo+Da+2:wght@500;600;700;800&display=swap" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+Devanagari:wght@400;500;600;700;800&family=Baloo+2:wght@500;600;700;800&display=swap" rel="stylesheet">

    <!-- Tailwind is still used by the inner pages (login, register, category …) -->
    <script src="https://cdn.tailwindcss.com"></script>
    <script>
        tailwind.config = {
            theme: { extend: {
                colors: {
                    navy: { 900:'#050D1F', 800:'#0A1733', 700:'#0F2149', 600:'#142C5E', 500:'#1B3A78', 400:'#24509E' },
                    gold: { 600:'#B8952B', 500:'#D4AF37', 400:'#E4C662', 300:'#F0D98C', 200:'#F6E7A8' }
                },
                fontFamily: {
                    sans:    ['Plus Jakarta Sans','Hind Siliguri','Noto Sans Bengali','Noto Sans Devanagari','sans-serif'],
                    display: ['Playfair Display','Baloo Da 2','Baloo 2','Plus Jakarta Sans','serif']
                }
            } }
        }
    </script>

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper@10/swiper-bundle.min.css">
    <!-- Landing page styles. The ?v= stamp changes whenever the file changes,
         so browsers can never serve a stale copy. -->
    <link rel="stylesheet" href="<?php echo assetUrl('assets/css/style.css'); ?>">
</head>
<body>

<div id="scrollProgress"></div>

<!-- ------------------------------------------------------------------------
     The "Become a seller" announcement bar that used to sit above the header
     has been removed on request. Login / Register live in the header and in
     the hero slider card on the landing page.
     --------------------------------------------------------------------- -->

<!-- ============================== Header ================================ -->
<header id="siteHeader" class="site-header">
    <div class="wrap">
        <div class="hdr-in">

            <a href="<?php echo esc($home_url); ?>" class="brand">
                <span class="brand-logo-wrap">
                    <span class="brand-glow"></span>
                    <img src="<?php echo esc($company_logo); ?>" alt="<?php echo esc($company_name); ?>" class="brand-logo">
                </span>
                <span>
                    <span class="brand-name"><?php echo esc($company_name); ?></span>
                    <span class="brand-tag"><?php echo esc($lang->translate('digital_service_platform', 'Digital Service Platform')); ?></span>
                </span>
            </a>

            <nav class="hdr-nav">
                <?php if ($is_authed): ?>
                    <a href="<?php echo esc($header_dashboard_link); ?>" class="nav-link"><?php echo esc($lang->translate('dashboard', 'Dashboard')); ?></a>
                <?php endif; ?>
                <a href="<?php echo esc($home_url); ?>" class="nav-link<?php echo $is_home ? ' active' : ''; ?>"><?php echo esc($lang->translate('home', 'Home')); ?></a>
                <a href="<?php echo esc($anchor('#services')); ?>" class="nav-link"><?php echo esc($lang->translate('services', 'Services')); ?></a>
                <a href="<?php echo esc($anchor('#about')); ?>"    class="nav-link"><?php echo esc($lang->translate('about', 'About Us')); ?></a>
                <a href="<?php echo esc($anchor('#contact')); ?>"  class="nav-link"><?php echo esc($lang->translate('contact', 'Contact')); ?></a>
            </nav>

            <div class="hdr-right">

                <div class="hdr-social">
                    <a href="<?php echo esc($facebook_url); ?>" target="_blank" rel="noopener" class="ico-btn" aria-label="Facebook"><i class="fab fa-facebook-f"></i></a>
                    <a href="<?php echo esc($youtube_url); ?>" target="_blank" rel="noopener" class="ico-btn" aria-label="YouTube"><i class="fab fa-youtube"></i></a>
                    <a href="<?php echo esc($whatsapp_channel_url); ?>" target="_blank" rel="noopener" class="ico-btn" aria-label="WhatsApp"><i class="fab fa-whatsapp"></i></a>
                </div>

                <!-- Language switcher -->
                <div class="lang">
                    <button id="languageBtn" type="button" class="lang-btn" aria-haspopup="true" aria-expanded="false">
                        <span class="lang-code"><?php echo esc(strtoupper(substr($current_lang, 0, 2))); ?></span>
                        <span class="lang-name">
                            <?php
                            foreach ($languages as $l) { if ($l['code'] === $current_lang) { echo esc($l['name']); break; } }
                            ?>
                        </span>
                        <i class="fas fa-chevron-down lang-caret"></i>
                    </button>

                    <div id="languageMenu" class="lang-menu">
                        <p class="lang-menu-t"><?php echo esc($lang->translate('choose_language', 'Choose Language')); ?></p>
                        <div class="divider-gold"></div>
                        <?php foreach ($languages as $language): ?>
                            <?php $active = ($language['code'] === $current_lang); ?>
                            <a href="<?php echo langSwitchUrl($language['code']); ?>" class="lang-item<?php echo $active ? ' is-active' : ''; ?>">
                                <span class="lang-code"><?php echo esc(strtoupper(substr($language['code'], 0, 2))); ?></span>
                                <span class="f"><?php echo esc($language['name']); ?></span>
                                <?php if ($active): ?><i class="fas fa-check chk"></i><?php endif; ?>
                            </a>
                        <?php endforeach; ?>
                    </div>
                </div>

                <?php if ($is_authed): ?>
                    <a href="<?php echo esc($header_dashboard_link); ?>" class="btn btn-gold btn-sm hdr-cta">
                        <i class="fas fa-gauge-high"></i><?php echo esc($lang->translate('dashboard', 'Dashboard')); ?>
                    </a>
                <?php else: ?>
                    <a href="login.php"    class="btn btn-ghost btn-sm hdr-cta"><?php echo esc($lang->translate('login', 'Login')); ?></a>
                    <a href="register.php" class="btn btn-gold btn-sm hdr-cta"><?php echo esc($lang->translate('register', 'Register')); ?></a>
                <?php endif; ?>

                <button id="menuBtn" type="button" class="burger" aria-label="Menu"><i class="fas fa-bars"></i></button>
            </div>
        </div>
    </div>
    <div class="divider-gold"></div>
</header>

<!-- ============================ Mobile drawer =========================== -->
<div id="drawerBackdrop" class="drawer-backdrop"></div>
<aside id="mobileMenu" class="drawer">
    <div class="drawer-head">
        <div class="brand">
            <img src="<?php echo esc($company_logo); ?>" alt="" class="brand-logo" style="height:36px">
            <span class="brand-name"><?php echo esc($company_name); ?></span>
        </div>
        <button id="drawerClose" type="button" class="drawer-close" aria-label="Close"><i class="fas fa-xmark"></i></button>
    </div>

    <nav class="drawer-nav">
        <?php if ($is_authed): ?>
            <a href="<?php echo esc($header_dashboard_link); ?>"><i class="fas fa-gauge-high"></i><?php echo esc($lang->translate('dashboard', 'Dashboard')); ?></a>
        <?php endif; ?>
        <a href="<?php echo esc($home_url); ?>"><i class="fas fa-house"></i><?php echo esc($lang->translate('home', 'Home')); ?></a>
        <a href="<?php echo esc($anchor('#services')); ?>"><i class="fas fa-layer-group"></i><?php echo esc($lang->translate('services', 'Services')); ?></a>
        <a href="<?php echo esc($anchor('#about')); ?>"><i class="fas fa-shield-halved"></i><?php echo esc($lang->translate('about', 'About Us')); ?></a>
        <a href="<?php echo esc($anchor('#team')); ?>"><i class="fas fa-users"></i><?php echo esc($lang->translate('team', 'Team')); ?></a>
        <a href="<?php echo esc($anchor('#contact')); ?>"><i class="fas fa-envelope"></i><?php echo esc($lang->translate('contact', 'Contact')); ?></a>
        <?php if ($is_authed): ?>
            <a href="logout.php"><i class="fas fa-right-from-bracket"></i><?php echo esc($lang->translate('logout', 'Logout')); ?></a>
        <?php endif; ?>
    </nav>

    <div class="drawer-sec">
        <div class="divider-gold" style="margin-bottom:16px"></div>
        <p class="drawer-label"><?php echo esc($lang->translate('choose_language', 'Choose Language')); ?></p>
        <div class="drawer-langs">
            <?php foreach ($languages as $language): ?>
                <?php $active = ($language['code'] === $current_lang); ?>
                <a href="<?php echo langSwitchUrl($language['code']); ?>" class="drawer-lang<?php echo $active ? ' is-active' : ''; ?>">
                    <span class="lang-code"><?php echo esc(strtoupper(substr($language['code'], 0, 2))); ?></span>
                    <span><?php echo esc($language['name']); ?></span>
                </a>
            <?php endforeach; ?>
        </div>
    </div>

    <div class="drawer-actions">
        <?php if ($is_authed): ?>
            <a href="<?php echo esc($header_dashboard_link); ?>" class="btn btn-gold btn-block"><i class="fas fa-gauge-high"></i><?php echo esc($lang->translate('go_to_dashboard', 'Go to Dashboard')); ?></a>
        <?php else: ?>
            <a href="register.php" class="btn btn-gold btn-block"><i class="fas fa-user-plus"></i><?php echo esc($lang->translate('register', 'Register')); ?></a>
            <a href="login.php"    class="btn btn-ghost btn-block"><i class="fas fa-right-to-bracket"></i><?php echo esc($lang->translate('login', 'Login')); ?></a>
        <?php endif; ?>
    </div>

    <div class="drawer-social">
        <a href="<?php echo esc($facebook_url); ?>" target="_blank" rel="noopener"><i class="fab fa-facebook-f"></i></a>
        <a href="<?php echo esc($youtube_url); ?>" target="_blank" rel="noopener"><i class="fab fa-youtube"></i></a>
        <a href="<?php echo esc($whatsapp_channel_url); ?>" target="_blank" rel="noopener"><i class="fab fa-whatsapp"></i></a>
    </div>
</aside>

<script>
(function () {
    var menuBtn  = document.getElementById('menuBtn');
    var drawer   = document.getElementById('mobileMenu');
    var backdrop = document.getElementById('drawerBackdrop');
    var closeBtn = document.getElementById('drawerClose');

    function openDrawer()  { drawer.classList.add('open');    backdrop.classList.add('open');    document.body.style.overflow = 'hidden'; }
    function closeDrawer() { drawer.classList.remove('open'); backdrop.classList.remove('open'); document.body.style.overflow = ''; }

    if (menuBtn)  menuBtn.addEventListener('click', function (e) { e.stopPropagation(); openDrawer(); });
    if (closeBtn) closeBtn.addEventListener('click', closeDrawer);
    if (backdrop) backdrop.addEventListener('click', closeDrawer);
    if (drawer)   Array.prototype.forEach.call(drawer.querySelectorAll('a'), function (a) { a.addEventListener('click', closeDrawer); });

    var langBtn  = document.getElementById('languageBtn');
    var langMenu = document.getElementById('languageMenu');

    function closeLang() {
        if (!langMenu) return;
        langMenu.classList.remove('open');
        if (langBtn) langBtn.setAttribute('aria-expanded', 'false');
    }
    if (langBtn && langMenu) {
        langBtn.addEventListener('click', function (e) {
            e.stopPropagation();
            var open = !langMenu.classList.contains('open');
            langMenu.classList.toggle('open', open);
            langBtn.setAttribute('aria-expanded', open ? 'true' : 'false');
        });
        langMenu.addEventListener('click', function (e) { e.stopPropagation(); });
        document.addEventListener('click', closeLang);
    }

    document.addEventListener('keydown', function (e) { if (e.key === 'Escape') { closeDrawer(); closeLang(); } });

    var header = document.getElementById('siteHeader');
    var bar    = document.getElementById('scrollProgress');
    function onScroll() {
        var y = window.scrollY || document.documentElement.scrollTop;
        if (header) header.classList.toggle('scrolled', y > 24);
        if (bar) {
            var h = document.documentElement.scrollHeight - window.innerHeight;
            bar.style.width = (h > 0 ? (y / h) * 100 : 0) + '%';
        }
    }
    window.addEventListener('scroll', onScroll, { passive: true });
    onScroll();
})();
</script>
