<?php
//Vendor Autoloader
include(rtrim($_SERVER['DOCUMENT_ROOT'], '/') . '/vendor/autoload.php');
include(rtrim($_SERVER['DOCUMENT_ROOT'], '/') . '/mysyde/DcAutoloader.php');

$dcAutoloader = new DcAutoloader();
$dcAutoloader->register();

require_once("../../mysyde/common/common_functions.inc.php");
$envDir = rtrim(dirname(dirname(__DIR__)), '/') . '/config';
if (is_dir($envDir)) {
    $dotenv = new \Dotenv\Dotenv($envDir);
    $dotenv->load();
}

require_once((local_environment()) ? "../../mysyde/dc.config.php" : "../../mysyde/dc-server.config.php");
db_connect();

function updateReload(){
    $update = $GLOBALS['pdo_conn']->prepare("UPDATE main_infopoint_screens SET block_reload_active = :block_reload_active WHERE hash = :hash");
    $update->execute(array(':block_reload_active' => '0', ':hash' => $_POST['hash']));
}

$select_reload = $GLOBALS['pdo_conn']->prepare("SELECT * FROM main_infopoint_screens WHERE hash = :hash");
$select_reload->execute(array(':hash' => $_POST['hash']));
$res_select = $select_reload->fetch(PDO::FETCH_ASSOC);

$reload_time = $res_select['block_reload_time'];
$block_active = $res_select['block_reload_active'];
$reload = $res_select['reload'];

$current_date = new DateTime();

$time = 2;

$return = 0;
$fadeTime = new DateTime('');
$fadeTime->modify('+'.$time.' second');
if($fadeTime->format('d.m.Y H:i:s') >= $reload_time){
    $return = 2;
}

if($block_active == 1){
    if($current_date->format('d.m.Y H:i:s') >= $reload_time){
        updateReload();
        $return = 1;
    }
}else{
    if($reload == 1){
        $return = 1;
    }
}

echo $return;