<?php
/*
|--------------------------------------------------------------------------
| company/sp_nav.php — shared header/navigation for the Service Portal pages.
| Include AFTER session + company vars are available. Optional: set
| $spActive before include ('customers' | 'expenses' | 'stock' | 'allitems')
| to highlight the current link.
|--------------------------------------------------------------------------
*/
$__role    = $_SESSION['role'] ?? '';
$__company = $_SESSION['company_name'] ?? 'Company';
$__back    = ($__role === 'company_admin') ? 'dashboard.php' : '../customer/company_home.php';
$__a       = $spActive ?? '';
if (!function_exists('esc')) { function esc($v){ return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8'); } }
// Can this user handle the request queue (Packing / Stock / Manager role)?
$__canQueue = false; $__prole = 'none'; $__prBadge = 0;
if (isset($pdo)) {
    if (!function_exists('pr_role') && file_exists(__DIR__.'/../config/part_request_flow.php')) { require_once __DIR__.'/../config/part_request_flow.php'; }
    if (function_exists('pr_role')) {
        try {
            $__prole    = pr_role($pdo, (int)($_SESSION['company_id'] ?? 0), (int)($_SESSION['user_id'] ?? 0), $__role);
            $__canQueue = pr_can_queue($__prole);
            if ($__canQueue) { $__prBadge = pr_badge_count($pdo, (int)($_SESSION['company_id'] ?? 0), $__prole); }
        } catch (Throwable $e) { $__canQueue = false; }
    }
}
?>
<style>
    .sp-nav-bar { display:flex; justify-content:flex-start; align-items:center; gap:8px; flex-wrap:wrap; margin-bottom:12px; font-family:'Noto Sans Lao','Inter',Arial,sans-serif; }
    .sp-nav-bar a { display:inline-flex; align-items:center; justify-content:center; height:38px; padding:0 16px; border-radius:8px; text-decoration:none; font-weight:700; font-size:13px; white-space:nowrap; box-sizing:border-box; }
    .sp-nav-bar .sp-back { background:#0f172a; color:#fff; }
    .sp-nav-bar .sp-back:hover { background:#1e293b; }
    .sp-nav-bar .sp-link { background:#fff; border:1px solid #e6eaf2; color:#475569; }
    .sp-nav-bar .sp-link:hover { color:#2563eb; border-color:#bfdbfe; }
    .sp-nav-bar .sp-link.on { background:#2563eb; color:#fff; border-color:#2563eb; }
    .sp-nav-bar .sp-home { background:#0f766e; color:#fff; border-color:#0f766e; }
    .sp-nav-bar .sp-home:hover { background:#115e59; color:#fff; border-color:#115e59; }
    .sp-nav-bar .sp-link .nav-badge { display:inline-flex; align-items:center; justify-content:center; min-width:22px; height:22px; padding:0 6px; margin-left:7px; border-radius:999px; background:#dc2626; color:#fff; font-size:13px; font-weight:900; box-shadow:0 2px 5px rgba(220,38,38,.45); }
    .sp-nav-bar .sp-link.notif { background:#fee2e2; border-color:#fca5a5; color:#b91c1c; }
    .sp-nav-bar .sp-link.notif.on { background:#dc2626; color:#fff; border-color:#dc2626; }
</style>
<div class="sp-nav-bar">
    <a class="sp-back" href="<?= $__back ?>"<?= $__role==='company_admin' ? '' : ' onclick="if(document.referrer){history.back();return false;}"' ?>>&larr; Back</a>
    <a class="sp-link sp-home" href="../customer/desktop.php">🏠 Main Page</a>
    <a class="sp-link<?= $__a==='customers' ? ' on' : '' ?>" href="sp_customers.php">Customers</a>
    <a class="sp-link<?= $__a==='expenses' ? ' on' : '' ?>" href="sp_expenses.php">Customer Expenses</a>
    <a class="sp-link<?= $__a==='stock' ? ' on' : '' ?>" href="sp_stock.php">Stock</a>
    <a class="sp-link<?= $__a==='allitems' ? ' on' : '' ?>" href="sp_stock.php?list=1">All Items</a>
    <a class="sp-link<?= $__a==='create' ? ' on' : '' ?>" href="sp_part_requests.php">Request Form (Create)</a>
    <?php if ($__canQueue): ?><a class="sp-link<?= $__a==='queue' ? ' on' : '' ?><?= $__prBadge>0 ? ' notif' : '' ?>" href="sp_part_request_queue.php">Request Queue<?php if ($__prBadge>0): ?><span class="nav-badge"><?= $__prBadge ?></span><?php endif; ?></a><?php endif; ?>
</div>
<script>
/* Auto-refresh Service Portal pages so queues & badges stay live.
   Pauses while the user is typing or a popup/dropdown is open. */
(function(){
    var REFRESH_MS = 15000;
    function busy(){
        var a = document.activeElement;
        if (a && /^(INPUT|TEXTAREA|SELECT)$/.test(a.tagName)) return true;
        if (document.querySelector('.ac-list.show, .ac-grid.show')) return true;
        var dm = document.getElementById('drum-modal');
        if (dm && getComputedStyle(dm).display !== 'none') return true;
        return false;
    }
    setInterval(function(){ if (!busy() && !document.hidden) { location.reload(); } }, REFRESH_MS);
})();
</script>