<?php

require_once("../helper.php");

$consQuery = "SELECT
  DATE_FORMAT(datetime,'%b. %y') AS 'time',
  SUM(pvP/12 - IF(gridP < 0, -gridP/12,0) - IF(battP < 0, -battP/12,0) - heaterPwr/12) Direktverbrauch,
  SUM(heaterPwr/12) AS Heizstab,
  SUM(IF(battP < 0, -battP/12,0)) AS Batterieladung,
  SUM(gridPfeed/12) AS Einspeisung
FROM EnergyFlow
  WHERE DATE_FORMAT(datetime,'%Y%m') >= DATE_FORMAT(DATE_SUB(now(),INTERVAL 13 MONTH),'%Y%m')
  GROUP BY DATE_FORMAT(datetime, '%b. %y')
ORDER BY datetime ; ";

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



if (checkLogin()) {
    $mysql = new mysqli($mysql_server, $mysql_solarUser, $mysql_solarPass, $mysql_solarDB);
    $result = mysqli_query($mysql, $consQuery);
    if(!$result){
        echo "Error:<br>".mysqli_error($mysql)."<br />";
    }
    $obj = (object)[]; // Cast empty array to object
    $obj->labels = [];
    $obj->datasets = [];
    $i = 0;
    $filled = 0;
    if ($result->num_rows > 1) {
        $ii = 0;
        $row = $result->fetch_assoc();
        foreach ($row as $name => $value) {
            $dataset = (object)[];
            if ($name != "time") {
                $dataset->borderColor = $linecolors[$name];
                $dataset->backgroundColor = $linecolors[$name]."55";
                $dataset->borderWidth=1;
                $dataset->pointRadius= 0;
                $dataset->pointHoverRadius= 5;
                $dataset->tension=0.2;
                if ($name == "Solarleistung") {
                    $dataset->stack = "SolarPwr";
                    $dataset->fill = "none";
                    $dataset->yAxisID = 'y';
                } else if ($name == "Ladestand") {
                    $dataset->stack = "Charge";
                    $dataset->fill = "none";
                    $dataset->yAxisID = 'y1';
                } else {
                    $dataset->stack = "Consumers";
                    if ($filled == 0) {
                        $filled = 1;
                        $dataset->fill = "origin";
                    } else {
                        $dataset->fill = "-1";
                    }
                    $dataset->yAxisID = 'y';
                }
                $dataset->label = $name;

                $dataset->data[] = $value;
                $obj->datasets[] = clone $dataset;
                $ii++;
            } else {
                $obj->labels[] = $value;
            }
        }
        while ($row = $result->fetch_assoc()) {
            $ii = 0;
            foreach ($row as $name => $value) {
                if ($name != "time") {
                    $obj->datasets[$ii]->data[] = $value;
                    $ii++;
                } else {
                    $obj->labels[] = $value;
                }
            }
        }
    }
  

}


//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]}]}';
