<?= $this->extend('templates/baseAdmin') ?>

<?= $this->section('content') ?>

<link rel="stylesheet" href="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.min.css">

<h1><?= $tituloEncuesta ?></h1>

<div class="card">
    <div class="card-body">

        <table id="table-respuestas" class="table table-bordered table-striped">
            <thead>
                <th>ID Prestador</th>
                <th>Nombre</th>
                <th>Fecha</th>
                <?php foreach ($preguntas as $pregunta) : ?>
                    <th><?= $pregunta['question'] ?></th>
                <?php endforeach ?>
            </thead>
            <tbody>

                <?php foreach ($respuestas as $user) : ?>
                    <tr>
                        <td><?= $user[0][0]['idprestador'] ?></td>
                        <td><?= $user[0][0]['nombre'] . ' ' . $user[0][0]['apaterno'] . ' ' . $user[0][0]['amaterno'] ?></td>
                        <td><?= $user[0][0]['created_at'] ?></td>
                        <?php foreach ($user as $respuesta) : ?>
                            <!-- Respuestas de opción múltiple -->
                            <?php if ($respuesta[0]['type'] === '3') : ?>
                                <td>
                                    <?php foreach ($respuesta as $opcion) : ?>
                                        <?= $opcion['answer'] ?>
                                        <br>
                                    <?php endforeach ?>
                                </td>
                            <?php else : ?>
                                <td><?= $respuesta[0]['answer'] ?></td>
                            <?php endif ?>
                        <?php endforeach ?>
                    </tr>
                <?php endforeach ?>

            </tbody>
        </table>

    </div>
</div>

<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://cdn.datatables.net/1.12.1/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/buttons/2.2.3/js/dataTables.buttons.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js"></script>
<script src="https://cdn.datatables.net/buttons/2.2.3/js/buttons.html5.min.js"></script>
<script src="https://cdn.datatables.net/buttons/2.2.3/js/buttons.print.min.js"></script>

<script>
    $(document).ready(function() {
        $('#table-respuestas').DataTable({
            dom: 'Bfrtip',
            buttons: [
                'copy', 'csv', 'excel', 'pdf', 'print'
            ]
        });
    });
    // document.addEventListener('DOMContentLoaded', function() {
    //     let table = new DataTable('#table-respuestas', {
    //         'buttons': ['excel'],
    //         'searching': true,
    //         'ordering': true,
    //         'language': {
    //             "decimal": "",
    //             "emptyTable": "No hay información",
    //             "info": "Mostrando _START_ a _END_ de _TOTAL_ registros",
    //             "infoEmpty": "Mostrando 0 to 0 of 0 registros",
    //             "infoFiltered": "(Filtrado de _MAX_ total registros)",
    //             "infoPostFix": "",
    //             "thousands": ",",
    //             "lengthMenu": "Mostrar _MENU_ registros",
    //             "loadingRecords": "Cargando...",
    //             "processing": "Procesando...",
    //             "search": "Buscar:",
    //             "zeroRecords": "No se encontraron resultados",
    //             "paginate": {
    //                 "first": "Primero",
    //                 "last": "Ultimo",
    //                 "next": "Siguiente",
    //                 "previous": "Anterior"
    //             }
    //         },
    //         'order': [],
    //     });
    // });
</script>

<?= $this->endSection() ?>