<?php

require_once "Carbon/Carbon.php";
use Carbon\Carbon;
include 'globals.php';
$start_date_param = htmlspecialchars($_GET["start_date"]);
$direction_param = htmlspecialchars($_GET["direction"]);
$zoom_type = htmlspecialchars($_GET["zoom_type"]);
$today_ts = get_ts_today($start_date_param, $direction_param);
if (!isset($today_ts)) {
    echo "No records found";
} else {
    $date = Carbon::createFromTimeStamp($today_ts);
    $start_day_utc = $date->startOfDay()->format('U');
    $end_day_utc = $date->endOfDay()->format('U');
    $row = get_current_geographical($end_day_utc);
    error_log("||referer=" . $_SERVER['HTTP_REFERER']);
    error_log("||today_ts=" . $today_ts);
    error_log("||eod=" . $end_day_utc);
    error_log("||zoom_type={$zoom_type}");
    error_log("||direction=" . $direction_param);
    error_log("||rs=" . $row['ts']);
    switch ($zoom_type) {
        case 1:
            // Humidity
            $field_name = "zoom_temp_hum";
            $field_value = $row['zoom_temp_hum'] == 1 ? 0 : 1;
            break;
        case 3:
            // Sewer
            $field_name = "zoom_sewer";
if ($size == 1) {
    $default_size_1 = "selected='selected'";
}
if ($size == 2) {
    $default_size_2 = "selected='selected'";
}
$today_ts = get_ts_today($start_date_param, $direction_param);
if (!isset($today_ts)) {
    echo "No records found";
} else {
    $date = Carbon::createFromTimeStamp($today_ts);
    $start_day_utc = $date->startOfDay()->format('U');
    $prev_day_str = $date->copy()->subDay()->format($param_date_format);
    $next_day_str = $date->copy()->addDay()->format($param_date_format);
    $end_day_utc = $date->endOfDay()->format('U');
    $geo_row = get_current_geographical($end_day_utc, $id);
    echo "<div style='padding:10px;'>";
    echo "<table border=0>";
    echo "<tr><td width={$range_width}></td><td width ={$graph_width}></td><td width=100></td></tr>";
    echo "<tr><td colspan=3>";
    echo "<table border=0 width=100%>";
    echo "<tr><td>";
    echo "<h2>{$sensor_type_name} Dashboard</h2>";
    echo "</td><td width='400' style='vertical-align:top'>";
    $geo_name = $geo_row['name'];
    echo "<span style='padding:4px 10px 4px 10px;font-size:20px;font-weight:bold;color:#CC6666;vertical-align:top;'>";
    echo $group_name;
    if (strlen($geo_name) > 0) {
        echo " - " . $geo_name;
    }
    echo "</span>";
    exit;
}
if (!isset($_GET["start_ts"])) {
    error_log("Must specify start_ts parameter");
    exit;
}
if (!isset($_GET["end_ts"])) {
    error_log("Must specify end_ts parameter");
    exit;
}
$id = htmlspecialchars($_GET["id"]);
$width = htmlspecialchars($_GET["width"]);
$height = htmlspecialchars($_GET["height"]);
$start_ts = htmlspecialchars($_GET["start_ts"]);
$end_ts = htmlspecialchars($_GET["end_ts"]);
$geo_row = get_current_geographical($end_ts, $id);
$font_size = 11;
$line_fill_color = 'white@0.65';
function add_plotlines($start_ts, $ts_arr)
{
    global $graph;
    $last_ts = end($ts_arr);
    $first_ts = reset($ts_arr);
    $line_pos = $start_ts + 3600 * 8;
    if ($first_ts < $line_pos && $last_ts > $line_pos) {
        $line8 = new PlotLine(VERTICAL, $line_pos, 'azure3', 1);
        $graph->AddLine($line8);
    }
    $line_pos = $start_ts + 3600 * 16;
    if ($first_ts < $line_pos && $last_ts > $line_pos) {
        $line16 = new PlotLine(VERTICAL, $line_pos, 'azure3', 1);
echo "</tr>";
echo "<tr>";
echo "<td colspan=8 style='text-align:center;'>";
echo "<div style='display:inline-block;border:2px solid darkmagenta;padding:10px;font-weight:bold;'>";
echo "<a href='help/resources.html'>Resources / Help</a>";
echo "</div>";
echo "</td>";
echo "</tr>";
echo "<tr><td></td><td></td><td></td><td></td><td></td><td></td>";
echo "<th style='text-align:center;'>Last Reading</th><th style='text-align:center;'>Group Type</th></tr>";
while ($row = mysqli_fetch_array($result)) {
    $result_geo = mysqli_query($conn, "SELECT name from geographical where group_id=" . $row['id'] . " order by ts desc");
    $row_geo = mysqli_fetch_array($result_geo);
    $result_group = mysqli_query($conn, "SELECT max(ts) as ts from readings where group_id=" . $row['id']);
    $row_group = mysqli_fetch_array($result_group);
    $geo_row = get_current_geographical($row_group['ts'], $row['id']);
    $result_location = mysqli_query($conn, "select name from locations where type=1 and group_id=" . $row['id'] . " and ts=" . "(select max(ts) as ts from locations where type=1 and group_id=" . $row['id'] . ")");
    $row_location = mysqli_fetch_array($result_location);
    echo "<tr>\n";
    echo "<td style='text-align:right;'><span style='font-size:20px;font-weight:bold;'>" . $row['name'] . "</span><br/>";
    echo "<i style='font-size:16px;'>" . $row_location['name'] . "</i>";
    echo "</td>\n";
    if (strlen($geo_row['zoom_temp_hum']) > 0 && strlen($geo_row['zoom_sewer']) > 0) {
        echo "<td><img src='images/calendar.png' onclick='click_button(" . $row['id'] . ");' height=40 width=40 style='cursor:pointer;'></td>";
        echo "<td><img src='images/graph.png' onclick='click_button(" . $row['id'] . ",\"dashboard.php\");' height=40 width=40 style='cursor:pointer;'></td>";
        echo "<td><img src='images/barchart.png' onclick='click_button(" . $row['id'] . ",\"events/event_monthly.php\");' height=40 width=40 style='cursor:pointer;'></td>";
        echo "<td><img src='images/download.png' onclick='click_button(" . $row['id'] . ",\"transfer/download.php\");' height=40 width=40 style='cursor:pointer;'></td>";
        $name = get_sensor_type_name($row['id']);
        echo "<td style='text-align:center'>";
        echo "<img src='images/location.png' onclick='click_button(" . $row['id'] . ",\"add_geographical.php\");' height=30 width=30 style='cursor:pointer;'><br/>";
        if (strlen($row_geo['name']) > 0) {