@php
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
use App\Support\OwnerWorkspace;

$ownerWorkspaceGroups = collect();
$ownerWorkspaceActiveId = null;
$ownerWorkspaceActiveName = 'Todos los equipos';
$isOwnerArea = request()->routeIs('owner.*');

if ($isOwnerArea && Auth::check()) {
  $ownerOrganizationId = (int) (Auth::user()->organization_id ?? 0);
  if ($ownerOrganizationId > 0) {
    $ownerWorkspaceGroups = OwnerWorkspace::groupsForUser(Auth::user());
    $ownerWorkspaceActiveId = OwnerWorkspace::activeGroupId(request(), $ownerOrganizationId);
    $ownerWorkspaceActiveName = $ownerWorkspaceActiveId
      ? (string) ($ownerWorkspaceGroups->firstWhere('id', $ownerWorkspaceActiveId)?->name ?: 'Equipo')
      : 'Todos los equipos';
  }
}
@endphp

@if ($isOwnerArea)
<style>
  .owner-workspace-trigger {
    min-width: 210px;
    text-align: left;
    display: inline-flex;
    align-items: center;
    justify-content: space-between;
    font-weight: 600;
  }

  .owner-workspace-modal .modal-content {
    border-radius: 1rem;
  }

  .owner-workspace-modal .modal-body {
    padding-top: 0.5rem;
  }

  .owner-workspace-option {
    width: 100%;
    border: 1px solid #dbdade;
    border-radius: 0.65rem;
    background: #fff;
    color: #5d596c;
    min-height: 46px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.65rem 0.85rem;
    margin-bottom: 0.6rem;
    font-weight: 600;
  }

  .owner-workspace-option.active {
    border-color: #ff4c51;
    background: #fff5f5;
    color: #ff4c51;
  }
</style>
<script>
  document.addEventListener('DOMContentLoaded', () => {
    const workspaceModal = document.getElementById('ownerWorkspaceModal');
    if (!workspaceModal) return;
    if (workspaceModal.parentElement !== document.body) {
      document.body.appendChild(workspaceModal);
    }
  });
</script>
@endif

<!--  Brand demo (display only for navbar-full and hide on below xl) -->
@if (isset($navbarFull))
<div class="navbar-brand app-brand demo d-none d-xl-flex py-0 me-4 ms-0">
  <a href="{{ url('/') }}" class="app-brand-link">
    <span class="app-brand-logo demo">@include('_partials.macros')</span>
    <span class="app-brand-text demo menu-text fw-bold">{{ config('variables.templateName') }}</span>
  </a>

  <!-- Display menu close icon only for horizontal-menu with navbar-full -->
  @if (isset($menuHorizontal))
  <a href="javascript:void(0);" class="layout-menu-toggle menu-link text-large ms-auto d-xl-none">
    <i class="icon-base ti tabler-x icon-sm d-flex align-items-center justify-content-center"></i>
  </a>
  @endif
</div>
@endif

<!-- ! Not required for layout-without-menu -->
@if (!isset($navbarHideToggle))
<div
  class="layout-menu-toggle navbar-nav align-items-xl-center me-4 me-xl-0{{ isset($menuHorizontal) ? ' d-xl-none ' : '' }} {{ isset($contentNavbar) ? ' d-xl-none ' : '' }}">
  <a class="nav-item nav-link px-0 me-xl-6" href="javascript:void(0)">
    <i class="icon-base ti tabler-menu-2 icon-md"></i>
  </a>
</div>
@endif

<div class="navbar-nav-right d-flex align-items-center" id="navbar-collapse">
  @if ($configData['hasCustomizer'] == true)
  <!-- Style Switcher -->
  <div class="navbar-nav align-items-center">
    <li class="nav-item dropdown me-2 me-xl-0">
      <a class="nav-link dropdown-toggle hide-arrow" id="nav-theme" href="javascript:void(0);"
        data-bs-toggle="dropdown">
        <i class="icon-base ti tabler-sun icon-md theme-icon-active"></i>
        <span class="d-none ms-2" id="nav-theme-text">Toggle theme</span>
      </a>
      <ul class="dropdown-menu dropdown-menu-start" aria-labelledby="nav-theme-text">
        <li>
          <button type="button" class="dropdown-item align-items-center active" data-bs-theme-value="light"
            aria-pressed="false">
            <span><i class="icon-base ti tabler-sun icon-22px me-3" data-icon="sun"></i>Light</span>
          </button>
        </li>
        <li>
          <button type="button" class="dropdown-item align-items-center" data-bs-theme-value="dark" aria-pressed="true">
            <span><i class="icon-base ti tabler-moon-stars icon-22px me-3" data-icon="moon-stars"></i>Dark</span>
          </button>
        </li>
        <li>
          <button type="button" class="dropdown-item align-items-center" data-bs-theme-value="system"
            aria-pressed="false">
            <span><i class="icon-base ti tabler-device-desktop-analytics icon-22px me-3"
                data-icon="device-desktop-analytics"></i>System</span>
          </button>
        </li>
      </ul>
    </li>
  </div>
  <!-- / Style Switcher-->
  @endif
  <ul class="navbar-nav flex-row align-items-center ms-auto gap-2">
    @if ($isOwnerArea)
    <li class="nav-item d-none d-md-block">
      <button class="btn btn-outline-success owner-workspace-trigger" data-bs-toggle="modal" data-bs-target="#ownerWorkspaceModal">
        <span>Equipo: {{ $ownerWorkspaceActiveName }}</span>
        <i class="icon-base ti tabler-chevron-down icon-xs ms-2"></i>
      </button>
    </li>
    @endif
    <!-- User -->
    <li class="nav-item navbar-dropdown dropdown-user dropdown">
      <a class="nav-link dropdown-toggle hide-arrow p-0" href="javascript:void(0);" data-bs-toggle="dropdown">
        <div class="avatar avatar-online">
          <img src="{{ asset('assets/img/avatars/mariachi-avatar.svg') }}" alt="Avatar Mariachi" class="rounded-circle" />
        </div>
      </a>
      <ul class="dropdown-menu dropdown-menu-end">
        <li>
          <a class="dropdown-item mt-0" href="javascript:void(0);">
            <div class="d-flex align-items-center">
              <div class="flex-shrink-0 me-2">
                <div class="avatar avatar-online">
                  <img src="{{ asset('assets/img/avatars/mariachi-avatar.svg') }}" alt="Avatar Mariachi" class="rounded-circle" />
                </div>
              </div>
              <div class="flex-grow-1">
                <h6 class="mb-0">
                  @if (Auth::check())
                  {{ Auth::user()->name }}
                  @else
                  John Doe
                  @endif
                </h6>
                <small class="text-body-secondary">{{ Auth::check() ? Auth::user()->role : 'Invitado' }}</small>
              </div>
            </div>
          </a>
        </li>
        <li>
          <div class="dropdown-divider my-1 mx-n2"></div>
        </li>
        @if (Auth::check())
        <li>
          <a class="dropdown-item" href="{{ route('auth-logout') }}"
            onclick="event.preventDefault(); document.getElementById('logout-form').submit();">
            <i class="icon-base ti tabler-logout icon-md me-3"></i><span>Cerrar sesión</span>
          </a>
        </li>
        <form method="POST" id="logout-form" action="{{ route('auth-logout') }}">
          @csrf
        </form>
        @else
        <li>
          <div class="d-grid px-2 pt-2 pb-1">
            <a class="btn btn-sm btn-danger d-flex" href="{{ route('login') }}" target="_blank">
              <small class="align-middle">Login</small>
              <i class="icon-base ti tabler-login ms-2 icon-14px"></i>
            </a>
          </div>
        </li>
        @endif
      </ul>
    </li>
    <!--/ User -->
  </ul>
</div>

@if ($isOwnerArea)
<div class="modal fade" id="ownerWorkspaceModal" tabindex="-1" aria-labelledby="ownerWorkspaceModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered owner-workspace-modal">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="ownerWorkspaceModalLabel">Cambiar equipos</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Cerrar"></button>
      </div>
      <div class="modal-body">
        <form action="{{ route('owner.workspace.switch') }}" method="POST">
          @csrf
          <div class="mb-2">
            <button type="submit" name="mariachi_group_id" value="" class="owner-workspace-option {{ empty($ownerWorkspaceActiveId) ? 'active' : '' }}">
              <span>Todos los equipos</span>
              @if (empty($ownerWorkspaceActiveId))
              <i class="icon-base ti tabler-check icon-sm"></i>
              @endif
            </button>
            @foreach ($ownerWorkspaceGroups as $group)
            <button type="submit" name="mariachi_group_id" value="{{ $group->id }}" class="owner-workspace-option {{ (int) $ownerWorkspaceActiveId === (int) $group->id ? 'active' : '' }}">
              <span>{{ $group->name }}</span>
              @if ((int) $ownerWorkspaceActiveId === (int) $group->id)
              <i class="icon-base ti tabler-check icon-sm"></i>
              @endif
            </button>
            @endforeach
          </div>
        </form>
      </div>
      <div class="modal-footer">
        @can('owner.mariachi_groups.view')
        <a href="{{ route('owner.settings.mariachi-groups') }}" class="btn btn-label-primary me-auto">Administrar equipos</a>
        @endcan
        <button type="button" class="btn btn-label-secondary" data-bs-dismiss="modal">Cancelar</button>
      </div>
    </div>
  </div>
</div>
@endif
