<!DOCTYPE html>
<html lang="pt">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>@yield('title', 'Painel') · NESTAB OPS</title>
    <link rel="stylesheet" href="{{ asset('css/nestab.css') }}">
</head>
<body class="app-body {{ $operatingMode === 'administration' ? 'is-admin-mode' : '' }}">
    <div class="app-shell">
        <aside class="sidebar" id="sidebar">
            <a href="{{ route('dashboard') }}" class="sidebar-brand">
                <img src="{{ asset('images/nestab-logomarca.png') }}" alt="NESTAB Security GPS">
                <span>OPS</span>
            </a>
            <div class="sidebar-context">
                <small>MODO ACTUAL</small>
                <strong>{{ $operatingMode === 'administration' ? 'Administração' : 'Operação' }}</strong>
            </div>
            <nav class="sidebar-nav" aria-label="Navegação principal">
                <span class="nav-section">VISÃO GERAL</span>
                <a href="{{ route('dashboard') }}" class="nav-link {{ request()->routeIs('dashboard') ? 'active' : '' }}">
                    <span class="nav-icon">⌂</span><span>Painel</span>
                </a>
                @if(auth()->user()->hasPermission('foundation.view'))
                    <a href="{{ route('foundation') }}" class="nav-link {{ request()->routeIs('foundation') ? 'active' : '' }}">
                        <span class="nav-icon">◇</span><span>Fundação</span>
                    </a>
                @endif
                @if($operatingMode === 'administration' && auth()->user()->hasPrivilegedRole())
                    <a href="{{ route('admin.index') }}" class="nav-link {{ request()->routeIs('admin.*') && !request()->routeIs('admin.audit.*') ? 'active' : '' }}">
                        <span class="nav-icon">◆</span><span>Administração</span>
                    </a>
                @endif
                @if(auth()->user()->hasPermission('audit.view'))
                    <a href="{{ route('admin.audit.index') }}" class="nav-link {{ request()->routeIs('admin.audit.*') ? 'active' : '' }}">
                        <span class="nav-icon">≡</span><span>Auditoria</span>
                    </a>
                @endif
                <span class="nav-section">MÓDULOS</span>
                @foreach([
                    ['Clientes e Comercial', '02'],
                    ['Técnico e Inventário', '03'],
                    ['Financeiro e Cobrança', '04'],
                    ['Administração e RH', '05'],
                ] as [$label, $phase])
                    <span class="nav-link disabled"><span class="nav-icon">{{ $phase }}</span><span>{{ $label }}</span><small>Em preparação</small></span>
                @endforeach
            </nav>
            <div class="sidebar-footer">
                <div class="user-mini">
                    <span class="avatar">{{ mb_strtoupper(mb_substr(auth()->user()->name, 0, 1)) }}</span>
                    <div><strong>{{ auth()->user()->name }}</strong><small>{{ auth()->user()->username }}</small></div>
                </div>
                <form method="POST" action="{{ route('logout') }}">
                    @csrf
                    <button class="text-button" type="submit">Terminar sessão</button>
                </form>
            </div>
        </aside>

        <div class="main-column">
            @if($operatingMode === 'administration')
                <div class="admin-mode-banner">Modo de Administração activo · acções privilegiadas ficam registadas</div>
            @endif
            <header class="topbar">
                <button class="mobile-menu" type="button" data-sidebar-toggle aria-label="Abrir menu">☰</button>
                @if(auth()->user()->hasPermission('search.use'))
                    <form action="{{ route('search') }}" method="GET" class="global-search">
                        <span>⌕</span>
                        <input type="search" name="q" value="{{ request('q') }}" placeholder="Pesquisar em todo o NESTAB OPS..." aria-label="Pesquisa global">
                        <kbd>Enter</kbd>
                    </form>
                @else
                    <div></div>
                @endif
                <div class="topbar-actions">
                    <div class="context-switcher">
                        <span class="status-dot"></span>
                        <select aria-label="Filial activa" data-branch-switch>
                            @if(auth()->user()->canUseConsolidatedView())
                                <option value="{{ route('branch-context.update', 'consolidado') }}" {{ $activeBranch ? '' : 'selected' }}>Todas as filiais</option>
                            @endif
                            @foreach($availableBranches as $branch)
                                <option value="{{ route('branch-context.update', $branch) }}" {{ $activeBranch?->id === $branch->id ? 'selected' : '' }}>{{ $branch->name }}</option>
                            @endforeach
                        </select>
                    </div>
                    @if(auth()->user()->hasPrivilegedRole())
                        <form method="POST" action="{{ route('operating-mode.update') }}">
                            @csrf
                            <input type="hidden" name="mode" value="{{ $operatingMode === 'administration' ? 'operation' : 'administration' }}">
                            <button class="mode-button {{ $operatingMode === 'administration' ? 'active' : '' }}" type="submit">
                                {{ $operatingMode === 'administration' ? 'Sair da Administração' : 'Modo de Administração' }}
                            </button>
                        </form>
                    @endif
                </div>
            </header>

            <main class="content">
                @if(session('success'))<div class="alert success">{{ session('success') }}</div>@endif
                @if(session('warning'))<div class="alert warning">{{ session('warning') }}</div>@endif
                @yield('content')
            </main>
        </div>
    </div>
    <form method="POST" data-branch-form hidden>@csrf</form>
    <script src="{{ asset('js/nestab.js') }}"></script>
</body>
</html>
