<!doctype html>
<html lang="es">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Revisar Formularios - Profesional</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">
                @php
                    $nombreSesion = trim((string) session('personal_nombre', 'Profesional'));
                    $nombreSaludo = $nombreSesion !== '' ? explode(' ', $nombreSesion)[0] : 'Profesional';
                @endphp
                <a class="navbar-brand d-flex align-items-center gap-2" href="/profesional/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="fw-semibold">¡Hola, {{ $nombreSaludo }}!</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="/profesional/dashboard">Mi Agenda</a></li>
                        <li class="nav-item"><a class="btn app-navbar-link" href="/profesional/clientes">Mis Clientes</a></li>
                        <li class="nav-item"><a class="btn app-navbar-link" href="/profesional/mis-datos">Mis Datos</a></li>
                        <li class="nav-item">
                            <a class="btn app-navbar-link position-relative" href="/profesional/formularios">
                                Revisar Formularios
                                @if(($formulariosPendientesCount ?? 0) > 0)
                                    <span class="position-absolute top-0 start-100 translate-middle p-1 bg-danger rounded-circle js-notificaciones-badge">
                                        <span class="visually-hidden">Hay formularios pendientes</span>
                                    </span>
                                @endif
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="btn app-navbar-link position-relative" href="/profesional/notificaciones" data-notificaciones-claves='@json($notificacionesClaves ?? [])'>
                                Notificaciones
                                @if(($notificacionesCount ?? 0) > 0)
                                    <span class="position-absolute top-0 start-100 translate-middle p-1 bg-danger rounded-circle js-notificaciones-badge d-none">
                                        <span class="visually-hidden">Hay notificaciones</span>
                                    </span>
                                @endif
                            </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="d-flex justify-content-between align-items-center mb-3">
            <h1 class="h4 mb-0">Revisar Formularios</h1>
            <a href="/profesional/dashboard" class="btn btn-outline-secondary btn-sm">Volver</a>
        </div>

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

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

        <div class="card border shadow-sm mb-3">
            <div class="card-body">
                <form method="GET" action="/profesional/formularios" class="row g-3 align-items-end">
                    <div class="col-12 col-md-5">
                        <label for="q" class="form-label">Buscar</label>
                        <input id="q" name="q" type="text" class="form-control" value="{{ request('q') }}" placeholder="Nombre, correo, celular, servicio...">
                    </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="pendiente" @selected(request('estado') === 'pendiente')>Pendientes</option>
                            <option value="aceptado" @selected(request('estado') === 'aceptado')>Aceptados</option>
                            <option value="aceptado_por_otro" @selected(request('estado') === 'aceptado_por_otro')>Aceptados por otro</option>
                            <option value="rechazado" @selected(request('estado') === 'rechazado')>Rechazados</option>
                        </select>
                    </div>

                    <div class="col-12 col-md-2">
                        <label for="cliente" class="form-label">Es cliente</label>
                        <select id="cliente" name="cliente" class="form-select">
                            <option value="">Todos</option>
                            <option value="si" @selected(request('cliente') === 'si')>Sí</option>
                            <option value="no" @selected(request('cliente') === 'no')>No</option>
                        </select>
                    </div>

                    <div class="col-12 col-md-2 d-flex gap-2">
                        <button type="submit" class="btn btn-primary w-100">Filtrar</button>
                        <a href="/profesional/formularios" 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>Fecha</th>
                        <th>Nombre</th>
                        <th>Correo</th>
                        <th>Celular</th>
                        <th>Servicio</th>
                        <th>Es cliente</th>
                        <th>Estado</th>
                        <th>Acciones</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($formularios as $formulario)
                        @php
                            $fechaFormulario = $formulario->created_at
                                ? $formulario->created_at->format('d/m/Y H:i:s')
                                : ($formulario->fechaenvio
                                    ? \Illuminate\Support\Carbon::parse($formulario->fechaenvio)->format('d/m/Y')
                                    : '-');
                        @endphp
                        <tr>
                            <td>{{ $fechaFormulario }}</td>
                            <td>{{ $formulario->nombrecompleto ?? '-' }}</td>
                            <td>{{ $formulario->correo ?? '-' }}</td>
                            <td>{{ $formulario->celular ?? '-' }}</td>
                            <td>{{ $formulario->servicio?->titulo ?? '-' }}</td>
                            <td>
                                @if($formulario->cliente_id)
                                    <span class="badge text-bg-success">Sí</span>
                                @else
                                    <span class="badge text-bg-danger">No</span>
                                @endif
                            </td>
                            <td>
                                @php
                                    $estadoBaseFormulario = trim((string) ($formulario->estado ?? ''));
                                    $estadoFormulario = (($formulario->aceptado_por_otro ?? false)
                                        && strcasecmp($estadoBaseFormulario, 'Aceptado') === 0)
                                        ? 'Aceptado por otro'
                                        : $estadoBaseFormulario;
                                    $estadoClase = match (mb_strtolower($estadoFormulario)) {
                                        'rechazado por todos' => 'text-bg-danger',
                                        'aceptado' => 'text-bg-success',
                                        'aceptado por otro' => 'text-bg-secondary',
                                        default => 'text-bg-warning',
                                    };
                                @endphp
                                <span class="badge {{ $estadoClase }}">{{ $estadoFormulario !== '' ? $estadoFormulario : '-' }}</span>
                            </td>
                            <td>
                                <a href="/profesional/formularios/{{ $formulario->id }}" class="btn btn-sm btn-outline-primary">Ver</a>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="8" class="text-center text-muted py-4">No hay formularios cargados.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        @if($formularios->hasPages())
            <div class="d-flex justify-content-center mt-3">
                {{ $formularios->links() }}
            </div>
        @endif
    </main>
    @include('partials.reportar-falla-boton')
</body>
</html>



