<!doctype html>
<html lang="es">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Profesionales - Administrador</title>
    <script>
        (function () {
            const root = document.documentElement;
            root.classList.add('sidebar-nav-pending');

            const releasePending = function () {
                root.classList.remove('sidebar-nav-pending');
            };

            const timeoutId = window.setTimeout(releasePending, 1200);

            if (document.querySelector('.admin-sidebar')) {
                window.clearTimeout(timeoutId);
                releasePending();
                return;
            }

            const observer = new MutationObserver(function () {
                if (!document.querySelector('.admin-sidebar')) {
                    return;
                }

                observer.disconnect();
                window.clearTimeout(timeoutId);
                releasePending();
            });

            observer.observe(document.documentElement, {
                childList: true,
                subtree: true,
            });
        })();
    </script>
    <style>
        @media (min-width: 992px) {
            html.sidebar-nav-pending main {
                visibility: hidden;
            }
        }
    </style>
    @vite(['resources/css/app.css', 'resources/js/app.js'])
</head>
<body id="top" class="d-flex flex-column min-vh-100 bg-light">
    <header class="app-navbar">
        <nav class="navbar navbar-expand-lg">
            <div class="container">
                <a class="navbar-brand d-flex align-items-center" href="/administrador/dashboard">
                    <div class="d-flex align-items-center justify-content-center" style="width: 130px; height: 52px;">
                        <img src="{{ asset('images/logo.png') }}" alt="Logo" class="img-fluid" style="max-height: 70px; width: auto; object-fit: contain;">
                    </div>
                    <span class="app-navbar-greeting ms-2">¡Hola, Administrador!</span>
                </a>

                <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#menuPrincipal" aria-controls="menuPrincipal" aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>

                <div class="collapse navbar-collapse" id="menuPrincipal">
                    <ul class="navbar-nav mx-auto mb-2 mb-lg-0">
                        <li class="nav-item"><a class="btn app-navbar-link" href="/administrador/profesionales">Profesionales</a></li>
                        <li class="nav-item"><a class="btn app-navbar-link" href="/administrador/dashboard">Mis Datos</a></li>
                        <li class="nav-item"><a class="btn app-navbar-link" href="/administrador/contenido-web">Contenido</a></li>
                        <li class="nav-item"><a class="btn app-navbar-link" href="/administrador/emails">Emails</a></li>
                        <li class="nav-item"><a class="btn app-navbar-link" href="/administrador/logs">Logs</a></li>
                        <li class="nav-item"><a class="btn app-navbar-link" href="/administrador/fallas">Fallas</a></li>
                                                <li class="nav-item">
                            <a class="btn app-navbar-link position-relative" href="/administrador/bugs">
                                Bugs
                                @if(($bugsPendientesCount ?? 0) > 0)
                                    <span class="position-absolute top-0 start-100 translate-middle p-1 bg-danger rounded-circle">
                                        <span class="visually-hidden">Hay bugs pendientes</span>
                                    </span>
                                @endif
                            </a>
                        </li>
                        <li class="nav-item"><a class="btn app-navbar-link" href="/administrador/backups">Backup</a></li>
                    </ul>

                    <a class="btn app-navbar-link" href="/logout">Cerrar Sesion</a>
                </div>
            </div>
        </nav>
    </header>

    <main class="container py-4 flex-grow-1">
        <h1 class="h4 mb-3">Profesionales</h1>

        @if(session('admin_action_success'))
            <div class="alert alert-success" role="alert">
                {{ session('admin_action_success') }}
            </div>
        @endif

        @if(session('admin_action_error'))
            <div class="alert alert-danger" role="alert">
                {{ session('admin_action_error') }}
            </div>
        @endif

        <div class="card border shadow-sm mb-3">
            <div class="card-body">
                <form method="GET" action="/administrador/profesionales" class="row g-3 align-items-end">
                    <div class="col-12 col-md-6">
                        <label for="q" class="form-label">Buscar profesional</label>
                        <input id="q" name="q" type="text" class="form-control" value="{{ request('q') }}" placeholder="Nombre, apellido, DNI, correo o matrícula">
                    </div>

                    <div class="col-12 col-md-3">
                        <label for="estado" class="form-label">Estado</label>
                        <select id="estado" name="estado" class="form-select">
                            <option value="">Todos</option>
                            <option value="activos" @selected(request('estado') === 'activos')>Activos</option>
                            <option value="inactivos" @selected(request('estado') === 'inactivos')>Inactivos</option>
                        </select>
                    </div>

                    <div class="col-12 col-md-3 d-flex gap-2">
                        <button type="submit" class="btn btn-primary w-100">Filtrar</button>
                        <a href="/administrador/profesionales" class="btn btn-outline-secondary w-100">Limpiar</a>
                    </div>
                </form>
            </div>
        </div>

        <div class="table-responsive bg-white border rounded shadow-sm">
            <table class="table table-striped table-hover mb-0 align-middle">
                <thead class="table-light">
                    <tr>
                        <th>ID</th>
                        <th>Nombre</th>
                        <th>Apellido</th>
                        <th>DNI</th>
                        <th>Correo</th>
                        <th>Matrícula</th>
                        <th>Profesión</th>
                        <th>Rol</th>
                        <th>Baja</th>
                        <th>Acciones</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($profesionales as $profesional)
                        @php
                            $persona = $profesional->persona;
                            $rol = $profesional->credencialProfesional?->rol ?? '-';
                            $profesionActual = $profesional->profesion?->titulo ?? '-';
                        @endphp
                        <tr>
                            <td>{{ $profesional->id }}</td>
                            <td>{{ $persona?->nombre ?? '-' }}</td>
                            <td>{{ $persona?->apellido ?? '-' }}</td>
                            <td>{{ $profesional->dni }}</td>
                            <td>{{ $profesional->correo }}</td>
                            <td>{{ $profesional->matricula }}</td>
                            <td>{{ $profesionActual }}</td>
                            <td>{{ $rol }}</td>
                            <td>
                                @if((int) $profesional->baja_id !== 1)
                                    <span class="badge text-bg-danger">Sí</span>
                                @else
                                    <span class="badge text-bg-success">No</span>
                                @endif
                            </td>
                            <td>
                                <div class="d-flex flex-wrap gap-2">
                                    <form method="POST" action="/administrador/profesionales/{{ $profesional->id }}/baja" onsubmit="return confirm('{{ (int) $profesional->baja_id !== 1 ? '¿Confirmás reactivar a este profesional?' : '¿Confirmás dar de baja a este profesional?' }}');">
                                        @csrf
                                        <button type="submit" class="btn btn-sm {{ (int) $profesional->baja_id !== 1 ? 'btn-outline-success' : 'btn-outline-danger' }}" title="{{ (int) $profesional->baja_id !== 1 ? 'Reactivar profesional' : 'Dar de baja' }}" aria-label="{{ (int) $profesional->baja_id !== 1 ? 'Reactivar profesional' : 'Dar de baja' }}">❌</button>
                                    </form>
                                    <a href="/administrador/profesionales/{{ $profesional->id }}/editar" class="btn btn-sm btn-outline-primary" title="Editar" aria-label="Editar">✏️</a>
                                    <a href="/administrador/profesionales/{{ $profesional->id }}/asignaciones" class="btn btn-sm btn-outline-secondary" title="Asignar servicios" aria-label="Asignar servicios">⚙️</a>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="10" class="text-center text-muted py-4">No hay profesionales cargados.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        @if($profesionales->hasPages())
            <div class="d-flex justify-content-center mt-3">
                {{ $profesionales->links() }}
            </div>
        @endif

        <div class="mt-3 d-flex justify-content-center">
            <a href="/administrador/profesionales/nuevo" class="btn btn-primary">Agregar nuevo profesional</a>
        </div>
    </main>
    @include('partials.reportar-falla-boton')
</body>
</html>



