<!doctype html>
<html lang="es">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Asignar Servicios - 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">
        <div class="row justify-content-center">
            <div class="col-12 col-lg-8">
                <h1 class="h4 mb-2">Asignar servicios</h1>
                <p class="text-muted mb-3">
                    Profesional: <strong>{{ $profesional->persona?->nombre }} {{ $profesional->persona?->apellido }}</strong>
                    (DNI: {{ $profesional->dni }})
                </p>

                @if($errors->any())
                    <div class="alert alert-danger" role="alert">
                        <ul class="mb-0 ps-3">
                            @foreach($errors->all() as $error)
                                <li>{{ $error }}</li>
                            @endforeach
                        </ul>
                    </div>
                @endif

                <div class="card border shadow-sm">
                    <div class="card-body">
                        <form method="POST" action="/administrador/profesionales/{{ $profesional->id }}/asignaciones" class="row g-3">
                            @csrf
                            @method('PUT')

                            <div class="col-12">
                                <label class="form-label">Profesión actual</label>
                                <input type="text" class="form-control bg-light" value="{{ $profesional->profesion?->titulo ?? '-' }}" readonly>
                            </div>

                            <div class="col-12">
                                <label class="form-label d-block mb-2">Servicios</label>
                                <div id="servicios-container" class="d-flex flex-column gap-2">
                                    @foreach($servicios as $servicio)
                                        <div class="form-check" data-profesion-id="{{ $servicio->profesion_id }}">
                                            <input
                                                class="form-check-input"
                                                type="checkbox"
                                                id="servicio_{{ $servicio->id }}"
                                                name="servicio_ids[]"
                                                value="{{ $servicio->id }}"
                                                @checked(in_array((string) $servicio->id, array_map('strval', old('servicio_ids', $serviciosSeleccionados ?? [])), true))
                                            >
                                            <label class="form-check-label" for="servicio_{{ $servicio->id }}">
                                                {{ $servicio->titulo }}
                                            </label>
                                        </div>
                                    @endforeach
                                </div>
                                <div class="form-text">Solo se muestran servicios activos de la profesión actual.</div>
                            </div>

                            <div class="col-12 d-flex gap-2 justify-content-end mt-2">
                                <a href="/administrador/profesionales" class="btn btn-outline-secondary">Cancelar</a>
                                <button type="submit" class="btn btn-primary">Guardar asignaciones</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </main>

    <script>
        (function () {
            const serviciosContainer = document.getElementById('servicios-container');
            const profesionId = '{{ (string) $profesional->profesion_id }}';

            if (!serviciosContainer) {
                return;
            }

            const items = Array.from(serviciosContainer.querySelectorAll('[data-profesion-id]'));

            const filtrarServicios = () => {
                items.forEach((item) => {
                    const input = item.querySelector('input[type="checkbox"]');
                    const visible = item.dataset.profesionId === profesionId;

                    item.classList.toggle('d-none', !visible);

                    if (!visible && input) {
                        input.checked = false;
                    }
                });
            };

            filtrarServicios();
        })();
    </script>
    @include('partials.reportar-falla-boton')
</body>
</html>



