<?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, true, "/views/tables/fichajes");
gen_plantilla(true, $pag, false, false, '', false, false);

/*******************************************************************************
 *                                [Parámetros]                                  *
 *******************************************************************************/

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

$hora1 = filter_input(INPUT_POST, "hora1", FILTER_SANITIZE_SPECIAL_CHARS);
$hora1 = ($hora1 == "" || $hora1 == "00:00") ? null : $hora1;
$hora2 = filter_input(INPUT_POST, "hora2", FILTER_SANITIZE_SPECIAL_CHARS);
$hora2 = ($hora2 == "" || $hora2 == "00:00") ? null : $hora2;
$hora3 = filter_input(INPUT_POST, "hora3", FILTER_SANITIZE_SPECIAL_CHARS);
$hora3 = ($hora3 == "" || $hora3 == "00:00") ? null : $hora3;
$hora4 = filter_input(INPUT_POST, "hora4", FILTER_SANITIZE_SPECIAL_CHARS);
$hora4 = ($hora4 == "" || $hora4 == "00:00") ? null : $hora4;
$hora5 = filter_input(INPUT_POST, "hora5", FILTER_SANITIZE_SPECIAL_CHARS);
$hora5 = ($hora5 == "" || $hora5 == "00:00") ? null : $hora5;
$hora6 = filter_input(INPUT_POST, "hora6", FILTER_SANITIZE_SPECIAL_CHARS);
$hora6 = ($hora6 == "" || $hora6 == "00:00") ? null : $hora6;

if (isset($_POST['id_incidencia']))
    $id_incidencia =  filter_input(INPUT_POST, "id_incidencia", FILTER_SANITIZE_NUMBER_INT);


/*******************************************************************************
 *                                 [Consulta]                                  *
 *******************************************************************************/
$consulta = "UPDATE registros SET `hora1`=?,`hora2`=?,`hora3`=?,`hora4`=?,`hora5`=?,`hora6`=? WHERE id=?;";
if (!consulta($consulta, [$hora1, $hora2, $hora3, $hora4, $hora5, $hora6, $id], false)) {
    crear_log(
        "error",
        "Error en la consulta",
        $pag,
        "No se ha podido actualizar la ficha, porque ha ocurrido un error en la consulta."
    );
    if (!isset($_POST['id_incidencia']))
        header("Location: " . SITIO_CARPETA . "/views/forms/modificar_registro?id=$id&tipo=error&mensaje=No se ha podido actualizar, ha ocurrido un error en la consulta");
    else
        header("Location: " . SITIO_CARPETA . "/views/tables/incidencias?tipo=error&mensaje=Error al actualizar el registro");
} else {
    crear_log(
        "info",
        "Registro actualizada",
        $pag,
        "Se ha el registro con id <b>[$id]</b>."
    );
    if (!isset($_POST['id_incidencia'])) {
        header("Location: " . SITIO_CARPETA . "/views/tables/fichajes?id=$id&tipo=correcto&mensaje=Registro actualizado correctamente");
    } else {
        //Corregir incidencia
        consulta('UPDATE `incidencias` SET `id_motivo`=1,`estado`=0 WHERE id=?;', [$id_incidencia], false);
        header("Location: " . SITIO_CARPETA . "/views/tables/incidencias?tipo=correcto&mensaje=Registro actualizado e incidencia corregida");
    }
    
}
