<?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 Tablets</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 = 1;
        $id_user = $_SESSION['UsuarioID'];

// 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">Gerenciar Faturas</h1>
                    </div>
                    <!-- /.col-lg-12 -->
                </div>
                <!-- /.row -->
                <?php
                if (!empty($_GET['msg'])) {
                    
                    echo "<div class='alert alert-success alert-dismissable'>";
                    echo "<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>";
                    if ($_GET['msg'] == 1)
                        echo "Fatura fechada com sucesso! </div>";
                    }
                ?>
                      
                <div class="panel panel-default">
                    <div class="panel-heading">
                        Dados das faturas abertas 
                        <div class="pull-right">
                            <form role="form" action="estado_fatura.php" method="POST">
                            <select name="cliente" id="input-search" required >
                               <option value ="" >
                                       Selecione o cliente
                               </option>
                                <?php
                                
                                $queryList = "SELECT nome, username FROM user WHERE nivel = 3 ORDER BY nome DESC";
                                $resultList = mysql_query($queryList);
                                
                                while ($rowList = mysql_fetch_array($resultList)) {   ?>
                               <option value ="<?php echo $rowList['username'] ?>" ><?php echo $rowList['nome'] ?></option>
                                <?php } ?>
                           </select>
                            <button type="submit" class="btn btn-primary btn-xs">Fechar Faturas</button>
                            </form>
                        </div>
                    </div>
                    <!-- /.panel-heading -->
                    <div class="panel-body">

                        <div class="table-responsive">
                            <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                                <thead>
                                    <tr>
                                        <th class="hidden-xs">#</th>
                                        <th>Descrição acerto</th>
                                        <th>Cliente</th>
                                        <th>Comissão Locação</th>
                                        <th class="hidden-xs">Comissão Dono</th>
                                        <th class="hidden-xs">Comissão Ponto</th>
                                        <th class="hidden-xs">Saldo Total</th>
                                        <th>Status</th>
                                    </tr>
                                </thead>
                                <tbody>
<?php

   if ($_SESSION['UsuarioNivel'] <= 2) {
    $result = mysql_query("SELECT tablet.cliente nome,metricas.id id, tablet.idprod, comissao_ponto,comissao_dono, comissao_locacao, saldo_total,DATE_FORMAT(metricas.dataorder,'%d/%m/%Y')  data_expiracao, metricas.status_leitura FROM metricas, tablet WHERE  metricas.idprod = tablet.idprod AND metricas.status = 0 AND metricas.status_leitura = 1 ORDER BY metricas.dataorder DESC") or die(mysql_error());
}


$i = 1;
while ($row = mysql_fetch_array($result)) {
    

    echo "<tr class='gradeA'><td class='hidden-xs'>" . $i . "</td><td>Tablet " . $row['idprod'] . " - Leitura de " . $row['data_expiracao'] . "</td>"
            . "<td>" . $row['nome'] . "</td><td>$ " . $row['comissao_locacao'] . "</td>"
            . "<td class='hidden-xs'>$ " . $row['comissao_dono'] . "</td>"
            . "<td class='hidden-xs'>$ " . $row['comissao_ponto'] . "</td>"
            . "<td class='hidden-xs'>$ " . $row['saldo_total'] . "</td>";
        if ($row['status_leitura'] == 1) {
            echo "<td class='center'><a href='estado_fatura.php?id=" . $row['id'] . "&opt=2' class='btn btn-success btn-xs' role='button'>ABERTA&nbsp</a></td>";
        } else {
            echo "<td class='center'><a href='estado_fatura.php?id=" . $row['id'] . "&opt=1' class='btn btn-danger btn-xs' role='button'>FECHADA</a></td>";
        }
        echo "</tr>";
    
        $i++;
        
        }
?>
                                </tbody>
                            </table>
                        </div>
                        <!-- /.table-responsive -->
                        <!-- /#page-wrapper -->

                                    <?php include 'newmaquina.php'; ?>


                    </div>
                    <!-- /#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">
        
         </script>       
                
                </body>

                </html>
