<!doctype html>
<html lang="es">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Contenido - 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-4">Gestión de Contenido</h1>

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

        @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="row g-4">
            <div class="col-12" id="seccion-quienes-somos">
                <div class="card border shadow-sm">
                    <div class="card-header bg-white">
                        <h2 class="h5 mb-0">Editar Quienes Somos</h2>
                    </div>
                    <div class="card-body">
                        <form action="/administrador/contenido-web" method="POST" class="row g-3">
                            @csrf
                            <div class="col-12">
                                <label for="quienessomos" class="form-label">Texto visible en la sección Quienes Somos</label>
                                <textarea id="quienessomos" name="quienessomos" class="form-control" rows="5">{{ old('quienessomos', $contenidoWeb?->quienessomos ?? '') }}</textarea>
                            </div>
                            <div class="col-12 d-flex justify-content-end">
                                <button type="submit" class="btn btn-primary">Guardar cambios</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>

            <div class="col-12" id="seccion-profesiones">
                <div class="card border shadow-sm">
                    <div class="card-header bg-white d-flex justify-content-between align-items-center">
                        <h2 class="h5 mb-0">Agregar, editar o dar de baja Profesión</h2>
                        <a href="/administrador/profesiones/crear" class="btn btn-sm btn-primary">Agregar profesión</a>
                    </div>
                    <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>ID</th>
                                        <th>Título</th>
                                        <th>Visible en formulario</th>
                                        <th class="text-end">Acciones</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @forelse($profesiones as $profesion)
                                        <tr>
                                            <td>{{ $profesion->id }}</td>
                                            <td>{{ $profesion->titulo }}</td>
                                            <td>{{ $profesion->visible_en_formulario ? 'Sí' : 'No' }}</td>
                                            <td class="text-end">
                                                <div class="d-inline-flex gap-2">
                                                    <a href="/administrador/profesiones/{{ $profesion->id }}/editar" class="btn btn-sm btn-outline-primary">Editar</a>
                                                </div>
                                            </td>
                                        </tr>
                                    @empty
                                        <tr>
                                            <td colspan="4" class="text-center text-muted py-4">No hay profesiones cargadas.</td>
                                        </tr>
                                    @endforelse
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-12" id="seccion-servicios">
                <div class="card border shadow-sm">
                    <div class="card-header bg-white d-flex justify-content-between align-items-center">
                        <h2 class="h5 mb-0">Agregar, editar o dar de baja Servicio</h2>
                        <a href="/administrador/servicios/crear" class="btn btn-sm btn-primary">Agregar servicio</a>
                    </div>
                    <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>ID</th>
                                        <th>Título</th>
                                        <th>Profesión</th>
                                        <th>Estado</th>
                                        <th>Visible en web</th>
                                        <th class="text-end">Acciones</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @forelse($servicios as $servicio)
                                        <tr>
                                            <td>{{ $servicio->id }}</td>
                                            <td>{{ $servicio->titulo }}</td>
                                            <td>{{ $servicio->profesion?->titulo ?? '-' }}</td>
                                            <td>{{ $servicio->estado }}</td>
                                            <td>{{ ($servicio->visibleenweb ?? 'si') === 'si' ? 'Si' : 'No' }}</td>
                                            <td class="text-end">
                                                <div class="d-inline-flex gap-2">
                                                    <a href="/administrador/servicios/{{ $servicio->id }}/editar" class="btn btn-sm btn-outline-primary">Editar</a>
                                                </div>
                                            </td>
                                        </tr>
                                    @empty
                                        <tr>
                                            <td colspan="6" class="text-center text-muted py-4">No hay servicios cargados.</td>
                                        </tr>
                                    @endforelse
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-12" id="seccion-asistente">
                <div class="card border shadow-sm">
                    <div class="card-header bg-white">
                        <h2 class="h5 mb-0">Asistente Virtual (FAQ)</h2>
                    </div>
                    <div class="card-body border-bottom">
                        <div class="alert alert-info py-2" role="alert">
                            Para configurar textos fijos del asistente, usé estas intenciones: <strong>ui_nombre</strong> (nombre del asistente), <strong>ui_burbuja</strong> (burbuja flotante), <strong>ui_panel_inicio</strong> (primer mensaje del panel) y <strong>ui_error</strong> (mensaje cuando no encuentra respuesta). En esas intenciones, las palabras clave son opcionales. Los <strong>ui_chips</strong> ahora se administran en la sección separada de abajo.
                        </div>
                        <form action="/administrador/contenido-web/faqs" method="POST" class="row g-3">
                            @csrf
                            <div class="col-md-4">
                                <label for="faq_intencion" class="form-label">Intención (opcional)</label>
                                <input type="text" id="faq_intencion" name="intencion" class="form-control" value="{{ old('intencion') }}" maxlength="100" placeholder="Ej: turnos">
                            </div>
                            <div class="col-md-4">
                                <label for="faq_orden" class="form-label">Orden</label>
                                <input type="number" id="faq_orden" name="orden" class="form-control" value="{{ old('orden', 100) }}" min="0" max="65535" required>
                            </div>
                            <div class="col-md-4">
                                <label for="faq_activo" class="form-label">Estado</label>
                                <select id="faq_activo" name="activo" class="form-select" required>
                                    <option value="1" {{ old('activo', '1') === '1' ? 'selected' : '' }}>Activo</option>
                                    <option value="0" {{ old('activo') === '0' ? 'selected' : '' }}>Inactivo</option>
                                </select>
                            </div>
                            <div class="col-12">
                                <label for="faq_palabras_clave" class="form-label">Palabras clave (separadas por coma, opcional para intenciones ui_*)</label>
                                <input type="text" id="faq_palabras_clave" name="palabras_clave" class="form-control" value="{{ old('palabras_clave') }}" placeholder="Ej: turno, consulta, formulario">
                            </div>
                            <div class="col-12">
                                <label for="faq_respuesta" class="form-label">Respuesta</label>
                                <textarea id="faq_respuesta" name="respuesta" class="form-control" rows="3" maxlength="5000" required>{{ old('respuesta') }}</textarea>
                            </div>
                            <div class="col-12 d-flex justify-content-end">
                                <button type="submit" class="btn btn-primary">Agregar FAQ</button>
                            </div>
                        </form>
                    </div>

                    <div class="card-body border-bottom">
                        <div class="d-flex justify-content-between align-items-center mb-3">
                            <h3 class="h6 mb-0">ui_chips (sugerencias rápidas)</h3>
                        </div>

                        <form action="/administrador/contenido-web/faqs" method="POST" class="row g-3 mb-3">
                            @csrf
                            <input type="hidden" name="intencion" value="ui_chips">
                            <input type="hidden" name="palabras_clave" value="">

                            <div class="col-md-7">
                                <label for="chip_respuesta" class="form-label">Texto del ui_chip</label>
                                <input type="text" id="chip_respuesta" name="respuesta" class="form-control" value="{{ old('respuesta') }}" maxlength="255" required placeholder="Ej: Quiero sacar un turno">
                            </div>
                            <div class="col-md-2">
                                <label for="chip_orden" class="form-label">Orden</label>
                                <input type="number" id="chip_orden" name="orden" class="form-control" value="{{ old('orden', 100) }}" min="0" max="65535" required>
                            </div>
                            <div class="col-md-2">
                                <label for="chip_activo" class="form-label">Estado</label>
                                <select id="chip_activo" name="activo" class="form-select" required>
                                    <option value="1" {{ old('activo', '1') === '1' ? 'selected' : '' }}>Activo</option>
                                    <option value="0" {{ old('activo') === '0' ? 'selected' : '' }}>Inactivo</option>
                                </select>
                            </div>
                            <div class="col-md-1 d-flex align-items-end">
                                <button type="submit" class="btn btn-outline-primary w-100">➕</button>
                            </div>
                        </form>

                        <div class="table-responsive">
                            <table class="table table-striped table-hover mb-0 align-middle">
                                <thead class="table-light">
                                    <tr>
                                        <th>ID</th>
                                        <th>ui_chip</th>
                                        <th>Orden</th>
                                        <th>Estado</th>
                                        <th class="text-end">Acciones</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @forelse($chipsAsistenteAdmin as $chip)
                                        <tr>
                                            <td>{{ $chip->id }}</td>
                                            <td colspan="3">
                                                <form action="/administrador/contenido-web/faqs/{{ $chip->id }}" method="POST" class="row g-2">
                                                    @csrf
                                                    @method('PUT')
                                                    <input type="hidden" name="intencion" value="ui_chips">
                                                    <input type="hidden" name="palabras_clave" value="">

                                                    <div class="col-md-8">
                                                        <input type="text" name="respuesta" class="form-control form-control-sm" value="{{ $chip->respuesta }}" maxlength="255" required>
                                                    </div>
                                                    <div class="col-md-2">
                                                        <input type="number" name="orden" class="form-control form-control-sm" value="{{ $chip->orden }}" min="0" max="65535" required>
                                                    </div>
                                                    <div class="col-md-2">
                                                        <select name="activo" class="form-select form-select-sm" required>
                                                            <option value="1" {{ $chip->activo ? 'selected' : '' }}>Activo</option>
                                                            <option value="0" {{ !$chip->activo ? 'selected' : '' }}>Inactivo</option>
                                                        </select>
                                                    </div>
                                                    <div class="col-12 d-flex justify-content-end">
                                                        <button type="submit" class="btn btn-sm btn-outline-primary">Guardar</button>
                                                    </div>
                                                </form>
                                            </td>
                                            <td class="text-end">
                                                <form action="/administrador/contenido-web/faqs/{{ $chip->id }}" method="POST" onsubmit="return confirm('¿Confirmás borrar este ui_chip?');">
                                                    @csrf
                                                    @method('DELETE')
                                                    <button type="submit" class="btn btn-sm btn-outline-danger">Borrar</button>
                                                </form>
                                            </td>
                                        </tr>
                                    @empty
                                        <tr>
                                            <td colspan="5" class="text-center text-muted py-4">No hay ui_chips cargados.</td>
                                        </tr>
                                    @endforelse
                                </tbody>
                            </table>
                        </div>
                    </div>

                    <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>ID</th>
                                        <th>Intención</th>
                                        <th>Palabras clave</th>
                                        <th>Respuesta</th>
                                        <th>Orden</th>
                                        <th>Estado</th>
                                        <th class="text-end">Acciones</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @forelse($faqsAsistente as $faq)
                                        <tr>
                                            <td>{{ $faq->id }}</td>
                                            <td colspan="5">
                                                <form action="/administrador/contenido-web/faqs/{{ $faq->id }}" method="POST" class="row g-2">
                                                    @csrf
                                                    @method('PUT')
                                                    <div class="col-md-3">
                                                        <input type="text" name="intencion" class="form-control form-control-sm" value="{{ $faq->intencion }}" maxlength="100" placeholder="Intención">
                                                    </div>
                                                    <div class="col-md-3">
                                                        <input type="text" name="palabras_clave" class="form-control form-control-sm" value="{{ implode(', ', $faq->palabras_clave ?? []) }}" placeholder="Palabras clave">
                                                    </div>
                                                    <div class="col-md-4">
                                                        <input type="text" name="respuesta" class="form-control form-control-sm" value="{{ $faq->respuesta }}" maxlength="5000" required>
                                                    </div>
                                                    <div class="col-md-1">
                                                        <input type="number" name="orden" class="form-control form-control-sm" value="{{ $faq->orden }}" min="0" max="65535" required>
                                                    </div>
                                                    <div class="col-md-1">
                                                        <select name="activo" class="form-select form-select-sm" required>
                                                            <option value="1" {{ $faq->activo ? 'selected' : '' }}>Act.</option>
                                                            <option value="0" {{ !$faq->activo ? 'selected' : '' }}>Inact.</option>
                                                        </select>
                                                    </div>
                                                    <div class="col-12 d-flex justify-content-end">
                                                        <button type="submit" class="btn btn-sm btn-outline-primary">Guardar</button>
                                                    </div>
                                                </form>
                                            </td>
                                            <td class="text-end">
                                                <form action="/administrador/contenido-web/faqs/{{ $faq->id }}" method="POST" onsubmit="return confirm('¿Confirmás borrar esta FAQ?');">
                                                    @csrf
                                                    @method('DELETE')
                                                    <button type="submit" class="btn btn-sm btn-outline-danger">Borrar</button>
                                                </form>
                                            </td>
                                        </tr>
                                    @empty
                                        <tr>
                                            <td colspan="7" class="text-center text-muted py-4">No hay FAQs cargadas para el asistente.</td>
                                        </tr>
                                    @endforelse
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </main>
    @include('partials.reportar-falla-boton')
</body>
</html>



