<!doctype html>
<html lang="es">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Fallas - 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">
            <h1 class="h4 mb-0">Fallas del sistema</h1>
            <a href="/administrador/dashboard" class="btn btn-outline-secondary btn-sm">Volver</a>
        </div>

        <div class="card border shadow-sm mb-3">
            <div class="card-body">
                <form method="GET" action="/administrador/fallas" class="row g-3 align-items-end">
                    <div class="col-12 col-md-3">
                        <label for="codigo" class="form-label">Codigo</label>
                        <select id="codigo" name="codigo" class="form-select">
                            <option value="">Todos</option>
                            @foreach($codigosDisponibles as $codigo)
                                <option value="{{ $codigo }}" @selected(request('codigo') === $codigo)>{{ $codigo }}</option>
                            @endforeach
                        </select>
                    </div>

                    <div class="col-12 col-md-2">
                        <label for="desde" class="form-label">Desde</label>
                        <input id="desde" name="desde" type="date" class="form-control" value="{{ request('desde') }}">
                    </div>

                    <div class="col-12 col-md-2">
                        <label for="hasta" class="form-label">Hasta</label>
                        <input id="hasta" name="hasta" type="date" class="form-control" value="{{ request('hasta') }}">
                    </div>

                    <div class="col-12 col-md-3">
                        <label for="q" class="form-label">Texto</label>
                        <input id="q" name="q" type="text" class="form-control" value="{{ request('q') }}" placeholder="Mensaje, URL o trace...">
                    </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="/administrador/fallas" class="btn btn-outline-secondary w-100">Limpiar</a>
                    </div>
                </form>
            </div>
        </div>

        <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 style="width: 170px;">Fecha y hora</th>
                                <th style="width: 160px;">Codigo</th>
                                <th style="width: 260px;">Mensaje</th>
                                <th style="width: 240px;">URL</th>
                                <th style="width: 320px;">Trace</th>
                                <th style="width: 120px;">Accion</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($fallas as $falla)
                                @php
                                    $fechaFalla = $falla->fecha_hora ? \Illuminate\Support\Carbon::parse($falla->fecha_hora)->format('d/m/Y H:i:s') : '-';
                                @endphp
                                <tr>
                                    <td>{{ $falla->id }}</td>
                                    <td>{{ $fechaFalla }}</td>
                                    <td>{{ $falla->codigo ?: '-' }}</td>
                                    <td style="white-space: pre-line;">{{ \Illuminate\Support\Str::limit((string) $falla->mensaje, 180) }}</td>
                                    <td>{{ \Illuminate\Support\Str::limit((string) $falla->url, 100) }}</td>
                                    <td style="white-space: pre-line; min-width: 320px;">{{ \Illuminate\Support\Str::limit((string) $falla->track_trace, 320) }}</td>
                                    <td>
                                        <button
                                            type="button"
                                            class="btn btn-outline-primary btn-sm"
                                            data-bs-toggle="modal"
                                            data-bs-target="#modalFalla{{ $falla->id }}"
                                        >
                                            Ver detalle
                                        </button>

                                        <div class="modal fade" id="modalFalla{{ $falla->id }}" tabindex="-1" aria-labelledby="modalFalla{{ $falla->id }}Label" aria-hidden="true">
                                            <div class="modal-dialog modal-xl modal-dialog-scrollable">
                                                <div class="modal-content">
                                                    <div class="modal-header">
                                                        <h2 class="modal-title fs-5" id="modalFalla{{ $falla->id }}Label">Detalle de falla #{{ $falla->id }}</h2>
                                                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Cerrar"></button>
                                                    </div>
                                                    <div class="modal-body">
                                                        <div class="mb-3">
                                                            <strong>Fecha y hora:</strong>
                                                            {{ $fechaFalla }}
                                                        </div>
                                                        <div class="mb-3">
                                                            <strong>Codigo:</strong>
                                                            {{ $falla->codigo ?: '-' }}
                                                        </div>
                                                        <div class="mb-3">
                                                            <strong>URL:</strong>
                                                            <div class="text-break">{{ $falla->url ?: '-' }}</div>
                                                        </div>
                                                        <div class="mb-3">
                                                            <strong>Mensaje:</strong>
                                                            <pre class="mb-0 p-3 bg-light border rounded" style="white-space: pre-wrap; word-break: break-word;">{{ (string) $falla->mensaje }}</pre>
                                                        </div>
                                                        <div>
                                                            <strong>Trace completo:</strong>
                                                            <pre class="mb-0 p-3 bg-light border rounded" style="white-space: pre-wrap; word-break: break-word;">{{ (string) $falla->track_trace }}</pre>
                                                        </div>
                                                    </div>
                                                    <div class="modal-footer">
                                                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cerrar</button>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="7" class="text-center text-muted py-4">No hay fallas registradas.</td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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