<?php
$filename = 'c_leitura.php';

if (!file_exists($filename)) {
    header("Location: error.php");
    exit;
}
include 'c_leitura.php';
?>
<?php
session_start();
?>

<!DOCTYPE html>
<html>

    <head>

        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">

        <title>Gerenciamento de Tablet</title>

        <!-- Core CSS - Include with every page -->
        <link href="css/bootstrap.min.css" rel="stylesheet">
        <link href="font-awesome/css/font-awesome.css" rel="stylesheet">

        <!-- Page-Level Plugin CSS - Blank -->

        <!-- SB Admin CSS - Include with every page -->
        <link href="css/sb-admin.css" rel="stylesheet">

    </head>

    <body>

        <?php
        $nivel_necessario = 6;

// Verifica se não há a variável da sessão que identifica o usuário
        if (!isset($_SESSION['UsuarioID']) OR ( $_SESSION['UsuarioNivel'] > $nivel_necessario)) {
// Destrói a sessão por segurança
            session_destroy();
// Redireciona o visitante de volta pro login
            header("Location: index.php");
            exit;
        }

        require_once("db.php");
        ?>
        <div id="wrapper">

            <nav class="navbar navbar-default navbar-fixed-top" role="navigation" style="margin-bottom: 0">
                <?php include 'header.php'; ?>
                <?php include 'navbar.php'; ?>
            </nav>

            <div id="page-wrapper">
                <div class="row">
                    <div class="col-lg-12">
                        <h1 class="page-header">Detalhamento do Tablet</h1>
                    </div>
                    <!-- /.col-lg-12 -->
                </div>
                <!-- /.row -->
                <?php
                if (!empty($_GET['id'])) {

                    $idprod = $_GET['id'];


                    $result = mysql_query("SELECT entrada,saida,entrada_virtual,saida_virtual,data,pendrive_id "
                            . " FROM leitura, tablet WHERE leitura.idprod = '$idprod' "
                            . " AND leitura.idprod = tablet.idprod") or die(mysql_error());
                    $row = mysql_fetch_array($result);



                    $creditos = strtoupper(substr(md5($idprod . $row['pendrive_id']), 0, 4));
                    $parametros = md5($creditos);


                    $link1 = "gerfile.php?file=pendrive.cre&text=" . $creditos;
                    $link2 = "gerfile.php?file=pendrive.par&text=" . $parametros;
                    ?>


                    <?php
                    if ($_SESSION['UsuarioNivel'] != 6) {
                        ?> 
                        <div class="col-lg-4">
                            <div class="panel panel-default">
                                <div class="panel-heading">
                                    <i class="fa fa-paste fa-fw"></i> Arquivos Pendrive
                                </div>
                                <!-- /.panel-heading -->
                                <div class="panel-body">
                                    <div class="list-group">
                                        <a href="<?php echo $link1 ?>" class="list-group-item">
                                            <i class="fa fa-money fa-fw"></i> Créditos
                                            <span class="pull-right text-muted small"><em>Download</em>
                                            </span>
                                        </a>
                                        <pre> <?php echo $creditos ?></pre>
                                        <a href="<?php echo $link2 ?>" class="list-group-item">
                                            <i class="fa fa-gear fa-fw"></i> Parâmetros
                                            <span class="pull-right text-muted small"><em>Download</em>
                                            </span>
                                        </a>
                                        <pre><?php echo $parametros ?></pre>
                                    </div>
                                    <!-- /.list-group -->
                                </div>
                                <!-- /.panel-body -->
                            </div>
                            <!-- /.panel -->
                        </div>


                        <?php
                    }
                    ?> 

                    <div id='leiturainfo'>
                        <div class="col-lg-8">
                            <div class="panel panel-default">
                                <div class="panel-heading">
                                    Informações sobre Total da Leitura
                                </div>
                                <!-- /.panel-heading -->
                                <div class="panel-body">
                                    <div class="table-responsive">
                                        <table class="table ">
                                            <thead>
                                                <tr>
                                                    <th class=" text-muted text-center"><h4>Data</h4></th>
                                                    <th class=" text-success text-center"><h4>Entrada</h4></th>
                                                    <th class=" text-danger text-center"><h4>Saida</h4></th>
                                                    <th class=" text-info text-center"><h4>Balanço</h4></th>
                                                </tr>
                                            </thead>

                                            <tbody>
                                                <tr>
                                                    <td class=" text-muted text-center"><h4><?php echo $row['data'] ?></h4></td>
                                                    <td class=" text-success text-center"><h4>$ <?php echo $row['entrada'] ?></h4></td>
                                                    <td class=" text-danger text-center"><h4>$ <?php echo $row['saida'] ?></h4></td>
                                                    <td class=" text-info text-center"><h4>$ <?php echo $row['entrada'] - $row['saida'] ?> </h4></td>
                                                </tr>
                                                <tr>
                                                    <td class=" text-muted text-center"><h4><?php echo $row['data'] ?></h4></td>
                                                    <td class=" text-success text-center"><h4>$ <?php echo $row['entrada_virtual'] ?></h4></td>
                                                    <td class=" text-danger text-center"><h4>$ <?php echo $row['saida_virtual'] ?></h4></td>
                                                    <td class=" text-info text-center"><h4>$ <?php echo $row['entrada_virtual'] - $row['saida_virtual'] ?> </h4></td>
                                                </tr>
                                            </tbody>

                                        </table>
                                    </div>
                                    <!-- /.table-responsive -->
                                </div>
                                <!-- /.panel-body -->
                            </div>
                            <!-- /.panel -->
                        </div>
                        <!-- /.col-lg-6 -->

                    </div>

                <?php } ?>



                <!-- /#wrapper -->
            </div>
            <!-- Core Scripts - Include with every page -->
            <script src="js/jquery-1.10.2.js"></script>
            <script src="js/bootstrap.min.js"></script>
            <script src="js/plugins/metisMenu/jquery.metisMenu.js"></script>

            <!-- Page-Level Plugin Scripts - Blank -->
            <link href="css/plugins/dataTables/dataTables.bootstrap.css" rel="stylesheet">
            <script src="js/plugins/dataTables/jquery.dataTables.js"></script>
            <script src="js/plugins/dataTables/dataTables.bootstrap.js"></script>
            <!-- SB Admin Scripts - Include with every page -->
            <script src="js/sb-admin.js"></script>

            <!-- Page-Level Demo Scripts - Blank - Use for reference -->
            <script>
                $(document).ready(function () {
                    $('#dataTables-example').dataTable();
                });
            </script>
            <script type="text/javascript">

                var auto_refresh = setInterval(
                        function ()
                        {
                            $('#leiturainfo').load('leiturainfo.php', {'idprod': '<?php echo $idprod ?>'}).fadeIn("slow");
                        }, 1000); // refresh every 10000 milliseconds
            </script>   
    </body>

</html>
