<?php

require_once("../helper.php");

function getSSLPage($url) {
    $curlSession = curl_init();
    curl_setopt($curlSession, CURLOPT_URL, $url);
    curl_setopt($curlSession, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true);
    $jsonData = json_decode(curl_exec($curlSession),true);
    curl_close($curlSession);
    return $jsonData;
}

if (checkLogin()) {
	$close = 0;
	$hostname = "192.168.179.169";
	

	if (isset($_POST["setMode"]) || isset($_GET["setMode"])) {
		//send new value
		$_POST["setMode"] = intval($_POST["setMode"]) + intval($_GET["setMode"]);
		if($_POST["setMode"] > 0 && $_POST["setMode"] < 60){
			$pwr = $_POST["setMode"]*100;
			$headers[] = "GET /set?mode=man&pwr=".$pwr." HTTP/1.1";
			$headers[] = "Host: ".$hostname;
			$headers[] = "";
			$remote = fsockopen("tcp://".$hostname, 80, $errno, $errstr, 5);
			fwrite($remote, implode("\r\n", $headers)."\r\n");
			$file = '';
			$file .= fread($remote, 1024);	
			fclose($remote);
		}elseif($_POST["setMode"] == 0){
			$headers[] = "GET /set?mode=eco&pwr=0 HTTP/1.1";
			$headers[] = "Host: ".$hostname;
			$headers[] = "";
			$remote = fsockopen("tcp://".$hostname, 80, $errno, $errstr, 5);
			fwrite($remote, implode("\r\n", $headers)."\r\n");
			$file = '';
			$file .= fread($remote, 1024);	
			fclose($remote);
		}
		$close = 1;
		
	}
 } else {
	$close = 1;
}
if (!$close) {
	echo <<<ENDE
<style>
input[type='range']::-webkit-slider-runnable-track {
  background: linear-gradient(to right, #00788F, #00788F), #D7D7D7;
  background-size: var(--background-size, 0%) 100%;
  background-repeat: no-repeat;
}
</style>
<!--begin::Form-->
<form id="heater_form">
	<div class="col-sm-11">
		<div class="form-check">
			<label class="form-label" for="addedCharge">Leistung: </label>
			<div style="font-size: 15px;color: #297195;display: inline-block;" id="modal-slider-label">0</div>
			<input type="range" id="modal-slider" name="setMode" class="form-range range-color-track" min="0" max="60" />
			<div class="d-flex justify-content-between">
				<span class="max-amount f-16 font-weight-normal darkGray-color text-right mt-1">Auto</span>
				<span class="max-amount f-16 font-weight-normal darkGray-color text-right mt-1">6.0 kW</span>
			</div>
		</div>
	</div>
</form>
ENDE;
}
