<?php
	include_once('includes/db_connect.php');
	include_once('includes/global_functions.php');
    $radioid = $_GET['radioid'];
    $radiomac = $_GET['radiomac'];
    $tz = $_GET['tz'];
    

    if($radioid == "" && $radiomac == ""){
        print(json_encode([error => "Not a valid radioid"]));
        die();
    }else{
        if($radiomac != ""){
            $radiomac = str_replace(':', '', $radiomac);
            $radioid = substr($radiomac, 6 , 11);
        }
        
        $array = array();
       	if($stmt = $mysqli -> prepare("SELECT id, message, message, streamip, streamurl, streamport, tijd, snoozetijd, onlyOnce FROM alarm LEFT JOIN users ON alarm.uid = users.uid WHERE users.radiotoken = ? LIMIT 5")){
        	$stmt -> bind_param("s", $radioid);
        	$stmt -> execute();
        	$stmt -> bind_result($id, $message, $message, $streamip, $streamurl, $streamport, $tijdstr, $snoozetijd, $onlyOnce);
        	while($result = $stmt -> fetch()){
        	    $tijd = strtotime($tijdstr);
        	    
        	    $timezone_name = timezone_name_from_abbr('', $tz * 3600, true);
        	    $datTimeZoneuser = new DateTimeZone($timezone_name);
        	    $time = new DateTime('now', $datTimeZoneuser );
        	    
        	    if($tijd < strtotime($time->format("Y-m-d H:i:s   "))){
        	       $tijd = strtotime($tijdstr.' +1 day');
        	    }
        	    array_push($array, array(   YYYY=> date("Y", $tijd), 
        	                                MM => date("m", $tijd), 
        	                                DD => date("d", $tijd), 
        	                                hh => date("H", $tijd), 
        	                                mm => date("i", $tijd), 
        	                                ss => date("s", $tijd),
                                            name => $message,
        	                                ip => $streamip,
        	                                id => $id,
                                            port => $streamport,
                                            url => $streamurl,
                                            oo => $onlyOnce,
                                            st => $snoozetijd
        	                               ) 
        	               );
        	 };
        	$stmt -> close();
        }
        print(json_encode($array, JSON_UNESCAPED_SLASHES));
    }
    
    