<?php include "includes/header.php";?>

<?php include "includes/navbar.php";?>

  <div id="wrapper">

    <!-- Sidebar -->
   <?php include "includes/sidebar.php";?>

    <div id="content-wrapper">

      <div class="container-fluid">

        <!-- Breadcrumbs-->
        <ol class="breadcrumb">
          <li class="breadcrumb-item">
            <a href="#">Ola</a>
          </li>
          <li class="breadcrumb-item active"><?php echo $_SESSION['nome'];?></li>
        </ol>

        <!-- Icon Cards-->
        <div class="row">
       <div class="container">      
            <!-- Button trigger modal -->
<button type="button" class="btn btn-success btn-sm" data-toggle="modal" data-target="#exampleModal">
  <span><i class="fas fa-fw fa-plus"></i> Usuarios</span>
</button>

<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        		 <div class="container">
		  <div class="col-sm-10">
		  
		   <form action="includes/code.php" method="post" enctype="multipart/form-data">
		    
		     <div class="form-group">
			  <label for="nome">Nome de usuario</label>
			   <input type="text" name="nome" class="form-control" autocomplete="off" required/>
			 </div>
			 
			  <div class="form-group">
			  <label for="email">Email de usuario</label>
			   <input type="email" name="email" class="form-control" autocomplete="off" required/>
			 </div>
			 
			  <div class="form-group">
			  <label for="senha">Senha de usuario</label>
			   <input type="password" name="senha" class="form-control" autocomplete="off" required/>
			 </div>
			 
			  <div class="form-group">
			  <label for="nivel">Nivel de usuario</label>
			  <select required name="nivel" class="form-control">
			   <option></option>
			   <option value='1'>Super Administrador</option>
			   <option value='2'>Administrador 2</option>

			  </select>
			 </div>
			 
			  <div class="form-group">
			  <label for="foto">Foto de usuario</label>
			   <input type="file" name="foto" class="form-control"/>
			 </div>
			 
			  <div class="form-group">			 
			   <input type="submit" name="submit" class="btn btn-primary btn-block" value="Cadastrar"/>
			 </div>
		   </form>
		  </div>
		 </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>
         

	   
	   
	   <br>
   
       <table class="table">
		  <thead>
		   <tr>
		    <th>Nome</th>
		    <th>Foto</th>
        <th>Active</th>
          <th>Editar</th>
		    <th>Eliminar</th>		   
		   </tr>
		    </thead>
			<tbody>
			<?php 
			$busca = mysqli_query($con,"SELECT * FROM usuarios");
			while($row = mysqli_fetch_assoc($busca)){
				
				$id = $row['id_user'];
				$nome = $row['nome'];
				$nivel = $row['nivel'];
				$estado = $row['status'];
				$foto = $row['foto'];
			?>
			 <tr>
			  <td><?php echo $nome;?></td>
        <td><img src="img/<?php echo $foto;?>" width="50px" height="50px"></td>
			  
	
			  <?php 
			 		if($_SESSION['nivel']  == 1){	  
			  if($estado == "Activo"){
				  ?>
				  
				<td><button type="button" class="btn btn-warning btn-sm"><a href="cadastrar_user.php?bloquear=<?php echo $id;?>"
			  style="text-decoration:none; color:white;"><span>Bloquear</span></a></button></td> 
			  <?php }else{
				  ?>
				   <td><button type="button" class="btn btn-success btn-sm"><a href="cadastrar_user.php?activar=<?php echo $id;?>"
			  style="text-decoration:none; color:white;"><span>Activar</span></a></button></td>
				  
			  <?php }
			  
			  ?>		  
			  
			 <td>
                <!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#exampleModalLongj<?php echo $id;?>">
  <span><i class="fa fa-paint-brush" aria-hidden="true"></i></span>
</button></td>

<!-- Modal -->
<div class="modal fade" id="exampleModalLongj<?php echo $id;?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLongTitle"><font color='red'>Actualizar <?php echo $row['nome'];?></font></h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
                  <div class="container">
              <div class="col-sm-10">
                  <form action="includes/code.php" method="post" enctype="multipart/form-data">
		    
			 <div class="form-group">
			  <label for="nome">Nome de usuario</label>
			   <input type="text" name="nome" class="form-control" value="<?php echo $row['nome'];?>" autocomplete="off" required/>
			 </div>
			 
			  <div class="form-group">
			  <label for="email">Email de usuario</label>
			   <input type="email" name="email" class="form-control" value="<?php echo $row['email'];?>" autocomplete="off" required/>
			 </div>
			 
			  <div class="form-group">
			  <label for="senha">Senha de usuario</label>
			   <input type="password" name="senha" class="form-control" autocomplete="off" required/>
			 </div>
			 
			  <div class="form-group">
			  <label for="nivel">Nivel de usuario</label>
			  <select required name="nivel" class="form-control">
			   <option></option>
			   <option value='1'>Super Administrador</option>
			   <option value='2'>Administrador 2</option>

			  </select>
			 </div>
			 <input type="hidden" name="idUp" value="<?php echo $id;?>"/>
			 
			  <div class="form-group">
			  <label for="foto">Foto de usuario</label>
			   <input type="file" name="foto" class="form-control"/>
			 </div>
			 
			  <div class="form-group">			 
			   <input type="submit" name="subUp" class="btn btn-primary btn-block" value="Actualizar"/>
			 </div>
		   </form>
           
         </div>
          </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>
              
             
           
                        <!-- Button trigger modal -->
<td><button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#exampleModalLong1<?php echo $id;?>">
  <span><i class="fa fa-trash" aria-hidden="true"></i></span>
</button></td>

<!-- Modal -->
<div class="modal fade" id="exampleModalLong1<?php echo $id;?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLongTitle"></h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <p><h3>Deseja eliminar <font color="red"><?php echo $row['nome'];?>?</font></h3></p>
        <button type="button" class="btn btn-danger btn-sm"><a href="includes/code.php?id_delete=<?php echo $id;?>" style="text-decoration:none; color:white"><span>Confirmar</span></a></button>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary btn-sm" data-dismiss="modal">Cancelar</button>
      </div>
    </div>
  </div>
</div>
<?php }else{
				echo "<td><font color='red'>sem permissao</font></td>";
				echo "<td><font color='red'>sem permissao</font></td>";
				echo "<td><font color='red'>sem permissao</font></td>";
			}?>
					  
			 </tr>
			
			<?php }?>
			</tbody>
		
		 </table>
	   
	   

      </div>
     </div>
        
     <!-- ACCOES DE USUARIO-->

	  <?php
    if(isset($_GET['bloquear']) && !empty($_GET['bloquear'])){
		
		$id_bloquear = $_GET['bloquear'];
		
		$actualiza_status = mysqli_query($con,"UPDATE usuarios SET status='Inactivo' WHERE id_user='$id_bloquear'");
		if($actualiza_status){
			echo "<script>window.open('cadastrar_user.php','_self')</script>";
		}else{			
			echo "ERRRRRRRRRRRRRROOOOOOORRRRRRRRRRR";
		}
		
		
	}
	
	
	
	if(isset($_GET['activar']) && !empty($_GET['activar'])){
		$id_activar = $_GET['activar'];		
		$actualiza_status1 = mysqli_query($con,"UPDATE usuarios SET status='Activo' WHERE id_user='$id_activar'");
		if($actualiza_status1){			
			echo "<script>window.open('cadastrar_user.php','_self')</script>";
		}else{			
			echo "ERRRRRRRRRRRRRROOOOOOORRRRRRRRRRR";
		}
		
		
	}
	
	
	
	if(isset($_GET['apagar']) && !empty($_GET['apagar'])){
		$id_apagar = $_GET['apagar'];
		
		$actualiza_status2 = mysqli_query($con,"DELETE FROM usuarios WHERE id_user='$id_apagar'");
		if($actualiza_status2){
			echo "<script>window.open('cadastrar_user.php','_self')</script>";
		}else{
			echo "ERRRRRRRRRRRRRROOOOOOORRRRRRRRRRR";
		}		
	}
	

	  ?>

      </div>
        
        
        </div>

     
        

      </div>
      <!-- /.container-fluid -->

      <!-- Sticky Footer -->
      <?php include "includes/footer.php";?>

    </div>
    <!-- /.content-wrapper -->

  </div>
  <!-- /#wrapper -->

  <!-- Scroll to Top Button-->
  <a class="scroll-to-top rounded" href="#page-top">
    <i class="fas fa-angle-up"></i>
  </a>

  <!-- Logout Modal-->
<?php include "includes/logout_modal.php";?>
 <?php include "includes/sweatscripFooter.php";?>