<!DOCTYPE html>
<html lang="fr">

<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <title>Allo-GG</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link rel="stylesheet" href="bootstrap.css" media="screen">
</head>

<?php
if (isset($_POST['password']) && $_POST['password'] == 'heuzef') {


  $etat_status[0]='success';
  $etat_status[1]='light';

  $porte_status[0]='info';
  $porte_status[1]='light';

  $sonette_status[0]='danger';
  $sonette_status[1]='light';

  $sonette_btn[0]='
  <p class="bs-component">
    <a href="index.php?action=sound&msg=2">
      <button type="submit" name="action" class="btn btn-outline-info" id="sound">🔊 Activer la sonette 🔔</button>
    </a>
  </p>
  ';

  $sonette_btn[1]='
  <p>
    <a href="index.php?action=mute&msg=3">
      <button type="submit" name="action" class="btn btn-outline-warning" id="mute">🔇 Désactiver la sonette 🔔</button>
    </a>
  </p>
  ';

  $message[0]='
  <hr/>
  <div class="alert alert-dismissible alert-danger">
    <button type="button" class="close" data-dismiss="alert">&times;</button>
    <h4>Erreur !</h4>
    <p>Houston, nous avons un petit problème ...</p>
  </div>
  ';
  $message[1]='
  <hr/>
  <div class="alert alert-dismissible alert-primary">
    <button type="button" class="close" data-dismiss="alert">&times;</button>
    <h4>La porte est ouverte !</h4>
    <p>YOU SHALL NOT PASS!</p>
    <p>🚪</p>
  </div>
  ';
  $message[2]='
  <hr/>
  <div class="alert alert-dismissible alert-success">
    <button type="button" class="close" data-dismiss="alert">&times;</button>
    <h4>La sonette est maintenant active !</h4>
    <p>"Dring Dring !"<br/>
    - Oui ?<br/>
    - C\'est "moi", le seul et l\'unique.<br/>
    - ...
    </p>
    <p>🔊</p>
  </div>
  ';
  $message[3]='
  <hr/>
  <div class="alert alert-dismissible alert-success">
    <button type="button" class="close" data-dismiss="alert">&times;</button>
    <h4>Silence !</h4>
    <p>Hush now! Quiet now!<br/>
    It\'s time to lay your sleepy head!<br/>
    Said hush now! Quiet now!<br/>
    It\'s time to go to bed!</p>
    <p>🔇</p>
  </div>
  ';
  $message[4]='
  <hr/>
  <div class="alert alert-dismissible alert-success">
    <button type="button" class="close" data-dismiss="alert">&times;</button>
    <h4>Redémarrage en cours ...</h4>
    <p>Oh My Gawd! I\'m a Girl ! And still Not Ginger !</p>
    <p>🔄</p>
  </div>
  ';
  $message[5]='
  <hr/>
  <div class="alert alert-dismissible alert-success">
    <button type="button" class="close" data-dismiss="alert">&times;</button>
    <h4>Ça va être tout noir !</h4>
    <p>🔌</p>
  </div>
  ';

  if ($_GET["action"] == "open")
  {
      exec("../scripts/open.sh > /dev/null &");
      exec("sleep 1");
  }

  elseif ($_GET['action'] == "sound")
  {
      exec("../scripts/sound.sh > /dev/null &");
      exec("sleep 1");
  }

  elseif ($_GET['action'] == "mute")
  {
      exec("../scripts/mute.sh > /dev/null &");
      exec("sleep 1");
  }

  elseif ($_GET['action'] == "reboot")
  {
      exec("../scripts/reboot.sh > /dev/null &");
  }

  elseif ($_GET['action'] == "poweroff")
  {
      exec("../scripts/poweroff.sh > /dev/null &");
  }
?>

  <body>
    <div class="container" style="text-align:center;">

      <?php echo $message[$_GET["msg"]]; ?>

      <hr/>
      <div class="page-header">
        <div class="row">
          <div class="col-lg-12">
            <a href="index.php"><img src="../logo/allo-gg-logo.png" class="img-fluid" alt="Responsive image"></a>
            <br/><br/>
          </div>
        </div>
      </div>

      <h2 class="text-primary">Interface de contrôle du module</h2>
      <?php $etat = exec("/usr/local/bin/gpio -g read 18"); ?>
      <?php $sonette = exec("/usr/local/bin/gpio -g read 17"); ?>
      <?php $porte = exec("/usr/local/bin/gpio -g read 4"); ?>

      <span class="badge badge-pill badge-<?php echo $etat_status[$etat]; ?>">&nbsp;</span>
      &nbsp;
      <span class="badge badge-pill badge-<?php echo $porte_status[$porte]; ?>">&nbsp;</span>
      &nbsp;
      <span class="badge badge-pill badge-<?php echo $sonette_status[$sonette]; ?>">&nbsp;</span>
      <hr/>

      <div class="row">
        <div class="col-lg-12">
          <p class="bs-component">
            <a href="index.php?action=open&msg=1">
              <button type="submit" name="action" class="btn btn-primary btn-lg" id="open">🚪 <strong>OUVERTURE DE LA PORTE</strong> 🔑</button>
            </a>
          </p>
          <hr/>

          <?php echo $sonette_btn[$sonette]; ?>

          <hr/>
          <p class="bs-component">
            <a href="index.php?action=reboot&msg=4">
              <button type="submit" name="action" class="btn btn-outline-success" id="update">🔄 Redémarrer ⏻</button>
            </a>
            <a href="index.php?action=poweroff&msg=5">
              <button type="submit" name="action" class="btn btn-outline-danger" id="poweroff">🔌 Éteindre ⚡️</button>
            </a>
          </p>

          <hr/>
          <p class="bs-component">
            <a href="check_services.php">
              <button type="button" class="btn btn-outline-secondary" id="log">📜 Journal d'activité 🔍</button>
            </a>
          </p>
        </div>
        <hr/>
      </div>

    </div>

    <script src="jquery.js"></script>
    <script src="bootstrap.js"></script>

  </body>
</html>

<?php
}
else {
	echo '
  <br/><br/><br/><br/>
  <div class="row col-lg-12">
    <div class="col-lg-4"></div>
    <div class="col-lg-4">
    <hr/>
    <form action="" method="post">
    <div class="input-group">
      <div class="input-group-prepend">
        <span class="input-group-text">🔒</span>
      </div>
      <input class="form-control" name="password" placeholder="Mot de passe ..." type="password">
      &nbsp;&nbsp;<button type="submit" class="btn btn-light">🔑 OK</button>
    </div>
    </form>
    <hr/>
    </div>
    <div class="col-lg-4"></div>
  </div>
  ';
}
?>
