<?php
$time = "00:00";
set_time_limit(60000);
session_start();
?>

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="iso-8859-2">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">

    <title>Hello, world!</title>
  </head>
  <body>
        <div class="jumbotron">
      <h1 class="display-4">Testovací server</h1>
      <p class="lead">Server určený pro spoustění testů</p>
      <hr class="my-4">
      <p>Klikni pro spustění, pozastavení, nebo nastavení cyklu</p>
      <form action="index.php" method="post">
        <label for="fname">Zvolte cestu k .cmd:</label>
        <input type="text" name="cmdPath1"><br><br>
        <input type="submit" id="btnMain" name="someAction" value="Manual start" />
     </form>
     <form action="index.php" method="post">
      <br/>
      <label for="appt">Zvolte si čas, při kterém se testy budou každý den zapínat:</label>
        <br>
        <input type="time" id="appt" name="appt"
       min="00:00" max="24:00" required>
       <br/><br/>
       <label for="fname">Zvolte cestu k .cmd:</label>
        <input type="text" name="cmdPath2"><br><br>
       <input type="submit" name="anotherAction" value="Periodic start" />
       </form>
       <br>
       <h1 id="state" style="visibility:hidden; color:red;">Running</h1>
       <p hidden id="save" value=""></p>
       
    </div>
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
  </body>
</html>

<?php
    if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['someAction']))
    {
      //global $path;
      /*$path = $GLOBALS["path"];
      echo "<script>alert('$path')</script>";*/
      if($_SESSION['path'] == "")
      {
        $_SESSION['path'] = $_POST["cmdPath1"];
      }      
      func();
    }
    else if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['anotherAction']))
    {
     // global $path;
      $_SESSION['path'] = $_POST["cmdPath2"];     
      $time =  $_POST["appt"];
      func2($time);
    }
    function func()
    {
       exec($_SESSION['path']);
    }

    function func2($time)
    {
        echo "<script>setInterval(() => tick('$time'), 1000); document.getElementById('state').style.visibility = 'visible';</script>";
    }
?>

<script>
//setInterval(() => tick("12:11"), 1000);
function tick(str) {
  var array = str.split(':');
  var mins = new Date().getMinutes();
  var hours = new Date().getHours();
  var seconds = new Date().getSeconds();
  if (mins == array[1] && hours == array[0] && seconds == "0") {
    var button = document.getElementById('btnMain');
    button.click();
  }
  console.log("Tick " + hours);
  console.log('Tick ' + mins);
}
</script>