<?php

require_once("../helper.php");
if(!isset($_GET["TO"])){
    $_GET["TO"] = 12;
}
$_GET["TO"] = intval($_GET["TO"]);
if(!isset($_GET["FROM"])){
    $_GET["FROM"] = -24;
}
$_GET["FROM"] = intval($_GET["FROM"]);

$Query = "SELECT
UNIX_TIMESTAMP(CONCAT(date,' ',sunrise)) AS sunrise, 
UNIX_TIMESTAMP(CONCAT(date,' ',sunset)) AS sunset
FROM solarLog.daylight
WHERE date BETWEEN DATE_ADD(NOW(),INTERVAL ".($_GET["FROM"]-24)." HOUR) and DATE_ADD(NOW(),INTERVAL ".$_GET["TO"]." HOUR)
ORDER BY date";

$linecolors["Solarleistung"] = "#FFFF00";
$linecolors["UG"] = "#FFaa00";
$linecolors["OG"] = "#FF4400";
$linecolors["Auto UG"] = "#00aaFF";
$linecolors["Auto OG"] = "#0044FF";
$linecolors["Heizstab"] = "#FF0000";
$linecolors["Batterieladung"] = "#00aa00";
$linecolors["Einspeisung"] = "#b0b0b0";
$linecolors["Ladestand"] = "#00aa00";
$linecolors["Vorhersage"] = "#2222FF";



if (checkLogin()) {
    $mysql = new mysqli($mysql_server, $mysql_solarUser, $mysql_solarPass, $mysql_solarDB);
    $result = mysqli_query($mysql, $Query);
    if (!$result) {
        echo "Error:<br>" . mysqli_error($mysql) . "<br />";
    }
    //$obj[] = (object)[]; // Cast empty array to object

    while ($row = $result->fetch_assoc()) {
        $ii = 1;
        $anno = (object)[];
            if($row["sunrise"] < (time()+$_GET["FROM"]*60*60)){
                $anno->xMin = (time()+$_GET["FROM"]*60*60)*1000;
            }else{
                $anno->xMin = $row["sunrise"]*1000;
            }
            if($row["sunset"] > (time()+$_GET["TO"]*60*60)){
                $anno->xMax = round(time()+$_GET["TO"]*60*60)*1000;
            }else{
                $anno->xMax = $row["sunset"]*1000;
            }
            $anno->borderWidth = 0;
            if($row["sunset"] > (time()+$_GET["FROM"]*60*60)){
                $obj[] = clone $anno;
            }
            
    }
}


//header('Content-Type: application/json');
echo json_encode($obj);
//echo '{"labels":[1761322682000,1761322782000,1761322882000,1761322982000,1761323082000,1761323182000,1761323282000],"datasets":[{"stack": "Stack 0","cubicInterpolationMode":"monotone","fill":"origin","label":"Acquisitions by year","data":[10,20,50,20,10,5,70]},{"fill": "false","stack": "Stack 1","cubicInterpolationMode": "monotone","label": "Acquisitions by year","data": [10,20,50,20,10,5,70]}]}';
