<?php

include "db.class.php";
$db = new db();
include "html.class.php";
$html = new html();

if (isset($_GET['unit_1'])) {
    // unit_1
    $data_water_res = $db->exec_query('water', ['*'], '', '', '', '', "unit_id = 1", "ORDER BY record_id DESC LIMIT 1");
    $unit_1_water_data = $data_water_res->fetch_assoc();
    $data_res = $db->exec_query('actions', ['*'], '', '', '', '', "unit_id = 1");
    $unit_1_data = $data_res->fetch_assoc();
    if ($unit_1_water_data['tank'] == 1) {
        $tank = "EMPTY";
    } else {
        $tank = "FULL";
    }
     echo "<div style='font-size:3em;'>{$unit_1_water_data['date_time']}<h2>TANK: $tank</h2></div>" . $html->generateCircularProgressBar(0, ($unit_1_data['threshold'] * 2.6 / 1000), ($unit_1_water_data['count'] * 2.6 / 1000), "L", 2);
    //echo "<div style='font-size:3em;'>{$unit_1_water_data['date_time']}<h2>TANK: $tank</h2></div>" . $html->generateCircularProgressBar(0, 6350, 6350, "L", 2);
}
if (isset($_GET['unit_2'])) {
    // unit_2
    $data_water_res = $db->exec_query('water', ['*'], '', '', '', '', "unit_id = 2", "ORDER BY record_id DESC LIMIT 1");
    $unit_2_water_data = $data_water_res->fetch_assoc();
    $data_res = $db->exec_query('actions', ['*'], '', '', '', '', "unit_id = 2");
    $unit_2_data = $data_res->fetch_assoc();
    echo "<div style='font-size:3em;'>{$unit_2_water_data['date_time']}</div>" . $html->generateCircularProgressBar(0, ($unit_2_data['threshold'] * 2.6 / 1000), ($unit_2_water_data['count'] * 2.6 / 1000), "L", 2);
}
if (isset($_GET['unit_3'])) {
    // unit_3
    $data_water_res = $db->exec_query('water', ['*'], '', '', '', '', "unit_id = 3", "ORDER BY record_id DESC LIMIT 1");
    $unit_3_water_data = $data_water_res->fetch_assoc();
    $data_res = $db->exec_query('actions', ['*'], '', '', '', '', "unit_id = 3");
    $unit_3_data = $data_res->fetch_assoc();
    echo "<div style='font-size:3em;'>{$unit_3_water_data['date_time']}</div>" . $html->generateCircularProgressBar(0, ($unit_3_data['threshold'] * 2.6 / 1000), (($unit_3_water_data['count'] * 2.6) / 1000), "L", 2);
}

if (isset($_GET['unit_1_threshold'])) {
    $liters = $_GET['unit_1_threshold'];
    $liters = $liters * 1000;
    $pulses = $liters / 2.6;
    $db->update('actions', ['threshold'], [$pulses], "unit_id = 1");
}


if (isset($_GET['unit_2_threshold'])) {
    $mil = $_GET['unit_2_threshold'];
    $liters = $mil * 1000;
    $pulses = $liters / 2.6;
    $db->update('actions', ['threshold'], [$pulses], "unit_id = 2");
}


if (isset($_GET['unit_3_threshold'])) {
    $liters = $_GET['unit_3_threshold'];
    $liters = $liters * 1000;
    $pulses = $liters / 2.6;
    $db->update('actions', ['threshold'], [$pulses], "unit_id = 3");
}

if (isset($_GET['reset_input'])) {
    $reset_input = $_GET['reset_input'];
    $db->update('reset_date', ['day'], [$reset_input], "record_id = 1");
}
