<?php
$ruta = "../";
require_once "../class/c_login.php";

// check if the session is started
if (!isset($_SESSION['user']))
    die(json_encode(['error' => 'No se ha iniciado sesión']));

$user = unserialize($_SESSION['user']);
$ruta = "../";
require_once "../include/bd_pdo.php";
global $pdo;
if ($_POST['periodo'] == 0) {
    $periodo = null;
} else
    $periodo = $_POST['periodo'];
$sql = "SELECT * FROM fs_diasfestivos(null, :dia) WHERE diasfestivos_id != :id";
$params = [':dia' => $_POST['diaFestivo'], ':id' => $_POST['id']];
$dia_general = query($sql, $params, false);
$sql = "SELECT * FROM fs_diasfestivos(null, null, :periodo, :dia) WHERE diasfestivos_id != :id";
$params = [':periodo' => $periodo, ':dia' => $_POST['diaFestivo'], ':id' => $_POST['id']];
$dia = query($sql, $params, false);
if (!$dia && !$dia_general) { //no hay repetidos
    $sql = "SELECT fu_update_diasfestivos(:id, :dia, :periodo)";
    query($sql, $params, false);
    header("Location: ../días_festivos.php");
    exit();
} else { //es repetido
    header("Location: ../días_festivos.php?error=1");
    exit();
}
