<?php
require_once "../../functions.php";
if (comprobar_override(str_replace($_SERVER['DOCUMENT_ROOT'], '', __FILE__))) exit;
$pag = basename($_SERVER['PHP_SELF']);
comprobar_postget('post', ['id'], $pag, false, '');
gen_plantilla(true, $pag, false, false, '', false, false);

$id = filter_input(INPUT_POST, "id", FILTER_SANITIZE_NUMBER_INT);
$aprobar = filter_input(INPUT_POST, "aprobado", FILTER_SANITIZE_NUMBER_INT);

$aprobacion = consulta('SELECT amr.*, r.fecha AS r_fecha FROM aprobaciones_modificaciones_registros AS amr
LEFT JOIN registros AS r ON r.id=amr.id_registro WHERE amr.id=?', [$id], true);
//Calcular cuantas entradas/salidas hay para no imprimir toda la tabla
$nivel = 1;
for ($i = 0; $i < count($aprobacion); $i++) {
    if ($nivel != 2 && !empty($aprobacion[$i]['h3_original'])) $nivel = 2;
    if (!empty($aprobacion[$i]['h5_original'])) {
        $nivel = 3;
        break;
    }
}
$i = 0;

?>
<dialog id='dialog' class="cristal" open>
    <h2>¿Estás seguro/a que deseas <span class="<?= (!$aprobar) ? 'rojo' : 'verde' ?>"><?= (!$aprobar) ? 'declinar' : 'aprobar' ?></span> la <?= match($aprobacion[0]['tipo']){'eliminar'=>'eliminación', 'crear'=>'creación','modificar'=>'modificación'}; ?>?</h2>
    <table>
        <?php imprEncabezadoTablaAprobaciones($nivel,1); ?>
        <tr>
            <td><?= date("d/m/Y", strtotime($aprobacion[$i]["r_fecha"])) ?></td>
            <?php
            for ($o = 1; $o <= 6; $o++) {
                if ($o == 3 && $nivel == 1) break;
                if ($o == 5 && $nivel == 2) break;
                if (!empty($aprobacion[$i]["h" . $o . "_original"])) {
                    echo "<td>";
                    $hora = DateTime::createFromFormat('H:i:s', $aprobacion[$i]["h" . $o . "_original"]);
                    $hora_mod = DateTime::createFromFormat('H:i:s', $aprobacion[$i]["h" . $o . "_modificada"]);
                    if ($hora && $hora_mod && $hora != $hora_mod)
                        echo "<span class='flex row gap02'>" .
                            '<b class="rojo">' . $hora->format('H:i') . '</b>' .
                            '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 640">
                        <defs>
                            <linearGradient id="fill-grad" x1="0" y1="0" x2="640" y2="0" gradientUnits="userSpaceOnUse">
                                <stop offset="0%" stop-color="var(--rojo)"/>
                                <stop offset="60%" stop-color="var(--azul)"/>
                                <stop offset="80%" stop-color="var(--verde)"/>
                            </linearGradient>
                        </defs>
                        <path d="M598.6 297.4C611.1 309.9 611.1 330.2 598.6 342.7L470.6 470.7C461.4 479.9 447.7 482.6 435.7 477.6C423.7 472.6 416 460.9 416 448L416 384L80 384C53.5 384 32 362.5 32 336L32 304C32 277.5 53.5 256 80 256L416 256L416 192C416 179.1 423.8 167.4 435.8 162.4C447.8 157.4 461.5 160.2 470.7 169.3L598.7 297.3z"
                                fill="url(#fill-grad)"/>
                    </svg>' .
                            '<b class="verde">' . $hora_mod->format('H:i') . '</b>' .
                            '</span>';
                    elseif($hora) echo $hora->format('H:i');
                    else echo $aprobacion[$i]["h" . $o . "_original"];
                    echo "</td>";
                } else echo "<td>-</td>";
                
            }
            echo "<td>".$aprobacion[$i]["tipo"]."</td>"; ?>
    </table>

    <section class='flex row gap5 pa1'>
        <button id='cancelar' class="btn-t" title='Cancelar'>Cancelar</button>
        <button class='btn-p' onclick="aprobar_modificacion(<?= $id ?>,<?= $aprobar ?>)"><?= (!$aprobar) ? 'Declinar' : 'Aprobar' ?></button>
    </section>

</dialog>