<!doctype html>
<html lang="es">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Consultas sin respuesta - 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" href="/administrador/bugs">Bugs</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="d-flex justify-content-between align-items-center mb-3">
            <div>
                <h1 class="h4 mb-0">Consultas sin respuesta del asistente</h1>
                @if($totalSinRevisar > 0)
                    <span class="badge bg-warning text-dark mt-1">{{ $totalSinRevisar }} sin revisar</span>
                @endif
            </div>
            <a href="/administrador/contenido/asistente-virtual" class="btn btn-outline-secondary btn-sm">Ir al asistente</a>
        </div>

        @if(session('admin_action_success'))
            <div class="alert alert-success alert-dismissible fade show" role="alert">
                {{ session('admin_action_success') }}
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Cerrar"></button>
            </div>
        @endif

        <div class="card border shadow-sm">
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-striped table-hover mb-0 align-middle">
                        <thead class="table-light">
                            <tr>
                                <th style="width: 80px;">ID</th>
                                <th>Consulta del usuario</th>
                                <th style="width: 180px;">Recibida</th>
                                <th style="width: 110px;">Estado</th>
                                <th style="width: 180px;">Acciones</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($pendientes as $item)
                                <tr @class(['table-warning' => !$item->revisado])>
                                    <td>{{ $item->id }}</td>
                                    <td>{{ $item->consulta }}</td>
                                    <td>
                                        @if($item->created_at)
                                            {{ \Illuminate\Support\Carbon::parse($item->created_at)->format('d/m/Y H:i') }}
                                        @else
                                            -
                                        @endif
                                    </td>
                                    <td>
                                        @if($item->revisado)
                                            <span class="badge bg-success">Revisado</span>
                                        @else
                                            <span class="badge bg-warning text-dark">Pendiente</span>
                                        @endif
                                    </td>
                                    <td class="d-flex gap-2 flex-wrap">
                                        @if(!$item->revisado)
                                            <form method="POST" action="/administrador/asistente-consultas/{{ $item->id }}/marcar-revisado">
                                                @csrf
                                                <button type="submit" class="btn btn-outline-success btn-sm">Marcar revisado</button>
                                            </form>
                                        @endif
                                        <form method="POST" action="/administrador/asistente-consultas/{{ $item->id }}">
                                            @csrf
                                            @method('DELETE')
                                            <button
                                                type="submit"
                                                class="btn btn-outline-danger btn-sm"
                                                onclick="return confirm('¿Eliminar esta consulta?')"
                                            >Eliminar</button>
                                        </form>
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="5" class="text-center text-muted py-4">No hay consultas sin respuesta registradas.</td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>

                @if($pendientes->hasPages())
                    <div class="px-3 py-2">
                        {{ $pendientes->links() }}
                    </div>
                @endif
            </div>
        </div>
    </main>

    <footer class="text-center py-3 text-muted small">
        &copy; {{ date('Y') }} Abogadas Litoral. Panel de administración.
    </footer>
</body>
</html>
