<!doctype html>
<html lang="es">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Configurar Mi Agenda</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'])
    <style>
        .panel-agenda {
            background: #fff;
            border: 1px solid rgba(0, 0, 0, 0.1);
            border-radius: 12px;
            padding: 1rem;
        }

        .tabla-config th,
        .tabla-config td {
            vertical-align: middle;
        }
    </style>
</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" href="/profesional/formularios">Revisar Formularios</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">
        @php
            $filasAtencion = old('dias', isset($bloquesAtencion) ? (is_array($bloquesAtencion) ? $bloquesAtencion : $bloquesAtencion->toArray()) : []);
            $filasReceso = old('recesos', isset($bloquesReceso) ? (is_array($bloquesReceso) ? $bloquesReceso : $bloquesReceso->toArray()) : []);
            $filasVacaciones = old('vacaciones', isset($vacaciones) ? (is_array($vacaciones) ? $vacaciones : $vacaciones->toArray()) : []);
            $filasFeriados = old('feriados', isset($feriados) ? (is_array($feriados) ? $feriados : $feriados->toArray()) : []);
            $duracionTurnoActual = (int) old('duracionturno', $duracionTurno ?? 30);
        @endphp

        <div class="d-flex justify-content-between align-items-center flex-wrap gap-2 mb-3">
            <h1 class="h4 mb-0">Configurar Mi Agenda</h1>
            <a href="/profesional/dashboard" class="btn btn-outline-secondary btn-sm">Volver a Mi Agenda</a>
        </div>

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

        @if($errors->any())
            <div class="alert alert-danger">
                <strong>Se encontraron errores:</strong>
                <ul class="mb-0 mt-2">
                    @foreach($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif

        <form method="POST" action="/profesional/configurar-agenda" class="d-grid gap-3">
            @csrf

            <section class="panel-agenda">
                <h2 class="h6 mb-3">Duración de los turnos</h2>
                <div class="row g-3 align-items-end">
                    <div class="col-12 col-md-4 col-lg-3">
                        <label class="form-label" for="duracionturno">Duración por turno (minutos)</label>
                        <input id="duracionturno" name="duracionturno" type="number" class="form-control" min="10" max="180" step="5" value="{{ $duracionTurnoActual }}" required>
                    </div>
                    <div class="col-12 col-md-8 col-lg-9">
                        <p class="small text-muted mb-0">Esta duración se usará para generar la disponibilidad y calcular el horario de fin de cada turno.</p>
                    </div>
                </div>
            </section>

            <section class="panel-agenda">
                <div class="d-flex justify-content-between align-items-center flex-wrap gap-2 mb-3">
                    <h2 class="h6 mb-0">Dias de atenciones</h2>
                    <button type="button" id="btnAgregarDia" class="btn btn-sm btn-outline-primary">Agregar horario de atencion</button>
                </div>
                <p class="small text-muted mb-3">Hasta 10 horarios en total: maximo 5 AM y 5 PM.</p>

                <div class="table-responsive">
                    <table class="table table-sm tabla-config" id="tablaDiasAtencion">
                        <thead>
                            <tr>
                                <th style="min-width: 160px;">Dia</th>
                                <th style="min-width: 130px;">Inicio</th>
                                <th style="min-width: 130px;">Fin</th>
                                <th style="min-width: 110px;">Tipo</th>
                                <th style="width: 70px;"></th>
                            </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                </div>
            </section>

            <section class="panel-agenda">
                <div class="d-flex justify-content-between align-items-center flex-wrap gap-2 mb-3">
                    <h2 class="h6 mb-0">Receso</h2>
                    <button type="button" id="btnAgregarReceso" class="btn btn-sm btn-outline-primary">Agregar receso</button>
                </div>

                <div class="table-responsive">
                    <table class="table table-sm tabla-config" id="tablaRecesos">
                        <thead>
                            <tr>
                                <th style="min-width: 160px;">Dia</th>
                                <th style="min-width: 130px;">Inicio</th>
                                <th style="min-width: 130px;">Fin</th>
                                <th style="width: 70px;"></th>
                            </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                </div>
            </section>

            <section class="panel-agenda">
                <div class="d-flex justify-content-between align-items-center flex-wrap gap-2 mb-3">
                    <h2 class="h6 mb-0">Vacaciones</h2>
                    <button type="button" id="btnAgregarVacacion" class="btn btn-sm btn-outline-primary">Agregar vacaciones</button>
                </div>

                <div class="table-responsive">
                    <table class="table table-sm tabla-config" id="tablaVacaciones">
                        <thead>
                            <tr>
                                <th style="min-width: 150px;">Fecha inicio</th>
                                <th style="min-width: 150px;">Fecha fin</th>
                                <th>Descripcion</th>
                                <th style="width: 70px;"></th>
                            </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                </div>
            </section>

            <section class="panel-agenda">
                <div class="d-flex justify-content-between align-items-center flex-wrap gap-2 mb-3">
                    <h2 class="h6 mb-0">Feriados</h2>
                    <button type="button" id="btnAgregarFeriado" class="btn btn-sm btn-outline-primary">Agregar feriado</button>
                </div>

                <div class="table-responsive">
                    <table class="table table-sm tabla-config" id="tablaFeriados">
                        <thead>
                            <tr>
                                <th style="min-width: 160px;">Fecha</th>
                                <th>Descripcion</th>
                                <th style="width: 70px;"></th>
                            </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                </div>
            </section>

            <div class="d-flex justify-content-end gap-2">
                <a href="/profesional/dashboard" class="btn btn-outline-secondary">Cancelar</a>
                <button type="submit" class="btn btn-primary">Guardar configuracion</button>
            </div>
        </form>
    </main>

    <template id="tplDiaAtencion">
        <tr>
            <td>
                <select class="form-select form-select-sm" data-field="dia_id" required></select>
            </td>
            <td>
                <input type="time" class="form-control form-control-sm" data-field="horariocomienzo" required>
            </td>
            <td>
                <input type="time" class="form-control form-control-sm" data-field="horariofin" required>
            </td>
            <td>
                <select class="form-select form-select-sm" data-field="tipo" required>
                    <option value="AM">AM</option>
                    <option value="PM">PM</option>
                </select>
            </td>
            <td>
                <button type="button" class="btn btn-sm btn-outline-danger" data-remove="row">X</button>
            </td>
        </tr>
    </template>

    <template id="tplReceso">
        <tr>
            <td>
                <select class="form-select form-select-sm" data-field="dia_id" required></select>
            </td>
            <td>
                <input type="time" class="form-control form-control-sm" data-field="comienzo" required>
            </td>
            <td>
                <input type="time" class="form-control form-control-sm" data-field="fin" required>
            </td>
            <td>
                <button type="button" class="btn btn-sm btn-outline-danger" data-remove="row">X</button>
            </td>
        </tr>
    </template>

    <template id="tplVacacion">
        <tr>
            <td>
                <input type="date" class="form-control form-control-sm" data-field="inicio" required>
            </td>
            <td>
                <input type="date" class="form-control form-control-sm" data-field="fin" required>
            </td>
            <td>
                <input type="text" class="form-control form-control-sm" data-field="descripcion" maxlength="255" placeholder="Vacaciones">
            </td>
            <td>
                <button type="button" class="btn btn-sm btn-outline-danger" data-remove="row">X</button>
            </td>
        </tr>
    </template>

    <template id="tplFeriado">
        <tr>
            <td>
                <input type="date" class="form-control form-control-sm" data-field="fecha" required>
            </td>
            <td>
                <input type="text" class="form-control form-control-sm" data-field="descripcion" maxlength="255" placeholder="Dia no laborable">
            </td>
            <td>
                <button type="button" class="btn btn-sm btn-outline-danger" data-remove="row">X</button>
            </td>
        </tr>
    </template>

    <script>
        document.addEventListener('DOMContentLoaded', function () {
            const diasCatalogo = @json($diasCatalogo ?? []);
            const datosIniciales = {
                dias: @json($filasAtencion ?? []),
                recesos: @json($filasReceso ?? []),
                vacaciones: @json($filasVacaciones ?? []),
                feriados: @json($filasFeriados ?? []),
            };

            const config = {
                dias: { tbodyId: 'tablaDiasAtencion', tplId: 'tplDiaAtencion', key: 'dias' },
                recesos: { tbodyId: 'tablaRecesos', tplId: 'tplReceso', key: 'recesos' },
                vacaciones: { tbodyId: 'tablaVacaciones', tplId: 'tplVacacion', key: 'vacaciones' },
                feriados: { tbodyId: 'tablaFeriados', tplId: 'tplFeriado', key: 'feriados' },
            };

            const getBody = (tbodyId) => document.querySelector('#' + tbodyId + ' tbody');

            const rellenarSelectDias = (selectEl) => {
                if (!selectEl) {
                    return;
                }

                selectEl.innerHTML = '<option value="">Seleccionar</option>';
                diasCatalogo.forEach((dia) => {
                    const option = document.createElement('option');
                    option.value = String(dia.id);
                    option.textContent = dia.descripcion;
                    selectEl.appendChild(option);
                });
            };

            const renumerar = (key) => {
                const body = getBody(config[key].tbodyId);
                if (!body) {
                    return;
                }

                body.querySelectorAll('tr').forEach((tr, index) => {
                    tr.querySelectorAll('[data-field]').forEach((input) => {
                        const campo = input.getAttribute('data-field');
                        input.name = key + '[' + index + '][' + campo + ']';
                    });
                });
            };

            const agregarFila = (key, valores = {}) => {
                if (key === 'dias') {
                    const bodyDias = getBody(config.dias.tbodyId);
                    if (bodyDias && bodyDias.querySelectorAll('tr').length >= 10) {
                        alert('Solo puedes cargar hasta 10 horarios de atencion.');
                        return;
                    }
                }

                const body = getBody(config[key].tbodyId);
                const tpl = document.getElementById(config[key].tplId);

                if (!body || !tpl) {
                    return;
                }

                const fragment = tpl.content.cloneNode(true);
                const row = fragment.querySelector('tr');
                const selectDia = row.querySelector('select[data-field="dia_id"]');

                if (selectDia) {
                    rellenarSelectDias(selectDia);
                }

                row.querySelectorAll('[data-field]').forEach((input) => {
                    const field = input.getAttribute('data-field');
                    const valor = valores[field] ?? '';
                    input.value = valor;
                });

                const removeBtn = row.querySelector('[data-remove="row"]');
                if (removeBtn) {
                    removeBtn.addEventListener('click', () => {
                        row.remove();
                        renumerar(key);
                    });
                }

                body.appendChild(row);
                renumerar(key);
            };

            document.getElementById('btnAgregarDia')?.addEventListener('click', () => agregarFila('dias'));
            document.getElementById('btnAgregarReceso')?.addEventListener('click', () => agregarFila('recesos'));
            document.getElementById('btnAgregarVacacion')?.addEventListener('click', () => agregarFila('vacaciones'));
            document.getElementById('btnAgregarFeriado')?.addEventListener('click', () => agregarFila('feriados'));

            (datosIniciales.dias || []).forEach((fila) => agregarFila('dias', fila));
            (datosIniciales.recesos || []).forEach((fila) => agregarFila('recesos', fila));
            (datosIniciales.vacaciones || []).forEach((fila) => agregarFila('vacaciones', fila));
            (datosIniciales.feriados || []).forEach((fila) => agregarFila('feriados', fila));

            if ((datosIniciales.dias || []).length === 0) {
                agregarFila('dias');
            }
        });
    </script>

    @include('partials.reportar-falla-boton')
</body>
</html>


