<?php
session_start();
error_reporting(1);
include('includes/config.php');

$start = $_POST['start'];
$end = $_POST['end']; 
$interval = $_POST['interval'];
$selectTable = SELECT_TABLE;

//devid required
//INNER JOIN table2
//ON table1.column_name = table2.column_name;
//$old = "SELECT * FROM `aws`  where  CreationDate >= '$start' and CreationDate <= '$end' order by CreationDate desc";

$startDate = date("Y-m-d H:i:s", strtotime($start));
$endDate = date("Y-m-d H:i:s", strtotime($end));  

$intervalLast = 5;
if (!empty($interval)) {
    if (strcmp("DEFAULT", $interval) == 0) {
          $default = TRUE;
        
    } else { 
        
        $setInverval = explode(" ", $interval);
        $intervalLast = $setInverval[0];
        $intervalType = $setInverval[1];
        $time = "00:00:00";
        // if (strcmp($intervalType, "DAY") == 0) {
        //      $intervalType = "MINUTE";
        //      $intervalLast = 1440;
             
        //     // $time = "00:00-23:59";
               
        //  } 
    }
}
//print_r($startdate, $endDate);
$old = "SELECT id, ROUND(MAX(WSmax),2) as WSmax, ROUND(AVG(WSavg),2) as WSavg, ROUND(MIN(WSmin),2) as WSmin, ROUND(MAX(TMPmax),2) as TMPmax, ROUND(AVG(TMPavg),2) as TMPavg, ROUND(MIN(TMPmin),2) as TMPmin, ROUND(MAX(RHmax),2) as RHmax, ROUND(AVG(RHavg),2) as RHavg, ROUND(MIN(RHmin),2) as RHmin, Battery, WD, ROUND(SUM(RAINavg),2) as RAINavg, ROUND(AVG(SOLARavg),2) as SOLARavg, ROUND(AVG(BPavg),2) as BPavg, CreationDate from $selectTable WHERE CreationDate BETWEEN '$startDate' AND '$endDate' GROUP BY FLOOR(UNIX_TIMESTAMP(CreationDate)/($intervalLast * 60)) ORDER BY $selectTable.`CreationDate` ASC";

//$old = "SELECT id, ROUND(MAX(WSmax),2) as WSmax, ROUND(AVG(WSavg),2) as WSavg, ROUND(MIN(WSmin),2) as WSmin, ROUND(MAX(TMPmax),2) as TMPmax, ROUND(AVG(TMPavg),2) as TMPavg, ROUND(MIN(TMPmin),2) as TMPmin, ROUND(MAX(RHmax),2) as RHmax, ROUND(AVG(RHavg),2) as RHavg, ROUND(MIN(RHmin),2) as RHmin, Battery, WD, ROUND(AVG(BPavg),2) as BPavg, ROUND(SUM(RAINavg),2) as RAINavg, ROUND(AVG(SOLARavg),2) as SOLARavg, CreationDate from $selectTable WHERE DATE(CreationDate) BETWEEN '$startDate' AND '$endDate' GROUP BY FLOOR(UNIX_TIMESTAMP(CreationDate)/($intervalLast * 60)) ORDER BY $selectTable.`CreationDate` ASC";
if($default == TRUE) {
        //echo "Default is::",$default;
        $old = "SELECT * from $selectTable WHERE CreationDate BETWEEN '$startDate' AND '$endDate' ORDER BY $selectTable.`CreationDate` ASC";
    
}

if (strcmp($intervalType, "DAY") == 0) {

    $old = "SELECT id, ROUND(MAX(WSmax),2) as WSmax, ROUND(AVG(WSavg),2) as WSavg, ROUND(MIN(WSmin),2) as WSmin, ROUND(MAX(TMPmax),2) as TMPmax, ROUND(AVG(TMPavg),2) as TMPavg, ROUND(MIN(TMPmin),2) as TMPmin, ROUND(MAX(RHmax),2) as RHmax, ROUND(AVG(RHavg),2) as RHavg, ROUND(MIN(RHmin),2) as RHmin, Battery, WD, ROUND(SUM(RAINavg),2) as RAINavg, ROUND(AVG(SOLARavg),2) as SOLARavg, ROUND(AVG(BPavg),2) as BPavg, CreationDate from $selectTable WHERE CreationDate BETWEEN '$startDate' AND '$endDate' GROUP BY DATE(DATE_SUB(CreationDate, INTERVAL 24 HOUR)) ORDER BY $selectTable.`CreationDate` ASC";
} 

if (strcmp($intervalType, "MONTH") == 0) {

    $old = "SELECT id, ROUND(MAX(WSmax),2) as WSmax, ROUND(AVG(WSavg),2) as WSavg, ROUND(MIN(WSmin),2) as WSmin, ROUND(MAX(TMPmax),2) as TMPmax, ROUND(AVG(TMPavg),2) as TMPavg, ROUND(MIN(TMPmin),2) as TMPmin, ROUND(MAX(RHmax),2) as RHmax, ROUND(AVG(RHavg),2) as RHavg, ROUND(MIN(RHmin),2) as RHmin, Battery, WD, ROUND(SUM(RAINavg),2) as RAINavg, ROUND(AVG(SOLARavg),2) as SOLARavg, ROUND(AVG(BPavg),2) as BPavg, CreationDate from $selectTable WHERE CreationDate BETWEEN '$startDate' AND '$endDate' GROUP BY MONTH(CreationDate) ORDER BY $selectTable.`CreationDate` ASC";
} 

if (strcmp($intervalType, "YEAR") == 0) {

    $old = "SELECT id, ROUND(MAX(WSmax),2) as WSmax, ROUND(AVG(WSavg),2) as WSavg, ROUND(MIN(WSmin),2) as WSmin, ROUND(MAX(TMPmax),2) as TMPmax, ROUND(AVG(TMPavg),2) as TMPavg, ROUND(MIN(TMPmin),2) as TMPmin, ROUND(MAX(RHmax),2) as RHmax, ROUND(AVG(RHavg),2) as RHavg, ROUND(MIN(RHmin),2) as RHmin, Battery, WD, ROUND(SUM(RAINavg),2) as RAINavg, ROUND(AVG(SOLARavg),2) as SOLARavg, ROUND(AVG(BPavg),2) as BPavg, CreationDate from $selectTable WHERE CreationDate BETWEEN '$startDate' AND '$endDate' GROUP BY YEAR(CreationDate) ORDER BY $selectTable.`CreationDate` ASC";
} 
    
//$old = "SELECT * FROM `aws_aqm` order by CreationDate desc";
$query = $dbh->prepare($old);
$query->execute();


$results = $query->fetchAll(PDO::FETCH_OBJ);



if($query->rowCount() > 0){
    echo json_encode($results);
    // if (strcmp($intervalType, "DAY") == 0) {
    //     // if ($startDate != date("Y-m-d")) {
    //     //     array_pop($results);
    //     // }
	   // echo json_encode($results);

    // } else {
	   // echo json_encode($results);
    // }
    //echo json_encode(array_pop($results));
    //echo json_encode($results);
	//echo json_encode(array("st" => $startDate, "en"=> $endDate));

} else {
	$msg = "No data Found";
	echo json_encode(array("warning" => $msg));
}
	
?>
