<?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;
	if (isset($_POST["evStart/Stop"])) {
		if ($_POST["evStart/Stop"] == "Laden starten") {
			$close = 1;
			getSSLPage("http://192.168.179.122/api/set?frc=2");
		} else if ($_POST["evStart/Stop"] == "Laden stoppen") {
			$close = 1;
			getSSLPage("http://192.168.179.122/api/set?frc=1");
		}
		$close = 1;
	} else if (isset($_POST["evMode"])) {
		if ($_POST["fte"] > 100)
			$_POST["fte"] = 100;
		else if ($_POST["fte"] < 0)
			$_POST["fte"] = 0;
		if ($_POST["evAmp"] > 16)
			$_POST["evAmp"] = 16;
		if ($_POST["evAmp"] < 6)
			$_POST["evAmp"] = 6;
		$_POST["fte"] = $_POST["fte"] * 90;
		$seconds = strtotime('1970-01-01 ' . $_POST["ftt"] . ':00GMT');
		if ($_POST["evMode"] == "eco") {
			$close = 1;
			if ($_POST["ftt"] && $_POST["fte"] > 0) {
				getSSLPage("http://192.168.179.122/api/set?ate=" . $_POST["fte"] . "&att=" . $seconds . "&amp=" . $_POST["evAmp"] . "&fup=true&lmo=4");
			} else {
				getSSLPage("http://192.168.179.122/api/set?amp=" . $_POST["evAmp"] . "&fup=true&lmo=4");
			}
		} else if ($_POST["evMode"] == "default") {
			$close = 1;
			if ($_POST["ftt"] && $_POST["fte"] > 0) {
				getSSLPage("http://192.168.179.122/api/set?ate=" . $_POST["fte"] . "&att=" . $seconds . "&amp=" . $_POST["evAmp"] . "&fup=true&lmo=3");
			} else {
				getSSLPage("http://192.168.179.122/api/set?amp=" . $_POST["evAmp"] . "&fup=true&lmo=3");
			}
		} else if ($_POST["evMode"] == "NextTrip") {
			$close = 1;
			if ($_POST["ftt"] && $_POST["fte"] > 0) {
				getSSLPage("http://192.168.179.122/api/set?ate=" . $_POST["fte"] . "&att=" . $seconds . "&amp=" . $_POST["evAmp"] . "&fup=true&lmo=5");
			} else {
				getSSLPage("http://192.168.179.122/api/set?amp=" . $_POST["evAmp"] . "&fup=true&lmo=5");
			}
		}
		$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>
<div class="d-grid gap-2 col-6 mx-auto my-4">
	<button class="btn btn-primary" id="evStart/Stop">Laden starten</button>
</div>
<!--begin::Form-->
<form id="carOG_form">
	<div class="card m-3">
		<div class="card-header py-1 px-3">
			<div class="card-title">Modus</div>
		</div>
		<!--begin::Body-->
		<div class="card-body">
			<div class="col-sm-10">
				<div class="form-check">
					<input class="form-check-input" type="radio" name="evMode" id="EV_Default" value="default">
					<label class="form-check-label" for="EV_Default"> Immer Laden </label></input>
				</div>
				<div class="form-check">
					<input class="form-check-input" type="radio" name="evMode" id="EV_Eco" value="eco">
					<label class="form-check-label" for="EV_Eco"> Überschuss </label></input>
				</div>
				<div class="form-check disabled">
					<input class="form-check-input" type="radio" name="evMode" id="EV_NextTrip" value="NextTrip">
					<label class="form-check-label" for="EV_NextTrip"> Ladeplanung </label></input>
				</div>
			</div>
		</div>
	</div>
	<div class="card m-3">
		<div class="card-header py-1 px-3">
			<div class="card-title">Max. Ladestrom</div>
		</div>
		<div class="card-body">
			<div class="col-sm-10">	
				<div class="form-check">
					<input class="form-check-input" type="radio" name="evAmp" id="EV_6A" value="6">
					<label class="form-check-label" for="EV_6A"> 6A </label></input>
				</div>
				<div class="form-check">
					<input class="form-check-input" type="radio" name="evAmp" id="EV_10A" value="10">
					<label class="form-check-label" for="EV_10A"> 10A </label></input>
				</div>
				<div class="form-check disabled">
					<input class="form-check-input" type="radio" name="evAmp" id="EV_16A" value="16">
					<label class="form-check-label" for="EV_16A"> 16A </label></input>
				</div>
			</div>
		</div>
	</div>
	<div class="card m-3">
		<div class="card-header py-1 px-3">
			<div class="card-title">Ladeplanung</div>
		</div>
  		<div class="card-body">
			<div class="row mb-3">
				<div class="form-check">
					<label class="form-label" for="addedCharge">Zusätzlich benötigte Ladung (%): </label>
					<div style="font-size: 15px;color: #297195;display: inline-block;" id="modal-slider-label">sdf</div>
					<input type="range" id="modal-slider" name="fte" class="form-range range-color-track" min="1" max="100" />
					<div class="d-flex justify-content-between">
						<span class="max-amount f-16 font-weight-normal darkGray-color text-right mt-1">1%</span>
						<span class="max-amount f-16 font-weight-normal darkGray-color text-right mt-1">100%</span>
					</div>
					<!--<input  name="fte" class="form-control" type="number" max="100" min="1" value="" />-->
				</div>
			</div>
			<div class="row mb-3">
				<div class="form-check">
					<label class="form-label" for="planedTime">Zeitpunkt zudem geladen sein soll: </label>
					<input id="EV_NextTripTime" name="ftt" class="form-control" size="2" type="time" value=""/>
				</div>
			</div>
  		</div>
	</div>
</form>
ENDE;
}
