<?php
include("seguranca.php"); // Inclui o arquivo com o sistema de segurança  
protegePagina(); // Chama a função que protege a página 
?>
<?php
include "conexao.php";
?>
<!DOCTYPE html>
<html lang="en">
    <head>        
        <!-- META SECTION -->
        <title>Portal dos Patrocinadores - Sistema Administrativo</title>            
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        
        <link rel="icon" href="favicon.ico" type="image/x-icon" />
        <!-- END META SECTION -->
        
        <!-- CSS INCLUDE -->        
        <link rel="stylesheet" type="text/css" id="theme" href="css/theme-default.css"/>
        <!-- EOF CSS INCLUDE -->                                     

    <!-- START SCRIPTS -->
        <!-- START PLUGINS -->


    </head>
    <body>
        <!-- START PAGE CONTAINER -->
        <div class="page-container">
            
        	<?php include 'topo.php'; ?>
            <!-- END PAGE SIDEBAR -->

		
            
            <!-- PAGE CONTENT --><!-- END X-NAVIGATION VERTICAL -->                     
                
                <!-- START BREADCRUMB -->
                <!-- END BREADCRUMB -->                
                <!-- PAGE CONTENT WRAPPER -->
                <div class="page-content-wrap">
                
                
                <div class="col-md-12">
                

          <form action="add_usuario.php" method="post" enctype="multipart/form-data" target="_self">
                

                            <div class="panel panel-default">
                                <div class="panel-heading">                                
                                    <h3 class="panel-title">Cadastro Usuário</h3>
                                    <ul class="panel-controls">
                                        <li><a href="#" class="panel-collapse"><span class="fa fa-angle-down"></span></a></li>
								        <li><a href="#" class="panel-fullscreen"><span class="fa fa-expand"></span></a></li>
                                    </ul>
                                    
                                                                    
                                </div>
                                <div class="panel-body">



             <div class="col-md-6">
              <div class="form-group">
                <label>Nome</label>
                <input type="text" name="nome" class="form-control" placeholder="Nome" id="nome" required="required" />         
              </div>
            </div>

            <div class="col-md-6">
                <div class="form-group">
                    <label>Sobrenome</label>
                    <input type="text" name="sobrenome" class="form-control" placeholder="Sobrenome" id="sobrenome" required="required" />
                </div>
            </div>

             <div class="col-md-6">
              <div class="form-group">
                <label>E-mail</label>
                <input type="text" name="email" class="form-control" placeholder="E-mail" id="email" required="required" />
              </div>
            </div>

             <div class="col-md-6">
              <div class="form-group">
                <label>Senha</label>
                <input type="password" name="senha" class="form-control" placeholder="Senha" id="senha" required="required" />         
              </div>
            </div>


            <div class="col-md-6">
                <div class="form-group">
                    <label>Data Limite</label>
                    <input type="text" name="limite_assinatura" class="form-control data" placeholder="Data Limite" id="limite_assinatura" required="required" />
                </div>
            </div>
			

                                </div>
                                
					<div class="panel-footer">
					   <button type="submit" class="btn btn-primary pull-right"><span class="fa fa-plus"></span> Adicionar</button>
				    </div>

                                
                            </div>


				</form>




                            <!-- START DEFAULT DATATABLE -->
                            <div class="panel panel-default">
                                <div class="panel-heading">                                
                                    <h3 class="panel-title">Usuários</h3>

                                    <ul class="panel-controls">
                                        <li><a href="#" class="panel-collapse"><span class="fa fa-angle-down"></span></a></li>
								        <li><a href="#" class="panel-fullscreen"><span class="fa fa-expand"></span></a></li>
                                    </ul>                                
                                </div>
                                <div class="panel-body">
                                    <div class="table-responsive">
                                        <table class="table tabledata">
                <thead>
                <tr>
                    <th>Nome</th>
                    <th>E-mail</th>
                    <th>Data Limite</th>
                    <th>&nbsp;</th>
                    <th>&nbsp;</th>
                  </tr>
                </thead>
                <tbody>
				<?php
				$sql_consult2 = $sqli->query("SELECT * FROM usuarios WHERE deleted_at IS NULL ORDER BY nome ASC");
				while ($dados = $sql_consult2->fetch_assoc()) {
				$id = $dados['id'];

				if($dados['limite_assinatura']) {
				    $limite_assinatura = date('d/m/Y', strtotime($dados['limite_assinatura']));
                }
				else {
                    $limite_assinatura = "--";
                }
				

				?>
                <tr>
                    <td><?php echo $dados['nome'] ?> <?php echo $dados['sobrenome'] ?></td>
                    <td><?php echo $dados['email'] ?></td>
                    <td><?php echo $limite_assinatura ?></td>
                    <td><a data-toggle="modal" data-target="#pop_edita_<?php echo $id ?>"><button class="btn btn-sm btn-primary"><span class="fa fa-search"></span></button></a>
                    </td>
                    <td><a data-toggle="modal" data-target="#pop_excluir_<?php echo $id ?>"><button class="btn btn-sm btn-primary"><span class="fa fa-trash-o"></span></button></a>
                    </td>
                </tr>

             
                  <?php
					}
					?>
                </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                            <!-- END DEFAULT DATATABLE -->

				
                </div>


                </div>
                <!-- END PAGE CONTENT WRAPPER -->                
            </div>            
            <!-- END PAGE CONTENT -->
        </div>
        <!-- END PAGE CONTAINER -->









        <?php
        $sql_consult2 = $sqli->query("SELECT * FROM usuarios WHERE deleted_at IS NULL ORDER BY nome ASC");
        while ($dados = $sql_consult2->fetch_assoc()) {
        $id = $dados['id'];

        if($dados['limite_assinatura']) {
            $limite_assinatura = date('d/m/Y', strtotime($dados['limite_assinatura']));
        }
        else {
            $limite_assinatura = "--";
        }


        ?>







        <div class="modal modal-primary" id="pop_excluir_<?php echo $id ?>">
            <form class="form-delete-user" data-popup="pop_excluir_<?php echo $id ?>" method="post" enctype="multipart/form-data" target="_self">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">×</span></button>
                            <h4 class="modal-title">Excluir Usuário</h4>
                        </div>
                        <div class="modal-body">

                            <div class="row">

                                <div class="col-md-12">
                                    <p>Tem certeza que deseja excluir o usuário <?php echo $dados['nome'] ?> <?php echo $dados['sobrenome'] ?>?</p>
                                </div>

                                <input type="hidden" value="<?php echo $id ?>" name="id" id="id" />

                            </div>
                        </div>
                        <div class="modal-footer">
                            <input type="submit" class="btn btn-primary pull-right" value="Sim" />
                            <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Não</button>
                        </div>
                    </div>
                    <!-- /.modal-content -->
                </div>
                <!-- /.modal-dialog -->
            </form>
        </div>



        <div class="modal modal-primary" id="pop_edita_<?php echo $id ?>">
            <form class="form-update-user" data-popup="pop_edita_<?php echo $id ?>" method="post" enctype="multipart/form-data" target="_self">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">×</span></button>
                            <h4 class="modal-title">Editar Usuário</h4>
                        </div>
                        <div class="modal-body">

                            <div class="row">

                                <input type="hidden" value="<?php echo $id ?>" name="id" id="id" />

                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label>Nome</label>
                                        <input type="text" name="nome" class="form-control" placeholder="Nome" id="nome" required="required" value="<?php echo $dados['nome'] ?>" />
                                    </div>
                                </div>

                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label>Sobrenome</label>
                                        <input type="text" name="sobrenome" class="form-control" placeholder="Sobrenome" id="sobrenome" required="required" value="<?php echo $dados['sobrenome'] ?>" />
                                    </div>
                                </div>

                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label>E-mail</label>
                                        <input type="text" name="email" class="form-control" placeholder="E-mail" id="email" required="required" value="<?php echo $dados['email'] ?>" disabled />
                                    </div>
                                </div>

                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label>Data Limite</label>
                                        <input type="text" name="limite_assinatura" class="form-control data" placeholder="Data Limite" id="data_limite" required="required" value="<?php echo $limite_assinatura ?>" />
                                    </div>
                                </div>


                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label>Alterar Senha</label>
                                        <input type="password" name="senha" class="form-control" placeholder="Senha" id="senha" />
                                    </div>
                                </div>


                            </div>
                        </div>
                        <div class="modal-footer">
                            <input type="submit" class="btn btn-primary pull-right" value="Atualizar" />
                            <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Fechar</button>
                        </div>
                    </div>
                    <!-- /.modal-content -->
                </div>
                <!-- /.modal-dialog -->
            </form>
        </div>




            <?php
        }
        ?>









        <!-- END TEMPLATE -->
    <!-- END SCRIPTS -->

        <script type="text/javascript" src="js/plugins/jquery/jquery.min.js"></script>
        <script type="text/javascript" src="js/plugins/jquery/jquery-ui.min.js"></script>
        <script type="text/javascript" src="js/plugins/bootstrap/bootstrap.min.js"></script>
        <!-- END PLUGINS -->

        <!-- THIS PAGE PLUGINS -->
        <script type='text/javascript' src='js/plugins/icheck/icheck.min.js'></script>
        <script type="text/javascript" src="js/plugins/mcustomscrollbar/jquery.mCustomScrollbar.min.js"></script>

        <script type="text/javascript" src="js/plugins/datatables/jquery.dataTables.min.js"></script>




        <!-- END PAGE PLUGINS -->

        <!-- START TEMPLATE -->
        <script type="text/javascript" src="js/plugins.js"></script>
        <script type="text/javascript" src="js/actions.js"></script>

        <script src="js/jquery.maskedinput.js"></script>
        <script src="js/jquery.maskMoney.js" type="text/javascript"></script>

        <script src="js/valor.js"></script>

        <script>
            $(function () {

                $('.data').mask('99/99/9999');



                $(document).on('submit','.form-update-user', function() {

                    var dados = new FormData(this);
                    var popup = $(this).data('popup');
                    $.ajax({
                        url : 'atualizar_usuario.php', /* URL que ser? chamada */
                        type : 'POST', /* Tipo da requisi??o */
                        data: dados,
                        dataType: 'json',
                        processData: false,
                        cache: false,
                        contentType: false,
                        success: function(data){
                            alert('Usuario atualizado com sucesso');
                            $('#'+popup).modal('hide');
                        }
                    });
                    return false;
                });


                $(document).on('submit','.form-delete-user', function() {
                    var dados = new FormData(this);
                    var popup = $(this).data('popup');
                    $.ajax({
                        url : 'delete_usuario.php', /* URL que ser? chamada */
                        type : 'POST', /* Tipo da requisi??o */
                        data: dados,
                        dataType: 'json',
                        processData: false,
                        cache: false,
                        contentType: false,
                        success: function(data){
                            alert('Usuario excluido com sucesso');
                            window.location.href='usuarios.php';
                        }
                    });
                    return false;
                });


            });
        </script>

        <script>
            $(function () {

                $('.tabledata').DataTable({
                    "paging": true,
                    "lengthChange": true,
                    "searching": true,
                    "ordering": true,
                    "info": true,
                    "autoWidth": true,
                    "order": [[ 0, "asc" ]]
                });

            });
        </script>
    </body>
</html>
