<?php
// Start or resume the session
session_start();

/*
Used to enhance security by preventing browsers and proxies from caching pages.
When a user logs out of a web application, you wouldn't want the next person using the same device to simply press the back button in the browser
and access the previous user's session or sensitive information.
This is particularly important in scenarios like shared devices, where multiple users might use the same browser sequentially.
However, it's noted that this method does not work for iOS users, which is be due to iOS's handling of cache control headers and
its aggressive caching strategies to improve user experience and performance.
*/
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1.
header("Pragma: no-cache"); // HTTP 1.0.
header("Expires: 0"); // Proxies.

// We verify that the user is logged in, if not we redirect the user to index.php the log in page.
if (!isset($_SESSION['username'])) {
  header("Location: index.php");
  exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>LeanC IOT | SaaS MES</title>
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <link rel='stylesheet' href='https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css'>
  <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.10.2/css/all.css'>
  <link rel="stylesheet" href="./style.css">
</head>
<body>
  <!-- NAV -->
  <nav class="navbar navbar-expand-custom navbar-mainbg">
    <a class="navbar-brand navbar-logo" href="dashboard.php">LeanC IOT | SaaS MES</a>
    <button class="navbar-toggler" type="button" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <i class="fas fa-bars text-white"></i>
    </button>
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto">
        <div class="hori-selector"><div class="left"></div><div class="right"></div></div>

        <li class="nav-item">
          <a class="nav-link" href="dashboard.php"><i class="fas fa-tachometer-alt"></i>Dashboard</a>
        </li>
        <li class="nav-item active">
          <a class="nav-link" href="monitoring.php"><i class="far fa-chart-bar"></i>Monitoring</a>
        </li>
        <!-- We check if the user's level is equal to "Admin". -->
        <?php if ($_SESSION['level'] == 'Admin') : ?>
          <li class="nav-item">
            <a class="nav-link" href="admin.php"><i class="fas fa-sign-out-alt"></i>Administration</a>
          </li>
        <?php endif; ?>
        <li class="nav-item">
          <a class="nav-link" href="logout.php"><i class="fas fa-sign-out-alt"></i>Log Out (<?php echo htmlspecialchars($_SESSION['username']); ?>)</a>
        </li>
      </ul>
    </div>
  </nav>
  <!-- NAV -->
  <div class="container mt-4">
    <form class="row" method="post" action="stats.php">
      <?php
      // Connecting to the database
      require_once '/my_custom_url/PRIVATE/db_config.php';
      $conn = new mysqli($servername, $username, $password, $dbname);

      // Checking the connection
      if ($conn->connect_error) {
        die("Connection failure:" . $conn->connect_error);
      }

      // Creates a new DateTime object representing the current date and time in a specified time zone.
      $currentDate = new DateTime("now", new DateTimeZone('Europe/Paris'));
      $dateOfTheDay = $currentDate->format('Y-m-d');
      $hourOfNow = $currentDate->format('H:i');

      // SQL query to retrieve the list of different sensors.
      $sql = "SELECT DISTINCT Sensor FROM ACTIVITY";
      $result = $conn->query($sql);

      // If there are results, then generate the dropdown list.
      if ($result->num_rows > 0) {
        echo '<div class="col-12 mb-3"><select name="nameoftheSensor" class="form-control">';
        while($row = $result->fetch_assoc()) {
          echo '<option value="' . htmlspecialchars($row['Sensor']) . '">' . htmlspecialchars($row['Sensor']) . '</option>';
        }
        echo '</select></div>';
      } else {
        echo "<p>No sensor found.</p>";
      }
      // Terminates the connection to the database server.
      $conn->close();
      ?>
      <!--
      The current version of the analysis period selection form could be enhanced to prevent :
      - negative inputs (end date before start date)
      - zero-duration inputs (end date equals start date).
      This improvement will be proposed soon, or readers are welcome to suggest improvements!
      For now, I'll postpone this feature to the next version, as there is no risk of bugs other than the form returning no results.
      -->
      <div class="col-12 col-sm-6 col-md-3 form-group">
        <label for="startDate" class="form-label">Start Date:</label>
        <input type="date" class="form-control" id="startDate" name="startDate" value="<?php echo $dateOfTheDay; ?>">
      </div>
      <div class="col-12 col-sm-6 col-md-3 form-group">
        <label for="startHour" class="form-label">Start Time:</label>
        <!-- By default, we propose to set the start time of analysis to midnight.-->
        <input type="time" class="form-control" id="startHour" name="startHour" value="00:00">
      </div>
      <div class="col-12 col-sm-6 col-md-3 form-group">
        <label for="ceaseDate" class="form-label">End Date:</label>
        <input type="date" class="form-control" id="ceaseDate" name="ceaseDate" value="<?php echo $dateOfTheDay; ?>">
      </div>
      <div class="col-12 col-sm-6 col-md-3 form-group">
        <label for="ceaseHour" class="form-label">End Time:</label>
        <!-- By default, we propose to set the end time of analysis to the current time.-->
        <input type="time" class="form-control" id="ceaseHour" name="ceaseHour" value="<?php echo $hourOfNow; ?>">
      </div>
      <div class="col-12 col-md-3 form-group">
        <button type="submit" class="btn btn-primary">Submit</button>
      </div>
    </form>
  </div>
  <script src='https://code.jquery.com/jquery-3.4.1.min.js'></script>
  <script src='https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/popper.min.js'></script>
  <script src='https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/js/bootstrap.min.js'></script>
  <script src="./script.js"></script>
</body>
</html>
