<?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 = 6;
        $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 Tablets</h1>
                    </div>
                    <!-- /.col-lg-12 -->
                </div>
                <!-- /.row -->
                <?php
                if (!empty($_GET['msg'])) {
                    
                    
                    if ($_GET['msg'] == 4){
                        
                        $idprod = $_GET['idprod'];
                        
                       echo '<div id="creditoinfo"> ';
                        
                       echo "<div class='alert alert-info alert-dismissable'>";
                       echo '<div class="pull-right"><img src="css/loader.gif"></div> ';
                       echo "Estabelecendo conexão com o tablet...  </div>";
                       
                       echo'</div>';
                       
                    }else {
                    
                    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 "Tablet cadastrado com sucesso! </div>";
                    if ($_GET['msg'] == 2)
                        echo "Tablet deletado com sucesso! </div>";
                    if ($_GET['msg'] == 3)
                        echo "Tablet alterado com sucesso! </div>"; 
                    }
                }
                ?>


  
  <!-- EDITAR TABLET -->
  
                <?php
                if (!empty($_GET['edit'])) {
                    $editar = $_GET['edit'];

                    $resultEdit = mysql_query("SELECT idprod,porcent_acumu, acum1min,acum2min, acum3min, acum4min, acum1med, acum2med, acum3med,acum4med, acum1max, acum2max, acum3max,acum4max, zerar,destrava, validade, 
                    ,id_ponto, ponto.nome,tablet.cliente,tablet.status, pendrive_id FROM tablet, ponto WHERE idprod='" . $editar . "' AND tablet.id_ponto = ponto.id  ORDER BY tablet.id DESC LIMIT 1");
                    $rowEdit = mysql_fetch_array($resultEdit);
                    ?>
                    <div class="row">
                        <div class="col-lg-2 center-block">

                        </div>
                        <div class="col-lg-12 center-block">
                            <form role="form" action="edita_tablet.php" method="POST">
                                <div class="panel panel-default ">
                                    <div class="panel-heading">
                                        Editar Tablet
                                    </div>
                                    
                                    
                                    <div class="panel-body">
                                        <div class="col-lg-4 center-block">
                                        <div class="form-group input-group">
                                            <span class="input-group-addon">Tablet  &nbsp  &nbsp  &nbsp  &nbsp</span>
                                            <input type="text" class="form-control" name="idprod"  <?php echo "value='" . $rowEdit['idprod'] . "'" ?> READONLY  >
                                        </div>

                                        <div class="form-group input-group">    
                                            <span class="input-group-addon"> Dificuldade </span>
                                            <input type="number" min="35" max="95" class="form-control" name="dificuldade" placeholder="Dificuldades" <?php echo "value='" . $rowEdit['dificuldade'] . "'" ?>  <?php if($_SESSION['UsuarioNivel'] != 1) echo "READONLY" ?>>
                                        </div>
                                         </div>
                                        
                                        
                                                                       <?php
                            if ($_SESSION['UsuarioNivel'] == 1) {
                       ?> 
                                        
                                   <div class="col-lg-4 center-block">

                                         <div class="form-group input-group">    
                                            <span class="input-group-addon">Pendrive Nº </span>
                                            <input type="number" min="1" max="100" class="form-control" name="pendrive" placeholder="Numero de Controle" <?php echo "value='" . $rowEdit['pendrive_id'] . "'" ?>  required>
                                        </div>
                                    
                                        <div class="form-group input-group">
                                            <span class="input-group-addon"> Pode Zerar</span>

                                            <select name="zerar" id="zerar" class="form-control">
                                            <?php
                                            if (($rowEdit['zerar']) == 0) {
                                               echo "<option value ='0' >Não</option>
                                                <option value ='1' >Sim</option>";
                                            }
                                            if ($rowEdit['zerar'] == 1) {
                                                echo "<option value ='1' >Sim</option>
                                                <option value ='0' >Não</option>";
                                            }
                                             ?>

                                            </select>
                                        </div> 
                                       
                                                   <div class="form-group input-group">
                                            <span class="input-group-addon">Destravado</span>

                                            <select name="destrava" id="destrava" class="form-control">
                                            <?php
                                            if (($rowEdit['destrava']) == 0) {
                                               echo "<option value ='0' >Não</option>
                                                <option value ='1' >Sim</option>";
                                            }
                                            if ($rowEdit['destrava'] == 1) {
                                                echo "<option value ='1' >Sim</option>
                                                <option value ='0' >Não</option>";
                                            }
                                             ?>

                                            </select>
                                        </div> 
                                </div>
                                     <?php }    ?>      
                         
                             <div class="col-lg-4 center-block">
                             <div class="form-group input-group">
                                            <span class="input-group-addon"> Ponto &nbsp &nbsp &nbsp &nbsp</span>
                                    <select name="ponto" id="ponto" class="form-control">
 
                                        <option <?php echo "value='" . $rowEdit['id_ponto'] . "'" ?>  selected="selected"><?php echo $rowEdit['nome'] ?> </option>

                                        <?php
                                        $resultUser = mysql_query("SELECT ponto.id,ponto.nome FROM ponto, user WHERE  ponto.id_apoio = user.id  AND user.username = '".$rowEdit['cliente']."' AND ponto.nome NOT IN ('".$rowEdit['nome']."') ORDER BY ponto.nome ASC");

                                        while ($rowUser = mysql_fetch_array($resultUser)) {
                                            echo '<option $selecionado value="' . $rowUser['id'] . '">' . $rowUser['nome'] . '</option>';
                                        }
                                        ?>

                                    </select>
                                    </div>
                                        
                               <div class="form-group input-group">    
                                            <span class="input-group-addon">% Acumulado </span>
                                            <input type="number" step="any" min="1" max="3"  class="form-control" name="porcent_acumu" placeholder="Porcentagem Acumulado" <?php echo "value='" . $rowEdit['porcent_acumu'] . "'" ?>  required >
                               </div> 
                                 </div> 
                                        
                              <div class="col-lg-12 center-block">   
                            <div class="panel-group" id="accordion">
                                <div class="row"> 
                              <div class="col-lg-4 center-block"> 
                                <div class="panel panel-default">
                                    <div class="panel-heading">
                                        <h1 class="panel-title">
                                            <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">Acumulado 1 (4 MAG) </a>
                                        </h1>
                                    </div>
                                    <div id="collapseOne" class="panel-collapse collapse in">
                                        <div class="panel-body">
                                         <div class="form-group input-group">    
                                            <span class="input-group-addon">Inicial &nbsp  </span>
                                            <input type="number" min="1" max="10000" class="form-control" name="acum4min" placeholder="Inicial" <?php echo "value='" . $rowEdit['acum4min'] . "'" ?> required >
                                         </div>
                                         <div class="form-group input-group">    
                                            <span class="input-group-addon">Atual &nbsp &nbsp </span>
                                            <input type="number" min="1" max="10000" class="form-control" name="acum4med" placeholder="Atual" <?php echo "value='" . $rowEdit['acum4med'] . "'" ?>  required>
                                         </div>
                                             <div class="form-group input-group">    
                                            <span class="input-group-addon">Máximo </span>
                                            <input type="number" min="1" max="10000" class="form-control" name="acum4max" placeholder="Máximo" <?php echo "value='" . $rowEdit['acum4max'] . "'" ?> required >
                                         </div>
                                        </div>
                                    </div>
                                </div>
                              </div>
							  
                                <div class="col-lg-4 center-block"> 
                                <div class="panel panel-default">
                                    <div class="panel-heading">
                                        <h1 class="panel-title">
                                            <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">Acumulado 2 (4 ABO) </a>
                                        </h1>
                                    </div>
                                    <div id="collapseTwo" class="panel-collapse collapse in">
                                          <div class="panel-body">
                                         <div class="form-group input-group">    
                                            <span class="input-group-addon">Inicial &nbsp  </span>
                                            <input type="number" min="1" max="10000" class="form-control" name="acum1min" placeholder="Inicial" <?php echo "value='" . $rowEdit['acum1min'] . "'" ?>  required>
                                         </div>
                                         <div class="form-group input-group">    
                                            <span class="input-group-addon">Atual &nbsp &nbsp </span>
                                            <input type="number" min="1" max="10000" class="form-control" name="acum1med" placeholder="Atual" <?php echo "value='" . $rowEdit['acum1med'] . "'" ?>  required>
                                         </div>
                                             <div class="form-group input-group">    
                                            <span class="input-group-addon">Máximo </span>
                                            <input type="number" min="1" max="10000" class="form-control" name="acum1max" placeholder="Máximo" <?php echo "value='" . $rowEdit['acum1max'] . "'" ?>  required>
                                         </div>
                                        </div>
                                    </div>
                                </div>
                                </div>
								
						 <div class="col-lg-4 center-block"> 
                                <div class="panel panel-default">
                                    <div class="panel-heading">
                                        <h5 class="panel-title">
                                            <a data-toggle="collapse" data-parent="#accordion" href="#collapseThree">Acumulado 3 (5 CAP) </a>
                                        </h5>
                                    </div>
                                    <div id="collapseThree" class="panel-collapse collapse in">
                                          <div class="panel-body">
                                         <div class="form-group input-group">    
                                            <span class="input-group-addon">Inicial &nbsp  </span>
                                            <input type="number" min="1" max="10000" class="form-control" name="acum2min" placeholder="Inicial" <?php echo "value='" . $rowEdit['acum2min'] . "'" ?> required >
                                         </div>
                                         <div class="form-group input-group">    
                                            <span class="input-group-addon">Atual &nbsp &nbsp </span>
                                            <input type="number" min="1" max="10000" class="form-control" name="acum2med" placeholder="Atual" <?php echo "value='" . $rowEdit['acum2med'] . "'" ?> required >
                                         </div>
                                             <div class="form-group input-group">    
                                            <span class="input-group-addon">Máximo </span>
                                            <input type="number" min="1" max="10000" class="form-control" name="acum2max" placeholder="Máximo" <?php echo "value='" . $rowEdit['acum2max'] . "'" ?> required >
                                         </div>
                                        </div>
                                    </div>
                                </div>
								
                                
                            </div>
							</div>
							</div>
							</div>
							
							<div class="col-lg-12 center-block">   
                            <div class="panel-group" id="accordion">
							<div class="row"> 
                                <div class="col-lg-4 center-block"> 
                                <div class="panel panel-default">
                                    <div class="panel-heading">
                                        <h5 class="panel-title">
                                            <a data-toggle="collapse" data-parent="#accordion" href="#collapseThree">Acumulado 4 (5 ABO) </a>
                                        </h5>
                                    </div>
                                    <div id="collapseThree" class="panel-collapse collapse in">
                                          <div class="panel-body">
                                         <div class="form-group input-group">    
                                            <span class="input-group-addon">Inicial &nbsp  </span>
                                            <input type="number" min="1" max="10000" class="form-control" name="acum3min" placeholder="Inicial" <?php echo "value='" . $rowEdit['acum3min'] . "'" ?> required >
                                         </div>
                                         <div class="form-group input-group">    
                                            <span class="input-group-addon">Atual &nbsp &nbsp </span>
                                            <input type="number" min="1" max="10000" class="form-control" name="acum3med" placeholder="Atual" <?php echo "value='" . $rowEdit['acum3med'] . "'" ?> required >
                                         </div>
                                             <div class="form-group input-group">    
                                            <span class="input-group-addon">Máximo </span>
                                            <input type="number" min="1" max="10000" class="form-control" name="acum3max" placeholder="Máximo" <?php echo "value='" . $rowEdit['acum3max'] . "'" ?> required >
                                         </div>
                                        </div>
                                    </div>
                                </div>
                            
                            </div>
                        </div>
                       </div>
                     </div>
                                        
                        <!-- .panel-body -->
                                        
                                        
                        
                                        
                                        <div class="pull-right">
                                            <a href="tablets.php" class="btn btn-default" role="button">Cancelar</a>
                                             <?php echo " <button type='submit' class='btn btn-primary'>Salvar</button>"; ?>
                                            </form>

                                        </div>
                                    </div>

                                </div>



                        </div>
                    </div>

                    <!-- /.col-lg-4 -->
    <?php
}
?>                

 <!-- /. EDITAR TABLET -->

  <!-- DELETAR TABLET -->
                <?php
                if (!empty($_GET['id'])) {
                    $deletar = $_GET['id'];
                    ?>
                    <div class="row">
                        <div class="col-lg-3 center-block">

                        </div>
                        <div class="col-lg-6 center-block">
                            <div class="panel panel-danger ">
                                <div class="panel-heading">
                                    Deletar Tablet
                                </div>
                                <div class="panel-body">
                                    <p>Você realmente deseja deletar o Tablet?</p>
                                </div>
                                <div class="panel-footer">.
                                    <div class="pull-right">

                                        <a href="tablets.php" class="btn btn-default btn-xs" role="button">Não</a>
    <?php echo "<a href='deleta_tablet.php?id=" . $deletar . "'  class='btn btn-danger btn-xs' role='button'>Sim</a>"; ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- /.col-lg-4 -->
    <?php
}
?>

 <!-- /. DELETAR TABLET -->
                    
 
 
 
 <!-- INSERIR CRÉDITOS -->
  
                <?php
                if (!empty($_GET['insert'])) {
                    $idprod = $_GET['insert'];

                    ?>
                    <div class="row">
                        <div class="col-lg-2 center-block">

                        </div>
                        <div class="col-lg-8 center-block">
                            <form role="form" action="add_creditos.php?tipo=1" method="POST">
                                <div class="panel panel-default ">
                                    <div class="panel-heading">
                                        Inserir crédito
                                    </div>
                                    <div class="panel-body">
                                        <div class="form-group input-group">
                                            <span class="input-group-addon">Tablet  &nbsp  &nbsp  &nbsp  &nbsp</span>
                                            <input type="text" class="form-control" name="idprod"  <?php echo "value='" . $idprod . "'" ?> READONLY  >
                                        </div>
                                        <div class="form-group input-group">    
                                            <span class="input-group-addon"> Valor   &nbsp &nbsp $</span>
                                            <input type="number" min="1" max="1000" class="form-control" name="valor" placeholder="Digite o valor do crédito" >
                                        </div>
                                        

                                        <div class="pull-right">

                                            <a href="tablets.php" class="btn btn-danger" role="button">Cancelar</a>
                                             <?php echo " <button type='submit' class='btn btn-success'>Enviar</button>"; ?>
                                            </form>

                                        </div>
                                    </div>

                                </div>



                        </div>
                    </div>

                    <!-- /.col-lg-4 -->
    <?php
}
?>                

 <!-- /. INSERIR CRÉDITOS -->

 
 
 
 
                    
                    
                <div class="panel panel-default">
                    <div class="panel-heading">
                        Dados dos tablets cadastrados 
                        <div class="pull-right">
                            
                <?php
                
                if ($_SESSION['UsuarioNivel'] != 6){
                
                $resultConfig = mysql_query("SELECT ligado FROM configuracao ORDER BY id ASC LIMIT 1");
                $rowConfig = mysql_fetch_array($resultConfig);
                $ligado = $rowConfig['ligado'];
                    if ($ligado == 1) {
                        echo "<a href='estado_server.php?opt=0' class='btn btn-success btn-xs' role='button'>SERVIDOR ONLINE &nbsp</a>";
                    } else {
                        echo "<a href='estado_server.php?opt=1' class='btn btn-danger btn-xs' role='button'>SERVIDOR OFFLINE</a>";
                    }
                }
                ?>      
                        </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>Tablet</th>
                                        <th>Cliente</th>
                                        <th>Ponto</th>
                                        <th class="hidden-xs">Data Cadastro</th>
                                        <th class="hidden-xs">Dificuldade</th>
                                        <th>Servidor</th>
                                        <th>Créditos</th>
<?php if ($_SESSION['UsuarioNivel'] != 5) echo '<th>Ações</th>'; ?>
                                    </tr>
                                </thead>
                                <tbody>
<?php


if ($_SESSION['UsuarioNivel'] == 1){
    $resultUser = mysql_query("SELECT tablet.id,tablet.idprod,cliente,ponto.nome ponto,tablet.cadastro cadastro, validade, ligado,expiracao, dificuldade FROM tablet,ponto WHERE tablet.id_ponto = ponto.id ORDER BY tablet.id DESC");
}
if ($_SESSION['UsuarioNivel'] == 2){
    $resultUser = mysql_query("SELECT tablet.id,tablet.idprod,tablet.cliente,ponto.nome ponto,tablet.cadastro cadastro, tablet.validade, tablet.ligado,tablet.expiracao, tablet.dificuldade FROM tablet,ponto,user WHERE tablet.id_ponto = ponto.id AND tablet.cliente = user.username AND user.id_apoio = ". $_SESSION['UsuarioID'] ."  ORDER BY tablet.id DESC");
}

if ($_SESSION['UsuarioNivel'] == 3){
    $resultUser = mysql_query("SELECT tablet.id,tablet.idprod,cliente,ponto.nome ponto,tablet.cadastro cadastro, validade, ligado,expiracao, dificuldade FROM tablet,ponto WHERE tablet.id_ponto = ponto.id  AND tablet.id_apoio = ". $id_user ." ORDER BY tablet.id DESC");
}

if ($_SESSION['UsuarioNivel'] == 4){
    $resultUser = mysql_query("SELECT tablet.id,tablet.idprod,cliente,ponto.nome ponto,tablet.cadastro cadastro, validade, ligado,expiracao, dificuldade FROM tablet,ponto WHERE tablet.id_ponto = ponto.id AND tablet.id_apoio = ". $id_user ."  ORDER BY  tablet.id DESC");
}

if (($_SESSION['UsuarioNivel'] == 5) OR ($_SESSION['UsuarioNivel'] == 6)){
    $resultUser = mysql_query("SELECT tablet.id,tablet.idprod,tablet.cliente,ponto.nome ponto,tablet.cadastro cadastro, tablet.validade, tablet.ligado, tablet.dificuldade, tablet.expiracao FROM tablet,ponto, user WHERE tablet.id_ponto = ponto.id AND ponto.id = user.id_apoio AND user.id = ".$_SESSION['UsuarioID']." ORDER BY  tablet.id DESC");
}
 $i = 1;
while ($rowUser = mysql_fetch_array($resultUser)) {

    echo "<tr class='gradeA'><td class='hidden-xs'>" . $i . "</td><td>" . $rowUser['idprod'] . "</td><td>" . $rowUser['cliente'] . "</td><td>" . $rowUser['ponto'] . "</td><td class='hidden-xs'>" . $rowUser['cadastro'] . "</td><td class='hidden-xs'>" . $rowUser['dificuldade'] . " %</td>";
    //if ($_SESSION['UsuarioNivel'] == 1) {
        if ($rowUser['ligado'] == 1) {
            echo "<td class='center'><a href='estado_server.php?id=" . $rowUser['idprod'] . "&opt=0' class='btn btn-success btn-xs' role='button'>ONLINE&nbsp</a></td>";
        } else {
            echo "<td class='center'><a href='estado_server.php?id=" . $rowUser['idprod'] . "&opt=1' class='btn btn-danger btn-xs' role='button'>OFFLINE</a></td>";
        }
        echo "<td class='center'><a href='tablets.php?insert=" . $rowUser['idprod'] . "' class='btn btn-success btn-xs' role='button'><i class='fa fa-upload fa-fw'></i></a> <a href='add_creditos.php?idprod=" . $rowUser['idprod'] . "&tipo=2' class='btn btn-danger btn-xs' role='button'><i class='fa fa-download fa-fw'></i></a> </td>";
        if ($_SESSION['UsuarioNivel'] != 5) echo "<td class='center'><a href='detalha_tablet.php?id=" . $rowUser['idprod'] . "' class='btn btn-primary btn-xs' role='button'><i class='fa fa-file fa-fw'></i></a>";
        if (($_SESSION['UsuarioNivel'] != 6) && ($_SESSION['UsuarioNivel'] != 5 )) echo " <a href='tablets.php?edit=" . $rowUser['idprod'] . "' class='btn btn-primary btn-xs' role='button'><i class='fa fa-edit fa-fw'></i></a> ";
        if ($_SESSION['UsuarioNivel'] == 1) echo " <a href='tablets.php?id=" . $rowUser['idprod'] . "' class='btn btn-primary btn-xs' role='button'><i class='fa fa-times fa-fw'></i></a> </td></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">
        
                var auto_refresh = setInterval(
function ()
{
$('#creditoinfo').load('creditoinfo.php', { 'idprod': '<?php echo $idprod ?>'  } ).fadeIn("slow");
}, 1000); // refresh every 10000 milliseconds
         </script>       
                
                </body>

                </html>
