<?php
require 'db.php';
session_start();



//zabranuje neopravnenemu pristupu na stranku
if ( $_SESSION['logged_inn'] != 1 ) {
  $_SESSION['message'] = "Musis se zalogovat nejdriv!";
  header("location: error.php"); 
}
else{

  

    $query = "SELECT * FROM data WHERE cas BETWEEN (CURDATE() - INTERVAL 30 DAY) AND CURDATE()";
    $result = $mysqli->query($query);
    $chart_data = '';


    while($row = $result->fetch_assoc())
    {
     $chart_data .= "{ datum:'".$row["cas"]."', cislo1:".$row["cislo1"].", cislo2:".$row["cislo2"].", cislo3:".$row["cislo3"].",cislo4:".$row["cislo4"]."}, ";
    }
    $chart_data = substr($chart_data, 0, -2);

    $query2 = "SELECT * FROM data WHERE idd = (SELECT MAX(idd) FROM data)";
    $result2 = $mysqli->query($query2);
    $row2 = $result2->fetch_assoc();
    //print_r($row2);
    //print_r($row2["cas"]);
    //odhlášení
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            if (isset($_POST['log_out'])) {
                session_start();
                session_unset();
                session_destroy();
                header("location: index.php");
            }

    }
//echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>';
}
?>


<!DOCTYPE html>
<html>
 <head>
  <title>iAgriculture</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.css">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
  <link rel="icon" href="ia.ico">
  <link href="signin.css" rel="stylesheet">
  <!--<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>-->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.min.js"></script>  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
  
  <style>
  .boxik {
            border-radius: 10px;
            border-style: solid;
            text-align: center;
            border-color: #e0e3e3;
            border-width: 2px;                       
         }
   #panel { margin-bottom: 1em;}
          
           
  </style>
  
 </head>
 <body>
 <!--navigace-->
    <nav class="navbar navbar-expand-lg navbar-light fixed-top" id="mainNav">
        <a class="navbar-brand font-weight-bold" href="/index.php">iAgriculture</a>
        <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarResponsive">
            <ul class="navbar-nav ml-auto">
                <li class="nav-item ">
                    <span style="margin-right:10px"><?=$_SESSION['email']?></span>
                </li>            
                <li class="nav-item">
                    <form action="chart.php" method="post" >
                        <button class="btn btn-outline-danger btn-sm" type="submit" formmethod="post" name="log_out">Odhlásit</button>
                    </form>
                </li>
            </ul>
        </div>
    </nav>
    <!-- konec navigace-->
 <section class="aplikace">   
  <div class="container col-md-12 col-sm-12 col-xs-12 col-lg-8 bg-light">    
    <div class="container col-md-12">        
        <h2> <span class="badge badge-pill badge-warning">Senzor: <?=$row2["id"]?></span></h2>
        <div class="row" id="panel">
            <div class="col-md-3 boxik"><strong>Poslední měření:</strong><br><strong><?=$row2["cas"]?></strong></div>                        
            <div class="col-md-9 ">
                <div class="row">
                    
                    <div class="col-md-3">
                        <div class=" boxik bg-secondary text-white"><strong>Teplota</strong></div>
                        <div class=" boxik bg-danger text-white" > <h4><?=$row2["cislo4"]?>°C</h4> </div>
                    </div>
                    <div class="col-md-9">
                        <div class="row">
                            <div class="col-md-12 boxik bg-secondary text-white"><strong>Vlhkost půdy</strong></div>
                        </div>
                        <div class="row">
                            <div class=" boxik col-md-4 bg-success "> <h4>horní: <?=$row2["cislo1"]?>%</h4> </div>
                            <div class=" boxik col-md-4 bg-secondary"> <h4>střední: <?=$row2["cislo2"]?>%</h4> </div>
                            <div class=" boxik col-md-4 bg-primary"> <h4>spodní: <?=$row2["cislo3"]?>%</h4> </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row alert alert-dark">
            <form action="export.php" method="post">
            <button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown" >CSV export</button>
            <div class="dropdown-menu">
                <input accesskey="C" class="dropdown-item" type="submit" formmethod="post" name="export30" value="30 dní">
                <input class="dropdown-item" type="submit" formmethod="post" name="export_all" value="All">
            </div>
            </form>
        </div> 
    </div>
    
    <div class="container col-md-12 boxik">      
        <h2 align="center">Graf vlhkosti</h2>     
        <br /><br />
        <div id="chart1"></div>     
    </div>
    <br>
    <div class="container col-md-12 boxik">      
        <h2 align="center">Graf teploty</h2>     
        <br /><br />
        <div id="chart2"></div>              
    </div>
  </div>
 </section>
 </body>
</html> 

<script>
Morris.Line({
 goals: [0, 100],
 element : 'chart1',
 data:[<?php echo $chart_data; ?>],
 xLabels:'day',
 xkey:'datum',
 ykeys:['cislo1', 'cislo2', 'cislo3'],
 labels:['horni', 'stredni', 'spodni'],
 hideHover:'auto',
 stacked:true,
 resize: true,
 postUnits: '%'
});
</script>
<script>
Morris.Area({
 goals: [-15,-10,-5,0,5,10,15,20,25,30,35,40,45],
 ymax:45,
 ymin:-15,
 element : 'chart2',
 data:[<?php echo $chart_data; ?>],
 xLabels:'day',
 xkey:'datum',
 ykeys:['cislo4'],
 labels:['teplota'],
 hideHover:'auto',
 stacked:true,
 resize: true,
 postUnits: '°C',
 fillOpacity: '0.5',
 lineColors: ['red']
});
</script>
