<!doctype html>
<html lang="es">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Mi Perfil - 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 Sesión</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-3">Editar datos de administradora</h1>

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

                @if(session('admin_action_error'))
                    <div class="alert alert-danger" role="alert">
                        {{ session('admin_action_error') }}
                    </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="card border shadow-sm">
                    <div class="card-body">
                        <form id="formPerfilAdmin" method="POST" action="/administrador/perfil" class="row g-3">
                            @csrf
                            @method('PUT')
                            <input id="contra_actual_hidden" name="contra_actual" type="hidden">

                            <div class="col-12 col-md-6">
                                <label class="form-label" for="nombre">Nombre</label>
                                <input id="nombre" name="nombre" type="text" class="form-control" value="{{ old('nombre', $administrador->persona?->nombre ?? '') }}" required>
                            </div>

                            <div class="col-12 col-md-6">
                                <label class="form-label" for="apellido">Apellido</label>
                                <input id="apellido" name="apellido" type="text" class="form-control" value="{{ old('apellido', $administrador->persona?->apellido ?? '') }}" required>
                            </div>

                            <div class="col-12 col-md-6">
                                <label class="form-label" for="dni">DNI</label>
                                <input id="dni" type="text" class="form-control bg-light" value="Campo no editable" readonly>
                            </div>

                            <div class="col-12 col-md-6">
                                <label class="form-label" for="correo">Correo</label>
                                <input id="correo" name="correo" type="email" class="form-control" value="{{ old('correo', $administrador->correo) }}" required>
                            </div>

                            <div class="col-12 col-md-6">
                                <label class="form-label" for="telefono">Celular</label>
                                <input id="telefono" name="telefono" type="text" class="form-control" value="{{ old('telefono', $telefonoActual->telefono ?? '') }}" placeholder="Ej.: 3411234567">
                            </div>

                            <div class="col-12 col-md-6">
                                <label class="form-label" for="usuario">Usuario</label>
                                <input id="usuario" name="usuario" type="text" class="form-control" value="{{ old('usuario', $administrador->credencial?->usuario ?? '') }}" required>
                            </div>

                            <div class="col-12 col-md-6">
                                <label class="form-label" for="contra">Nueva contraseña</label>
                                <input id="contra" name="contra" type="password" class="form-control" autocomplete="new-password" placeholder="Dejar en blanco para no cambiar">
                                <div class="form-text">Mínimo 6 caracteres.</div>
                            </div>

                            <div class="col-12 col-md-6">
                                <label class="form-label" for="contra_confirmation">Confirmar nueva contraseña</label>
                                <input id="contra_confirmation" name="contra_confirmation" type="password" class="form-control" autocomplete="new-password">
                            </div>

                            <div class="col-12 d-flex justify-content-end gap-2 mt-2">
                                <a href="/administrador/dashboard" class="btn btn-outline-secondary">Volver</a>
                                <button type="button" class="btn btn-outline-dark" data-bs-toggle="modal" data-bs-target="#modalPreguntaSecreta">
                                    Configurar pregunta y respuesta secreta
                                </button>
                                <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#modalConfirmarCambiosAdmin">Guardar cambios</button>
                            </div>
                        </form>
                    </div>
                </div>

                <div class="modal fade" id="modalConfirmarCambiosAdmin" tabindex="-1" aria-labelledby="modalConfirmarCambiosAdminLabel" aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h2 class="modal-title fs-5" id="modalConfirmarCambiosAdminLabel">Confirmar cambios</h2>
                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Cerrar"></button>
                            </div>
                            <div class="modal-body">
                                <p class="text-muted small mb-3">Para guardar los cambios, ingresa tu contraseña actual.</p>
                                <div class="mb-0">
                                    <label class="form-label" for="contra_actual_modal">Contraseña actual</label>
                                    <input id="contra_actual_modal" type="password" class="form-control" autocomplete="current-password" required>
                                    <div class="invalid-feedback">Debes ingresar tu contraseña actual para guardar los cambios.</div>
                                </div>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancelar</button>
                                <button type="button" id="btnConfirmarGuardarPerfilAdmin" class="btn btn-primary">Guardar cambios</button>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="modal fade" id="modalPreguntaSecreta" tabindex="-1" aria-labelledby="modalPreguntaSecretaLabel" aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h2 class="modal-title fs-5" id="modalPreguntaSecretaLabel">Configurar pregunta y respuesta secreta</h2>
                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Cerrar"></button>
                            </div>
                            <form method="POST" action="/administrador/perfil/pregunta-secreta" class="row g-0">
                                @csrf
                                <div class="modal-body">
                                    <div class="mb-3">
                                        <label class="form-label" for="contra_actual_secreta">Contraseña actual</label>
                                        <input id="contra_actual_secreta" name="contra_actual_secreta" type="password" class="form-control" autocomplete="current-password" required>
                                    </div>
                                    <div class="mb-3">
                                        <label class="form-label" for="pregunta_secreta">Pregunta secreta</label>
                                        <input id="pregunta_secreta" name="pregunta_secreta" type="text" class="form-control" value="{{ old('pregunta_secreta') }}" maxlength="255" required>
                                    </div>
                                    <div class="mb-0">
                                        <label class="form-label" for="respuesta_secreta">Respuesta secreta</label>
                                        <input id="respuesta_secreta" name="respuesta_secreta" type="text" class="form-control" maxlength="255" required>
                                    </div>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancelar</button>
                                    <button type="submit" class="btn btn-primary">Guardar</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </main>
    <script>
        document.addEventListener('DOMContentLoaded', function () {
            var formPerfil = document.getElementById('formPerfilAdmin');
            var modalConfirmacion = document.getElementById('modalConfirmarCambiosAdmin');
            var inputContraActualModal = document.getElementById('contra_actual_modal');
            var inputContraActualHidden = document.getElementById('contra_actual_hidden');
            var botonConfirmarGuardado = document.getElementById('btnConfirmarGuardarPerfilAdmin');

            if (formPerfil && modalConfirmacion && inputContraActualModal && inputContraActualHidden && botonConfirmarGuardado) {
                var enviarFormularioPerfil = function () {
                    if (inputContraActualModal.value === '') {
                        inputContraActualModal.classList.add('is-invalid');
                        inputContraActualModal.focus();
                        return;
                    }

                    inputContraActualModal.classList.remove('is-invalid');
                    inputContraActualHidden.value = inputContraActualModal.value;

                    if (typeof bootstrap !== 'undefined') {
                        bootstrap.Modal.getOrCreateInstance(modalConfirmacion).hide();
                    }

                    formPerfil.submit();
                };

                botonConfirmarGuardado.addEventListener('click', enviarFormularioPerfil);

                inputContraActualModal.addEventListener('input', function () {
                    if (inputContraActualModal.value !== '') {
                        inputContraActualModal.classList.remove('is-invalid');
                    }
                });

                inputContraActualModal.addEventListener('keydown', function (event) {
                    if (event.key === 'Enter') {
                        event.preventDefault();
                        enviarFormularioPerfil();
                    }
                });

                modalConfirmacion.addEventListener('hidden.bs.modal', function () {
                    inputContraActualModal.value = '';
                    inputContraActualModal.classList.remove('is-invalid');
                });
            }

            @if($errors->has('contra_actual'))
                if (modalConfirmacion && typeof bootstrap !== 'undefined') {
                    bootstrap.Modal.getOrCreateInstance(modalConfirmacion).show();
                }
            @endif

            @if($errors->has('contra_actual_secreta') || $errors->has('pregunta_secreta') || $errors->has('respuesta_secreta'))
                var modalPreguntaSecreta = document.getElementById('modalPreguntaSecreta');
                if (modalPreguntaSecreta && typeof bootstrap !== 'undefined') {
                    bootstrap.Modal.getOrCreateInstance(modalPreguntaSecreta).show();
                }
            @endif
        });
    </script>
    @include('partials.reportar-falla-boton')
</body>
</html>



