<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
    if (isset($_GET['type'])&&isset($_GET['key'])) {
        if ($_GET['key'] === 'brt6k11t') {
            require('../connect.php');

            $conn = new mysqli($host, $user, $pass, $database);

            if($_GET['type']=="year"){
                $format = "%Y";
                $where = "";
                // $sql = "SELECT DATE_FORMAT(date, '%Y') AS year, SUM(`total_energy`) AS 'total',SUM(`grid_energy`) AS 'grid',SUM(`pv_energy`) AS 'pv',SUM(`battery_energy`) AS 'battery' FROM energy GROUP BY DATE_FORMAT(date, '%Y') ORDER BY year";
            }elseif ($_GET['type']=="month") {
                $format = "%Y-%m";
                $where = "WHERE `date` > \"2023-11-01\"";
                // $sql = "SELECT DATE_FORMAT(date, '%Y-%m') AS month, SUM(`total_energy`) AS 'total',SUM(`grid_energy`) AS 'grid',SUM(`pv_energy`) AS 'pv',SUM(`battery_energy`) AS 'battery' FROM energy GROUP BY DATE_FORMAT(date, '%Y-%m') ORDER BY month";
            }elseif ($_GET['type']=="day") {
                $format = "%Y-%m-%d";
                $where = "WHERE `date` > \"2023-11-01\"";
                // $sql = "SELECT DATE_FORMAT(date, '%Y-%m-%d') AS day, SUM(`total_energy`) AS 'total',SUM(`grid_energy`) AS 'grid',SUM(`pv_energy`) AS 'pv',SUM(`battery_energy`) AS 'battery' FROM energy GROUP BY DATE_FORMAT(date, '%Y-%m-%d') ORDER BY day";
            }
            $sql = "SELECT DATE_FORMAT(date, '$format') AS {$_GET['type']}, SUM(`total_energy`) AS 'total',SUM(`grid_energy`) AS 'grid',SUM(`pv_energy`) AS 'pv',SUM(`battery_energy`) AS 'battery' FROM energy $where GROUP BY DATE_FORMAT(date, '$format') ORDER BY {$_GET['type']}";
            // echo $sql;

            $result = mysqli_query($conn, $sql);

            $data = array(); // Tablica na dane
            $labels = array(); // Tablica na unikalne etykiety dat
            // $string = "total_energy','grid_energy','pv'";
            // $string = str_replace(',', ' ', $string);
            // $array = explode(' ', $string);
            $array = array(
                0 => "total",
                1 => "grid",
                2 => "pv"
            );
            
            

            if (mysqli_num_rows($result) > 0) {
                // Odczytaj unikalne daty z wyników zapytania
                while ($row = mysqli_fetch_assoc($result)) {
                    $date = $row[$_GET['type']];
                                
                    // Sprawdzenie, czy godzina nie istnieje jeszcze w tablicy $labels
                    if (!in_array($date, $labels)) {
                        $labels[] = $date; // Dodaj godzinę do tablicy $labels
                    }
                }

                foreach ($array as $element) {
                    $values = array(); // Tablica na wartości

                    // Przewiń kursor wyników do początku dla każdego elementu
                    mysqli_data_seek($result, 0);

                    while ($row = mysqli_fetch_assoc($result)) {
                            // if (($row['r100'] !== '') || ($row['r201'] && $row['r202'] && $row['r203'] && $row['r204'] && $row['r205'] !== '-1')) {
                            // }
                        $values[] = $row[$element]; // Dodawanie wartości do tablicy
                    }

                    $data[$element] = array(
                        'values' => $values // Utworzenie tablicy danych z wartościami
                    );
                }

                $response = array(
                    'success' => 'yes',
                    'labels' => $labels, // Dodanie tablicy z unikalnymi etykietami do odpowiedzi
                    'data' => $data // Dodanie tablicy danych do odpowiedzi
                );
            } else {
                $response = array(
                    'success' => 'no',
                    'message' => 'Brak danych w wybranym dniu'
                );
            }

            $conn->close();

            header('Content-Type: application/json');
            echo json_encode($response);
	    }
    }
?>
