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

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

?>
<?php
session_start();
error_reporting(0);
?>

<!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 = 5;

// 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 Pontos</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 "Ponto cadastrado com sucesso! </div>";
                    if ($_GET['msg'] == 2)
                        echo "Ponto deletado com sucesso! </div>";
                    if ($_GET['msg'] == 3)
                        echo "Ponto alterado com sucesso! </div>";
                }
                ?>


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

                    $resultEdit = mysql_query("SELECT ponto.nome nome,porcent_ponto,ponto.id_apoio id_apoio, user.username apoio  FROM ponto, user WHERE user.id = ponto.id_apoio AND ponto.id = $editar");
                    $rowEdit = mysql_fetch_array($resultEdit);
                    ?>
                    <div class="row">
                        <div class="col-lg-2 center-block">

                        </div>
                        <div class="col-lg-8 center-block">
                            <form role="form" action="edita_ponto.php" method="POST">
                                <div class="panel panel-default ">
                                    <div class="panel-heading">
                                        Editar Ponto
                                    </div>

                                    <div class="panel-body">

                                          <?php
                            if ($_SESSION['UsuarioNivel'] == 1) {
                       ?> 
                             <label>Selecione um Cliente</label>
                                <div class="form-group">
                                    <select name="apoio" id="apoio" class="form-control">
 
                                        <option <?php echo "value='" . $rowEdit['id_apoio'] . "'" ?>  selected="selected"><?php echo $rowEdit['apoio'] ?> </option>

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

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

                                    </select>
                                    </div>
                          <?php }    ?>  
                                        
                                        
                                        <div class="form-group input-group">
                                            <span class="input-group-addon"> <i class="fa fa-user fa-fw"></i></span>
                                            <input type="text" class="form-control" name="nome" placeholder="Nome" <?php echo "value='" . $rowEdit['nome'] . "'" ?> autofocus required >
                                            <input type="hidden" class="form-control" name="id"  <?php echo "value='" . $editar . "'" ?>>

                                        </div>
                   
                                        <div class="form-group input-group">
                                            <span class="input-group-addon"> % </span>
                                            <input type="number" min="0" max="100" class="form-control" name="porcent_ponto" placeholder="Porcentagem Ponto" <?php echo "value='" . $rowEdit['porcent_ponto'] . "'" ?> autofocus required >
                                        </div>
                                        <div class="pull-right">

                                            <a href="pontos.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
                }
                ?>                
                
                
                
<?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 Ponto
                                </div>
                                <div class="panel-body">
                                    <p>Você realmente deseja deletar o Ponto?</p>
                                </div>
                                <div class="panel-footer">.
                                    <div class="pull-right">

                                        <a href="pontos.php" class="btn btn-default btn-xs" role="button">Não</a>
    <?php echo "<a href='deleta_ponto.php?id=" . $deletar . "'  class='btn btn-danger btn-xs' role='button'>Sim</a>"; ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!-- /.col-lg-4 -->
    <?php
}
?>



                <div class="panel panel-default">
                    <div class="panel-heading">
                        Dados dos pontos cadastrados 
              <?php if ( ($_SESSION['UsuarioNivel'] == 1) OR ($_SESSION['UsuarioNivel'] == 3) OR ($_SESSION['UsuarioNivel'] == 4)){ ?>
                        <div class="pull-right">
                            <button class="btn btn-primary btn-xs"  data-toggle="modal" data-target="#newponto">
                                Novo ponto
                            </button>
                        </div>
              <?php } ?>
                    </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>#</th>
                                        <th>Nome</th>
                                         <?php
                                        if ($_SESSION['UsuarioNivel'] <= 2) {
                                            echo "<th>Cliente</th>";
                                        }
                                        ?> 
                                        <th>% Ponto</th>
                                        <th>Data Registro</th>
                                        <?php
                                        if ($_SESSION['UsuarioNivel'] != 2) {
                                            echo "<th>Ações</th>";
                                        }
                                        ?> 
                                    </tr>
                                </thead>
                                
                                <tbody>
                                    <?php
                                     if ($_SESSION['UsuarioNivel'] == 1){
                                       $resultUser = mysql_query("SELECT ponto.id id,ponto.nome nome,DATE_FORMAT(ponto.cadastro,'%d-%m-%Y %H:%i') cadastro, porcent_ponto, user.username cliente FROM ponto, user WHERE  ponto.id_apoio = user.id ORDER BY id DESC");
                                     } elseif ($_SESSION['UsuarioNivel'] == 2){
                                       $resultUser = mysql_query("SELECT ponto.id id,ponto.nome nome,DATE_FORMAT(ponto.cadastro,'%d-%m-%Y %H:%i') cadastro, porcent_ponto, user.username cliente FROM ponto, user WHERE  ponto.id_apoio = user.id AND user.id_apoio = ". $_SESSION['UsuarioID'] ." ORDER BY id DESC");

                                     }else{
                                       
                                        $resultUser = mysql_query("SELECT id,nome,DATE_FORMAT(cadastro,'%d-%m-%Y %H:%i') cadastro, porcent_ponto FROM ponto WHERE id_apoio = ".$_SESSION['UsuarioID']." ORDER BY id DESC");

                                     }
                                    
                                $i = 1;
                                    while ($rowUser = mysql_fetch_array($resultUser)) {
                                        echo "<tr class='gradeA'><td>" . $i . "</td><td>" . $rowUser['nome'] . "</td>";
                                        if  ($_SESSION['UsuarioNivel'] <= 2) echo "<td>" . $rowUser['cliente'] . "</td>";
                                        echo "<td>" . $rowUser['porcent_ponto'] . "</td><td>" . $rowUser['cadastro'] . "</td>";
                                       if  ($_SESSION['UsuarioNivel'] != 2){
                                           echo "<td class='center'><a href='arquivo_pendrive.php?id=" . $rowUser['id'] . "' class='btn btn-primary btn-xs'  title='Arquivos Pendrive' role='button'><i class='fa fa-file fa-fw'></i></a> <a href='pontos.php?edit=" . $rowUser['id'] . "' class='btn btn-primary btn-xs' title='Editar Ponto' role='button'><i class='fa fa-edit fa-fw'></i></a> <a href='pontos.php?id=" . $rowUser['id'] . "' class='btn btn-primary btn-xs' title='Excluir Ponto' role='button'><i class='fa fa-times fa-fw'></i></a></td></tr> ";
                                       }
                                $i++;
                                    }
                                    ?>
                                </tbody>
                                
                            </table>
                        </div>
                        <!-- /.table-responsive -->
                        <!-- /#page-wrapper -->

<?php include 'newponto.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>
    // tooltip demo
    $('.tooltip-demo').tooltip({
        selector: "[data-toggle=tooltip]",
        container: "body"
    })

    // popover demo
    $("[data-toggle=popover]")
        .popover()
    </script>
                </body>

                </html>
