<?php
// pwa/components/pwa_nav.php
// Outputs header (logo + title + user) and bottom nav based on role
// Call pwa_nav($userArray)

if (!function_exists('pwa_nav')) {
    function pwa_nav($user = [])
    {
        $role = $user['role'] ?? 'Guest';
        $displayName = $user['name'] ?? ($user['id'] ? 'User#'.$user['id'] : 'Guest');

        // Topbar HTML
        ?>
        <div class="pwa-topbar">
            <img class="pwa-logo" src="/assets/menu_logo.png" alt="AtulyaSolar">
            <div class="pwa-title">AtulyaSolar — Portal</div>
            <div class="pwa-user"><?= htmlspecialchars($displayName, ENT_QUOTES) ?></div>
        </div>
        <div class="pwa-body-pad"></div>
        <?php

        // Bottom nav items per role
        $items = [];

        // Core items common to most roles
        $items_common = [
            ['id'=>'home','label'=>'Home','href'=>'/pwa/index.html','icon'=>'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor"><path d="M3 9.5L12 3l9 6.5"/><path d="M9 22V12h6v10"/></svg>'],
            ['id'=>'customers','label'=>'Customers','href'=>'/pwa/customers.html','icon'=>'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor"><circle cx="12" cy="7" r="4"/><path d="M6 21v-2a4 4 0 0 1 4-4h4a4 4 0 0 1 4 4v2"/></svg>'],
            ['id'=>'followups','label'=>'Followups','href'=>'/pwa/followups/index.html','icon'=>'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="7 10 12 15 17 10"/></svg>'],
            ['id'=>'tasks','label'=>'Tasks','href'=>'/pwa/tasks.html','icon'=>'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor"><path d="M9 11l3 3L22 4"/><path d="M21 12v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h11"/></svg>']
        ];

        // Role-specific adjustments
        switch (strtolower($role)) {
            case 'captain':
            case 'solardooot':
            case 'solardoot':
            case 'solardoor': // tolerate typos
                $items = $items_common;
                break;

            case 'statehead':
            case 'districthead':
                // Add reports/dashboard
                $items = $items_common;
                array_splice($items, 1, 0, [[
                    'id'=>'dashboard','label'=>'Dashboard','href'=>'/pwa/home/index.html','icon'=>'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor"><path d="M3 13h8V3H3v10z"/><path d="M21 21V11h-8v10h8z"/></svg>'
                ]]);
                break;

            case 'admin':
                $items = [
                    ['id'=>'dashboard','label'=>'Dashboard','href'=>'/pwa/admin/dashboard.php','icon'=>'<svg viewBox="0 0 24 24"><path d="M3 13h8V3H3v10z"/></svg>'],
                    ['id'=>'customers','label'=>'Customers','href'=>'/pwa/admin/customers.php','icon'=>'<svg viewBox="0 0 24 24"></svg>'],
                    ['id'=>'settings','label'=>'Settings','href'=>'/pwa/admin/settings.php','icon'=>'<svg viewBox="0 0 24 24"></svg>'],
                ];
                break;

            default:
                $items = $items_common;
                break;
        }

        // Render bottom nav
        echo '<div class="pwa-bottom-nav" role="navigation" aria-label="PWA Navigation">';
        foreach ($items as $it) {
            $href = htmlspecialchars($it['href'], ENT_QUOTES);
            $label = htmlspecialchars($it['label'], ENT_QUOTES);
            $icon = $it['icon'] ?? '';
            echo "<a class='pwa-bottom-item' href='{$href}' title='{$label}'>{$icon}<div>{$label}</div></a>";
        }
        echo '</div>';
    }
}
