<?php
session_start();
?>
<!DOCTYPE html>

<html>
        <head>
                <?php include("head.php"); ?>
        </head>

        <!-- Contenu du site -->
        <body>
                <?php 
                include("header.php");
		require("erreurs.php");
                if(isset($_SESSION['Login'], $_SESSION['Password']))
                {
                        ?>
			<div class="row">
			        <div class="col-6 col-md-6 offset-md-3 offset-3">
                			<br/>
                			<h1 style="margin-bottom:20px; margin-top:60px; border-bottom:1px solid #CCC; padding-bottom:20px;"><i class="fas fa-history"></i> Suppression d'une Raspberry</h1>
					<?php
					$capteurs = getSensorByRaspberry($_POST['ip']);

					$ini = "";
					$arduino_simple = "[arduino]\r\n";
					$arduino_spi = "[arduino]\r\n";
					$nucleo = "[nucleo]\r\n";

					$a_simple = false;
					$a_spi = false;
					$n = false;
					$ordre = " ordre=2";

					foreach($capteurs as $capteur)
					{
						$numero = $capteur['numero'];
						$raspberry = $capteur['raspberry'];
						$version = $capteur['version'];
						if($version == 0)
						{
							$a_simple = true;
							$arduino_simple = $arduino_simple.$raspberry." capteur=".$numero."-".$version." setup=0".$ordre."\n";
						}
						elseif($version == 1)
						{
							$a_spi = true;
							$arduino_spi= $arduino_spi.$raspberry." capteur=".$numero."-".$version." setup=0".$ordre."\n";
						}
						elseif($version == 2)
						{
							$n = true;
							$nucleo = $nucleo.$raspberry." capteur=".$numero."-".$version." setup=0".$ordre."\n";
						}
					}

					if($a_simple) {$ini = $ini.$arduino_simple."\n";}
					if($a_spi) {$ini = $ini.$arduino_spi."\n";}
					if($n) {$ini = $ini.$nucleo."\n";}

					if(removeRpi($_POST['ip']))
					{
						file_put_contents('upload/inventaire.ini', $ini);

						$retour = array();
						$statut;
						exec("super cmd.sh", $retour, $statut);

						if($statut == 0)
						{
							$message = "Le Raspberry Pi a bien été supprimé !";
							$color = "alert-success";
						}
						else
						{
							$ansible = "Code retour : ".$statut." => ".$errors[$statut]."<br/>";
							$message = "Echec lors de la suppression !";
							$color = "alert-danger";
						}

						$size = count($retour);
						for($i = 0; $i<$size; $i++)
						{
							$ansible = $ansible.$retour[$i]."<br/>";
						}

						?>
						<div class="alert <?php echo $color;?>" role="alert">
							<?php echo $message;?>
						</div>
						<div class="alert alert-secondary" role="alert">
							<?php echo $ansible;?>
						</div>
						<?php
					}
					else
					{
						?>
						<div class="alert alert-danger" role="alert">
					     		Echec lors de la suppression.
						</div>
						<meta http-equiv="Refresh" content="3;url=gestionRaspberry.php" />
						<?php
					}
					?>
        			</div>
			</div>
			<?php
                }
                else
                {
                        include("formConnexion.php");
                }
                ?>
        </body>
</html>

