<?php

require_once("../helper.php");
$close = 0;
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()) {
	if (isset($_POST["sensorSelect1"])) {	//if form war sent add an action
		if(isset($_POST["floor"])){
			switch($_POST["floor"]){
				case "UG":
					$floor = "UG";
					break;
				case "EG":
					$floor = "EG";
					break;
				case "OG":
					$floor = "OG";
					break;
				default:
					$floor = "";
			}
		}else{
			$floor = "";
		}
		if(isset($_POST["tSpanFrom"])){
			$tFrom = date("Y-m-d H:i:s",strtotime($_POST["tSpanFrom"]));
		}else{
			$tFrom = "00:10:00";
		}
		if(isset($_POST["tSpanTo"])){
			$tTo = date("Y-m-d H:i:s",strtotime($_POST["tSpanTo"]));
		}else{
			$tTo = "23:59:00";
		}
		if(isset($_POST["runOnce"])){
			$force = true;
		}else{
			$force = false;
		}
		if(isset($_POST["actMo"])){$mo = true;}else{$mo = false;}
		if(isset($_POST["actDi"])){$di = true;}else{$di = false;}
		if(isset($_POST["actMi"])){$mi = true;}else{$mi = false;}
		if(isset($_POST["actDo"])){$do = true;}else{$do = false;}
		if(isset($_POST["actFr"])){$fr = true;}else{$fr = false;}
		if(isset($_POST["actSa"])){$sa = true;}else{$sa = false;}
		if(isset($_POST["actSo"])){$so = true;}else{$so = false;}
		if(isset($_POST["actFerien"])){$ferien = true;}else{$ferien = false;}
		if(isset($_POST["actFeier"])){$feiertag = true;}else{$feiertag = false;}
		$close = 0;
		if(isset($_POST["changeID"])){
			$id = intval($_POST["changeID"]);
			if($_POST["changeID"] != "0" && $id == 0){
				$id = -1;
			}
		}else{
			$id = -1;
		}
		$id = 2;
		$mysql = new mysqli($mysql_server, $mysql_solarUser, $mysql_solarPass, $mysql_solarDB);

		if($id > -1){
			$qry = "UPDATE autoActions SET
				 `floor` = '".$floor."', `window_from`= '".$tFrom."', `window_to` = '".$tTo."',
				 `force_once` = '".$force."', `mo` = '".$mo."', `di` = '".$di."', `mi` = '".$mi."',
				 `do` = '".$do."', `fr` = '".$fr."', `sa` = '".$sa."', `so` = '".$so."', 
				 `ferien` = '".$ferien."', `feiertag` = '".$feiertag."' WHERE id=".$id.";";
			mysqli_query($mysql, $qry);
			mysqli_query($mysql, "DELETE FROM autoactionsActors WHERE actionID=".$id.";");
			mysqli_query($mysql, "DELETE FROM autoactionsSensors WHERE actionID=".$id.";");
			$actionID = $id;
		}else{
			$qry = "INSERT into autoActions 
				(`id`, `floor`, `window_from`, `window_to`, `force_once`, `mo`, `di`, `mi`, `do`, `fr`, `sa`, `so`, `ferien`, `feiertag`, `last_run`) 
				VALUES (NULL, '".$floor."', '".$tFrom."', '".$tTo."', '".$force."', '".$mo."', '".$di."', '".$mi."', '".$do."', '".$fr."', '".$sa."', '".$so."', '".$ferien."', '".$feiertag."', '2020-01-01 12:00:00.000000')";
			mysqli_query($mysql, $qry);
			$actionID = mysqli_insert_id($mysql);
		}
		
		//Get all Sensors:
		$num=1;
		while(isset($_POST["sensorSelect".$num])){
			$id = intval($_POST["sensorSelect".$num]);
			if($_POST["sensorSelect".$num] != "0" && $id == 0){
				$id = -1;
			}
			if(isset($_POST["btnLogic".$num])){
				if(strtolower($_POST["btnLogic".$num]) == "oder"){
					$weight = "or";
				}else{
					$weight = "and";
				}
			}else{
				$weight = "and";
			}
			if (in_array($_POST["btnOperator".$num], array('+','-','=','&lt;','&gt;','!='))) {
				$cond = $_POST["btnOperator".$num];
			}else{
				$cond = "=";
				echo $_POST["btnOperator".$num];
			}
			echo $_POST["btnOperator".$num];
			$valID = intval($_POST["paramSelect".$num]);
			if($_POST["paramSelect".$num] != "0" && $id == 0){
				$valID = -1;
			}
			$state = mysqli_real_escape_string($mysql, $_POST["threshold".$num]);

			$qry = "INSERT INTO `autoactionsSensors` 
				(`id`, `sensorID`, `state`, `valID`, `condType`, `link`, `actionID`) 
				VALUES (NULL, '".$id."', '".$state."', '".$valID."', '".$cond."', '".$weight."', '".$actionID."')";
			mysqli_query($mysql, $qry);
			$num++;
		}
		$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="accordion" id="actionAccordion">
  <div class="accordion-item">
    <h2 class="accordion-header" id="headingOne">
      <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
        <i class="bi bi-lightning-fill"></i> &nbsp;&nbsp;Auslöser
      </button>
    </h2>
    <div id="collapseOne" class="accordion-collapse collapse show" aria-labelledby="headingOne" data-bs-parent="#actionAccordion">
      <div class="accordion-body" id="sensorsList">
	  	<div class="input-group mb-1" id="sensorSettings1">
	  		<div class="form-floating">
        		<select class="form-select" id="sensorSelect1"  name="sensorSelect1" aria-label="Default select example">
				</select>
				<label for="sensorSelect1">Sensor</label>
			</div>
			<div class="form-floating" id="paramBlock1">
				<select class="form-select" id="paramSelect1" name="paramSelect1" aria-label="Default select example">
				</select>
				<label for="paramSelect1">Messwert</label>
			</div>
			<button class="btn btn-outline-secondary " type="button" id="btnOperator1" name="btnOperator1">&gt;</button>
			<div class="form-floating" id="valBlock1">
				<input  type="number" class="form-control" id="threshold1" placeholder="0" value="0" name="threshold1"></input>
				<label for="threshold1">Wert/Schwelle</label>
			</div>
		</div>
      </div>
	  <button class="btn btn-outline-success btn-sm" type="button" id="btnAddSensor"><i class="bi bi-plus-lg"></i></button>
    </div>
  </div>
  <div class="accordion-item">
    <h2 class="accordion-header" id="headingTwo">
      <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
        <i class="bi bi-calendar-check-fill"></i> &nbsp;&nbsp;Bedingungen
      </button>
    </h2>
    <div id="collapseTwo" class="accordion-collapse collapse" aria-labelledby="headingTwo" data-bs-parent="#actionAccordion">
      <div class="accordion-body">
		<div class="input-group mb-3" id="timespan">
			<span class="input-group-text">Aktiver Zeitraum: </span>
			<div class="form-floating">
				<input  type="time" class="form-control" id="tSpanFrom" name="tSpanFrom" placeholder="0" value="00:00"></input>
				<label>Von</label>
			</div>
			<div class="form-floating">
				<input  type="time" class="form-control" id="tSpanTo" name="tSpanTo" placeholder="0" value="23:59"></input>
				<label>Bis</label>
			</div>
		</div>
		Aktive Wochentage:
		<div class="input-group mb-3" id="timespan">
			<div class="input-group-text">
				<input class="form-check-input mt-0 me-1" type="checkbox" value="1" id="actMo" name="actMo"><label class="form-check-label" for="actMo">Mo</label>
			</div>
			<div class="input-group-text">
				<input class="form-check-input mt-0 me-1" type="checkbox" value="1" id="actDi" name="actDi"><label class="form-check-label" for="actDi">Di</label>
			</div>
			<div class="input-group-text">
				<input class="form-check-input mt-0 me-1" type="checkbox" value="1" id="actMi" name="actMi"><label class="form-check-label" for="actMi">Mi</label>
			</div>
			<div class="input-group-text">
				<input class="form-check-input mt-0 me-1" type="checkbox" value="1" id="actDo" name="actDo"><label class="form-check-label" for="actDo">Do</label>
			</div>
			<div class="input-group-text">
				<input class="form-check-input mt-0 me-1" type="checkbox" value="1" id="actFr" name="actFr"><label class="form-check-label" for="actFr">Fr</label>
			</div>
			<div class="input-group-text">
				<input class="form-check-input mt-0 me-1" type="checkbox" value="1" id="actSa" name="actSa"><label class="form-check-label" for="actSa">Sa</label>
			</div>
			<div class="input-group-text">
				<input class="form-check-input mt-0 me-1" type="checkbox" value="1" id="actSo" name="actSo"><label class="form-check-label" for="actSo">So</label>
			</div>
		</div>
		Ferien/Feiertage:
		<div class="input-group mb-3" id="timespan">
			<div class="input-group-text">
				<input class="form-check-input mt-0 me-2" type="checkbox" value="1" id="actFerien" name="actFerien"><label class="form-check-label" for="actFerien">In den Ferien ausführen</label>
			</div>
			<div class="input-group-text">
				<input class="form-check-input mt-0 me-2" type="checkbox" value="1" id="actFeier" name="actFeier"><label class="form-check-label" for="actFeier">An Feiertagen ausführen</label>
			</div>
		</div>
		Falls die Bedingung bis zuletzt nicht erfüllt wurde:
		<div class="input-group mb-3" id="timespan">
			<div class="input-group-text">
				<input class="form-check-input mt-0 me-2" type="checkbox" value="1" id="runOnce" name="runOnce"><label class="form-check-label" for="runOnce">Auf jeden Fall zu Ende des aktiven Zeitraums ausführen</label>
			</div>
		</div>
      </div>
    </div>
  </div>
  <div class="accordion-item">
    <h2 class="accordion-header" id="headingThree">
      <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
        <i class="bi bi-play-fill"></i> &nbsp;&nbsp;Aktionen
      </button>
    </h2>
    <div id="collapseThree" class="accordion-collapse collapse" aria-labelledby="headingThree" data-bs-parent="#actionAccordion">
      <div class="accordion-body">
        <div class="accordion-body" id="actorsList">
			<div class="input-group mb-3" id="actorSettings1">
				<div class="form-floating">
					<select class="form-select" id="actorSelect1" name="actorSelect1">
					</select>
					<label for="actorSelect1">Aktor</label>
				</div>
				<div class="form-floating" id="actParamBlock1">
					<select class="form-select" id="actParamSelect1" name="actParamSelect1">
					</select>
					<label for="paramSelect1">Eigenschaft</label>
				</div>
				<button class="btn btn-outline-secondary " type="button" id="btnActOperator1" name="btnActOperator1">&gt;</button>
				<div class="form-floating" id="actValBlock1">
					<input  type="number" class="form-control" id="actValue1" name="actValue1" placeholder="0" value="0"></input>
					<label for="actValue1">Sollwert</label>
				</div>
			</div>
		</div>
		<button class="btn btn-outline-success btn-sm" type="button" id="btnAddActor"><i class="bi bi-plus-lg"></i></button>
      </div>
    </div>
  </div>
</div>
</form>
ENDE;
}
