<?php
session_start();
include("../../php_crud/my_class.php");
$obj = new my_class();
extract($_POST);
// print_r ($_POST);
// echo("hello");

$newDate = date("Y-m-d", strtotime($date));
// print_r ($newDate);
$from_date_input = $newDate;
$days_to_fetch = $day;
$from_date_ts = strtotime($from_date_input);
$to_date_ts = strtotime("-$days_to_fetch  days", $from_date_ts);
$to_date_ts = strtotime("+1 day", $to_date_ts);
$nto_date = date('Y-m-d', $to_date_ts);

$from_date = date('d-M-Y', strtotime($from_date_input));
$from2_date = strtoupper($from_date);
// print_r($from2_date);
$to_date = date('d-M-Y', strtotime($nto_date));
$to2_date = strtoupper($to_date);
// print_r($to2_date);

// $cond = "S_PKID='$station' AND TO_CHAR(READ_DATE,'DD-MON-RRRR')='$date' ORDER BY READ_DATE ASC";
// $cond = "S_PKID='".$station."' and TO_DATE(M_DATE,'DD-MON-RRRR') BETWEEN '".$to2_date."' and '".$from2_date."' and D_TYPE='WL'";

$res = array();

$key_id = strtolower($key_id);

$flag = $obj->View_colmn_By_Cond("FFWC.STATIONS_TBL", "DANGER_LEVEL, RECORDED_HIGH_WL", "LOWER(KEYWORD)='$key_id' AND STATION_TYPE='1'");
$wl_level_high = $flag[0]["DANGER_LEVEL"];
$wl_danger_level = $flag[0]["RECORDED_HIGH_WL"];
// echo json_encode($wl_level_high);
// exit();
// $tbl = '"FFWC"."FFWC.DATA_TBL"';
$tbl = "FFWC.DATA_TBL";
// $cond = "S_PKID='".$station."' and TO_DATE(M_DATE,'DD-MON-RRRR') BETWEEN '".$to2_date."' and '".$from2_date."' and D_TYPE='WL'";
// $cond =  "STATION_CODE='$key_id'and TO_DATE(M_DATE,'DD-MON-RRRR') BETWEEN '".$to2_date."' and '".$from2_date."' and D_TYPE='WL'";
$cond = "LOWER(STATION_CODE)='$key_id' and TO_DATE(M_DATE,'DD-MON-RRRR') BETWEEN '$to2_date' and '$from2_date' and D_TYPE='W' ORDER BY M_DATE ASC";

// "S_PKID='".$station."'
$col = "TO_CHAR(M_DATE,'DD-MM-YYYY HH24:MI') AS READ_DATE,DATA_VALUE";

foreach ($obj->View_colmn_By_Cond($tbl, $col, $cond) as $value) {
	// print_r($value);
	$date = $value['READ_DATE'];
	$wl_level = $value['DATA_VALUE'];
	$res[] = array("$date", "$wl_level", "$wl_danger_level", "$wl_level_high");
	//  print_r($res);
}
// $response = array(
//     'data' => $res,
//     'danger_level' => $wl_danger_level,
//     'recorded_high_wl' => $wl_level_high
// );

if (count($res) > 0) {
	echo $datas = json_encode($res);
	// echo json_encode(array('danger_level' => $wl_danger_level, 'recorded_high_wl' => $wl_level_high));
} else {
	$res[] = array('', '', "$wl_danger_level", "$wl_level_high");
	echo $datas = json_encode($res);
	// echo json_encode(array('danger_level' => $wl_danger_level, 'recorded_high_wl' => $wl_level_high));
}
