<?php require_once __DIR__ . '/functions.php';
$siteName = setting('site_name', 'MJ Marketplace');
$pageTitle = $pageTitle ?? $siteName;
$metaDescription = $metaDescription ?? setting('site_description', 'Buy, sell, chat and deal safely with MJ Marketplace.');
$user = current_user();
$mjChatUnread = $user ? mj_chat_unread_count((int)$user['id']) : 0;
$enableAutoCountryRedirect = !empty($enableAutoCountryRedirect);
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title><?= seo_title($pageTitle) ?></title>
  <meta name="description" content="<?= e($metaDescription) ?>">
  <meta property="og:title" content="<?= e($pageTitle) ?>">
  <meta property="og:description" content="<?= e($metaDescription) ?>">
  <meta property="og:type" content="website">
  <meta name="theme-color" content="#2563eb">
  <link rel="manifest" href="<?= base_url('manifest.json') ?>">
  <link rel="preconnect" href="https://cdn.jsdelivr.net">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/swiper@11/swiper-bundle.min.css" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/aos@2.3.4/dist/aos.css" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/leaflet@1.9.4/dist/leaflet.css" rel="stylesheet">
  <link rel="stylesheet" href="<?= e(asset_versioned('css/style.css')) ?>">
  <script>window.MJ_BASE_URL = <?= json_encode(rtrim(base_url('/'), '/')) ?>; window.MJ_CSRF_TOKEN = <?= json_encode(function_exists('mj427_form_token') ? mj427_form_token() : csrf_token()) ?>; window.MJ_FORM_TOKEN = window.MJ_CSRF_TOKEN; window.MJ_NOTIFICATION_POLL_MS = <?= (int)setting('global_notification_poll_seconds','25')*1000 ?>; window.MJ_AUTO_COUNTRY_REDIRECT = <?= $enableAutoCountryRedirect ? 'true' : 'false' ?>; window.MJ_AREA_DETECTION = <?= setting('allow_ip_area_detection','1') !== '0' ? 'true' : 'false' ?>; window.MJ_DEBUG_MODE = <?= function_exists('mj428_admin_can_debug') && mj428_admin_can_debug() ? 'true' : 'false' ?>;</script>
  <link rel="stylesheet" href="<?= e(asset_versioned('css/ultimate-mj.css')) ?>">
  <link rel="stylesheet" href="<?= e(asset_versioned('css/listing-module.css')) ?>">
  <link rel="stylesheet" href="<?= e(asset_versioned('css/frontend-unified.css')) ?>">
  <link rel="stylesheet" href="<?= e(asset_versioned('css/frontend-v288.css')) ?>">
  <link rel="stylesheet" href="<?= e(asset_versioned('css/deals-v2916.css')) ?>">
  <link rel="stylesheet" href="<?= e(asset_versioned('css/v3-upgrade.css')) ?>">
  <link rel="stylesheet" href="<?= e(asset_versioned('css/mj-v304.css')) ?>">
  <link rel="stylesheet" href="<?= e(asset_versioned('css/mj-v30427-forms-location.css')) ?>">
  <link rel="stylesheet" href="<?= e(asset_versioned('css/mj-v30428-popup-speed.css')) ?>">
</head>
<body class="mj-public-body">
<div class="mj-top-strip d-none d-lg-block">
  <div class="container d-flex justify-content-between align-items-center small">
    <span><i class="bi bi-shield-check me-1"></i> Safe marketplace with country, city and company verification options.</span>
    <span><i class="bi bi-clock me-1"></i> Local time support by country / state / city</span>
  </div>
</div>
<header class="site-header sticky-top">
  <nav class="navbar navbar-expand-lg navbar-light mj-navbar">
    <div class="container">
      <a class="navbar-brand mj-brand" href="<?= base_url('/') ?>">
        <span class="mj-brand-mark"><i class="bi bi-shop"></i></span>
        <span><b>MJ</b> Marketplace</span>
      </a>
      <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#mjMainNav"><span class="navbar-toggler-icon"></span></button>
      <div class="collapse navbar-collapse" id="mjMainNav">
        <ul class="navbar-nav ms-auto align-items-lg-center gap-lg-1">
          <li class="nav-item"><a class="nav-link <?= mj_is_active_url('index.php') ?>" href="<?= base_url('/') ?>"><i class="bi bi-house-door me-1"></i>Home</a></li>
          <li class="nav-item"><a class="nav-link <?= mj_is_active_url('browse.php') ?>" href="<?= base_url('browse.php') ?>"><i class="bi bi-search me-1"></i>Browse</a></li>
          <li class="nav-item"><a class="nav-link <?= mj_is_active_url('companies.php') ?>" href="<?= base_url('companies.php') ?>"><i class="bi bi-buildings me-1"></i>Companies</a></li>
          <li class="nav-item"><a class="nav-link <?= mj_is_active_url('deals.php') ?>" href="<?= base_url('deals.php') ?>"><i class="bi bi-fire me-1"></i>Deals</a></li>
          <li class="nav-item"><a class="nav-link <?= mj_is_active_url('near-me.php') ?>" href="<?= base_url('near-me.php') ?>"><i class="bi bi-geo-alt me-1"></i>Near Me</a></li>
          <li class="nav-item"><a class="nav-link" href="<?= base_url('countries.php') ?>"><i class="bi bi-globe2 me-1"></i>Countries</a></li>
          <?php if(!$user): ?><li class="nav-item ms-lg-2"><a class="nav-link mj-v3-notification-btn" href="<?= base_url('notifications.php') ?>" title="Notifications"><i class="bi bi-bell"></i><span class="mj-v3-badge" data-mj-notification-count>0</span></a></li><?php endif; ?>
          <li class="nav-item"><a class="btn btn-primary mj-nav-cta ms-lg-2" href="<?= base_url('post-listing.php') ?>"><i class="bi bi-plus-circle me-1"></i>Post Free</a></li>
          <?php if ($user): ?>
            <li class="nav-item ms-lg-2"><a class="nav-link mj-v3-notification-btn" href="<?= base_url('notifications.php') ?>" title="Notifications"><i class="bi bi-bell"></i><span class="mj-v3-badge" data-mj-notification-count>0</span></a></li>
            <li class="nav-item dropdown ms-lg-2">
              <a class="nav-link dropdown-toggle mj-user-pill" href="#" role="button" data-bs-toggle="dropdown"><i class="bi bi-person-circle me-1"></i><?= e($user['name']) ?></a>
              <ul class="dropdown-menu dropdown-menu-end shadow border-0 rounded-4">
                <li><a class="dropdown-item" href="<?= base_url('my-account.php') ?>"><i class="bi bi-speedometer2 me-2"></i>My Account</a></li>
                <li><a class="dropdown-item" href="<?= base_url('selling-center.php') ?>"><i class="bi bi-shop-window me-2"></i>Selling Center</a></li>
                <li><a class="dropdown-item" href="<?= base_url('my-listings.php') ?>"><i class="bi bi-tags me-2"></i>My Listings</a></li>
                <li><a class="dropdown-item" href="<?= base_url('my-companies.php') ?>"><i class="bi bi-building me-2"></i>My Companies</a></li>
                <li><a class="dropdown-item" href="<?= base_url('my-deals.php') ?>"><i class="bi bi-fire me-2"></i>My Deals</a></li>
                <li><a class="dropdown-item" href="<?= base_url('my-leads.php?source=deal') ?>"><i class="bi bi-envelope-open me-2"></i>Deal Enquiries</a></li>
                <li><a class="dropdown-item d-flex justify-content-between align-items-center" href="<?= base_url('messages.php') ?>"><span><i class="bi bi-chat-dots me-2"></i>Messages</span><span data-mj-chat-unread-count class="badge bg-danger" style="<?= $mjChatUnread>0 ? '' : 'display:none' ?>"><?= e($mjChatUnread) ?></span></a></li>
                <li><a class="dropdown-item" href="<?= base_url('notifications.php') ?>"><i class="bi bi-bell me-2"></i>Notifications <span data-mj-notification-count class="badge bg-danger" style="display:none">0</span></a></li>
                <li><a class="dropdown-item" href="<?= base_url('buying-center.php') ?>"><i class="bi bi-bag-heart me-2"></i>Buying Center</a></li>
                <li><a class="dropdown-item" href="<?= base_url('appointments.php') ?>"><i class="bi bi-calendar-check me-2"></i>Appointments</a></li>
                <li><a class="dropdown-item" href="<?= base_url('my-orders.php') ?>"><i class="bi bi-bag-check me-2"></i>Orders</a></li>
                <li><a class="dropdown-item" href="<?= base_url('account-center.php') ?>"><i class="bi bi-person-gear me-2"></i>Account Center</a></li>
                <li><a class="dropdown-item d-flex justify-content-between align-items-center" href="<?= base_url('my-leads.php') ?>"><span><i class="bi bi-people me-2"></i>CRM / Leads</span><span data-mj-leads-count class="badge bg-success" style="display:none">0</span></a></li>
                <li><a class="dropdown-item" href="<?= base_url('billing-payments.php') ?>"><i class="bi bi-credit-card-2-front me-2"></i>Billing & Payments</a></li>
                <li><a class="dropdown-item" href="<?= base_url('billing-payments.php?tab=cart') ?>"><i class="bi bi-cart3 me-2"></i>Service Cart <?php if(function_exists('mj2919_cart_count') && ($cc=mj2919_cart_count((int)$user['id']))>0): ?><span class="badge bg-primary ms-1"><?=e($cc)?></span><?php endif; ?></a></li>
                <li><a class="dropdown-item" href="<?= base_url('trust-verification.php') ?>"><i class="bi bi-shield-check me-2"></i>Trust & Verification</a></li>
                <li><a class="dropdown-item" href="<?= base_url('help-support.php') ?>"><i class="bi bi-headset me-2"></i>Help & Support</a></li>
                <li><hr class="dropdown-divider"></li>
                <li><a class="dropdown-item text-danger" href="<?= base_url('logout.php') ?>"><i class="bi bi-box-arrow-right me-2"></i>Logout</a></li>
              </ul>
            </li>
          <?php else: ?>
            <li class="nav-item ms-lg-2"><a class="nav-link" href="<?= base_url('login.php') ?>">Login</a></li>
            <li class="nav-item"><a class="btn btn-outline-primary rounded-pill px-3" href="<?= base_url('register.php') ?>">Register</a></li>
          <?php endif; ?>
        </ul>
      </div>
    </div>
  </nav>
</header>
<main class="mj-main">
  <noscript><div class="container mt-3"><div class="alert alert-info">Enable JavaScript for automatic country detection and richer marketplace features.</div></div></noscript>
  <?php if (!empty($_SESSION['impersonator_admin_id'])): ?>
    <div class="container mt-3"><div class="alert alert-warning d-flex justify-content-between align-items-center rounded-4">
      <span><i class="bi bi-person-badge me-1"></i>Super Admin is logged in as this user for support/testing.</span>
      <a class="btn btn-sm btn-dark" href="<?= base_url('stop-impersonation.php') ?>">Return to Admin</a>
    </div></div>
  <?php endif; ?>
  <?php $mjAreaLabel = visitor_area_label(); if ($mjAreaLabel): ?>
    <div class="container mt-3"><div class="mj-local-priority-bar"><i class="bi bi-geo-alt-fill"></i><span>Showing nearby listings and companies first for <b><?= e($mjAreaLabel) ?></b>.</span><a href="<?= base_url('near-me.php') ?>">View Near Me</a></div></div>
  <?php endif; ?>
  <?php if (!empty($_SESSION['flash'])): ?><div class="container mt-3"><?php show_flash(); ?></div><?php endif; ?>
