<?php
require_once("../../functions.php");
$pag = basename($_SERVER['PHP_SELF']);
gen_plantilla(true, $pag, false, false, '', false, false);
if (session_status() !== PHP_SESSION_ACTIVE) session_start();
/*******************************************************************************
 *                    [Imprimir campos de la Base de Datos]                    *
 *******************************************************************************/
if (isset($_POST['nombre'])) {
    $res_array = consulta("SELECT * FROM grupos WHERE nombre LIKE ? ORDER BY nombre DESC;", ['%' . filter_input(INPUT_POST, 'nombre', FILTER_SANITIZE_SPECIAL_CHARS) . '%'], true);
} else {
    $res_array = consulta("SELECT * FROM grupos ORDER BY nombre DESC;", [], true);
}
if (empty($res_array)) no_hay_nada();

for ($i = 0; $i < count($res_array); $i++) : ?>
    <details>
        <summary>
            <section class="flex row gap1">
                <span style='display: flex; justify-content: flex-start; gap: .8rem;<?= ($res_array[$i]['id'] == 1) ? 'margin-top:.2rem;padding-bottom:.4rem' : '' ?>'>
                    <span class="titulo-summary"><?= $res_array[$i]['nombre'] ?></span>
                </span>
                <?php if ($res_array[$i]['id'] !== 1 && ($_SESSION['tipo_usuario'] == 'admin')): ?>
                    <section class='modificadoresficha'>
                        <a href='<?= SITIO_CARPETA ?>/views/forms/grupos?id=<?= $res_array[$i]['id'] ?>' title='Modificar a <?= $res_array[$i]['nombre'] ?>' class="flex supercentrado azul btn-circle-glass" style="width: 1.8rem; height:1.8rem; background-color: transparent;">
                            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="1em" height="1em" class="modificar">
                                <path d="M471.6 21.7c-21.9-21.9-57.3-21.9-79.2 0L362.3 51.7l97.9 97.9 30.1-30.1c21.9-21.9 21.9-57.3 0-79.2L471.6 21.7zm-299.2 220c-6.1 6.1-10.8 13.6-13.5 21.9l-29.6 88.8c-2.9 8.6-.6 18.1 5.8 24.6s15.9 8.7 24.6 5.8l88.8-29.6c8.2-2.7 15.7-7.4 21.9-13.5L437.7 172.3 339.7 74.3 172.4 241.7zM96 64C43 64 0 107 0 160V416c0 53 43 96 96 96H352c53 0 96-43 96-96V320c0-17.7-14.3-32-32-32s-32 14.3-32 32v96c0 17.7-14.3 32-32 32H96c-17.7 0-32-14.3-32-32V160c0-17.7 14.3-32 32-32h96c17.7 0 32-14.3 32-32s-14.3-32-32-32H96z" />
                            </svg>
                        </a>
                        <button title='Eliminar grupo <?= $res_array[$i]['nombre'] ?>' class="flex supercentrado btn-circle-glass rojo" style="width: 2rem; height:2rem" onclick="cargar_eliminar_grupo('<?= $res_array[$i]['id'] ?>')">
                            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 512" width="1em" height="1em" class="eliminar">
                                <path d="M38.8 5.1C28.4-3.1 13.3-1.2 5.1 9.2S-1.2 34.7 9.2 42.9l592 464c10.4 8.2 25.5 6.3 33.7-4.1s6.3-25.5-4.1-33.7L440.6 320H618.7c11.8 0 21.3-9.6 21.3-21.3C640 239.8 592.2 192 533.3 192H490.7c-15.9 0-31 3.5-44.6 9.7c1.3 7.2 1.9 14.7 1.9 22.3c0 30.2-10.5 58-28 79.9l-25.2-19.7C408.1 267.7 416 246.8 416 224c0-53-43-96-96-96c-31.1 0-58.7 14.8-76.3 37.7l-40.6-31.8c13-14.2 20.9-33.1 20.9-53.9c0-44.2-35.8-80-80-80C116.3 0 91.9 14.1 77.5 35.5L38.8 5.1zM106.7 192C47.8 192 0 239.8 0 298.7C0 310.4 9.6 320 21.3 320H234.7c.2 0 .4 0 .7 0c-20.6-18.2-35.2-42.8-40.8-70.8L121.8 192H106.7zM261.3 352C187.7 352 128 411.7 128 485.3c0 14.7 11.9 26.7 26.7 26.7H485.3c10.5 0 19.5-6 23.9-14.8L324.9 352H261.3zM512 160A80 80 0 1 0 512 0a80 80 0 1 0 0 160z" />
                            </svg>
                        </button>
                    </section>
                <?php endif; ?>
            </section>
        </summary>
        <section class="flex column gap1 alineado">
            <?= ($res_array[$i]['id'] == 1) ? '<b class="rojo">No se puede modificar ni eliminar el grupo "Por defecto"</b>' : '' ?>
            <?php
            //FIJO
            if ($res_array[$i]['tipo_grupo'] == 0):
                echo '<span class="cristal">FIJO</span>';
                impr_tabla_mostrar_horas($res_array, $i);
                echo '</section>';
            //FLEXIBLE DIARIO
            elseif ($res_array[$i]['tipo_grupo'] == 1 && $res_array[$i]['tipo_flexible'] == 1): ?>
                <span class="cristal">FLEXIBLE DIARIO</span>
                <section class="flex row wrap supercentrado">
                    <?php
                    $dias = ['lunes', 'martes', 'miercoles', 'jueves', 'viernes', 'sabado', 'domingo'];
                    $nombres = [
                        'lunes' => 'Lunes',
                        'martes' => 'Martes',
                        'miercoles' => 'Miércoles',
                        'jueves' => 'Jueves',
                        'viernes' => 'Viernes',
                        'sabado' => 'Sábado',
                        'domingo' => 'Domingo'
                    ];
                    $n_horas_dias = [
                        'lunes' => 'lunh',
                        'martes' => 'marh',
                        'miercoles' => 'mieh',
                        'jueves' => 'jueh',
                        'viernes' => 'vieh',
                        'sabado' => 'sabh',
                        'domingo' => 'domh'
                    ];

                    foreach ($dias as $dia) :
                        $checked = (isset($res_array[$i][$dia]) && $res_array[$i][$dia]) ? "checked" : "";
                    ?>
                        <label class="chk chk1 cristal" id="<?= $dia ?>-horas" style="flex-direction: column !important;">
                            <section class="flex row justificado gap1">
                                <input type="checkbox" name="<?= $dia ?>" id="<?= $dia ?>" <?= $checked ?> disabled>
                                <svg viewBox="0 0 64 64" height="2em" width="2em">
                                    <path d="M 0 16 V 56 A 8 8 90 0 0 8 64 H 56 A 8 8 90 0 0 64 56 V 8 A 8 8 90 0 0 56 0 H 8 A 8 8 90 0 0 0 8 V 16 L 32 48 L 64 16 V 8 A 8 8 90 0 0 56 0 H 8 A 8 8 90 0 0 0 8 V 56 A 8 8 90 0 0 8 64 H 56 A 8 8 90 0 0 64 56 V 16" pathLength="575.0541381835938" class="path"></path>
                                </svg>
                                <?= $nombres[$dia] ?>
                            </section>
                            <section class="flex row chk-horas" style="display: flex;">
                                <input type="text" min="0" class="horasinput" id="horas-<?= $dia ?>" name="horas-<?= $dia ?>" value="<?= $res_array[$i][$n_horas_dias[$dia]] ?>" disabled>
                            </section>
                        </label>
                    <?php
                    endforeach;
                    echo "</section>";
                //FLEXIBLE SEMANAL
                elseif ($res_array[$i]['tipo_grupo'] == 1 && $res_array[$i]['tipo_flexible'] == 2): ?>
                    <span class="cristal">FLEXIBLE SEMANAL</span>
                    <section class="flex row supercentrado">
                        <input type="text" min="0" class="horasinput" id="horas-semanal" name="horas-semanal"
                            value="<?= (isset($res_array[$i]['horas']) && $res_array[$i]['horas'] !== '') ? $res_array[$i]['horas'] : '' ?>">
                    </section>
            <?php endif;
                echo "</details>";
            endfor;
?>

<style>
details {
    width: 43rem;
    max-width: 100%;
    margin-left: auto;
    margin-right: auto;
}
@media (max-width: 1024px) {
    details {
        width: 90%;
    }
}
@media (max-width: 768px) {
    details {
        width: 100%;
    }
}
</style>
