<?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">
		  <div class="col-sm-6">
		  
		  <?php 
		  if(isset($_GET['editar']) && !empty($_GET['editar'])){
			  
			  $id_editar = $_GET['editar'];
			  
			  $buscar_dados_edit = "SELECT * FROM usuarios WHERE id_user='$id_editar'";
			  $conecte = mysqli_query($con,$buscar_dados_edit);
			  while($row = mysqli_fetch_assoc($conecte)){
				  
				  $nome = $row['nome'];
				  $email = $row['email'];
				  $nivel = $row['nivel'];
			
		  ?>
		  		  
		   <form action="" 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 $nome;?>" required/>
			 </div>
			 
			  <div class="form-group">
			  <label for="email">Email de usuario</label>
			   <input type="email" name="email" class="form-control" value="<?php echo $email;?>" required/>
			 </div>
			 
			  <div class="form-group">
			  <label for="senha">Nova senha</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>1</option>
			   <option>2</option>
			   <option>3</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="Actualizar"/>
			 </div>
		   </form>
		   
		   
		    <?php }
			  
			  if(isset($_POST['submit'])){
				  
				 $nome = $_POST['nome'];
		         $email = $_POST['email'];
		         $senha = $_POST['senha'];
		         $salt = "euamoprogramacao";
		         $senha_crypt = crypt($senha,$salt);
		         $nivel = $_POST['nivel'];
		         $foto = $_FILES['foto']['name'];
		         $foto_tmp = $_FILES['foto']['tmp_name'];
		          move_uploaded_file($foto_tmp,"img/$foto");
				  
				 $update = "UPDATE usuarios SET nome='$nome',email='$email',senha='$senha_crypt',nivel='$nivel',
				 foto='$foto',status='Activo' WHERE id_user='$id_editar'";
				 $grande = mysqli_query($con,$update);
				 
				 if($grande){
					 
					 echo "<script>alert('Usuario actualizado com sucesso')</script>";
					 echo "<script>window.open('lista_users.php','_self')</script>";
				 }else{
					 
					 echo "EROROROOROROOOOOOOOOOOOOOORRRRRRRRRR";
				 }
				  
				  
			  }
			  
			  
		  }
		  ?>
		  
		  
		  </div>
		 
		 </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";?>

  <!-- Bootstrap core JavaScript-->
  <script src="vendor/jquery/jquery.min.js"></script>
  <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

  <!-- Core plugin JavaScript-->
  <script src="vendor/jquery-easing/jquery.easing.min.js"></script>

  <!-- Page level plugin JavaScript-->
  <script src="vendor/chart.js/Chart.min.js"></script>
  <script src="vendor/datatables/jquery.dataTables.js"></script>
  <script src="vendor/datatables/dataTables.bootstrap4.js"></script>

  <!-- Custom scripts for all pages-->
  <script src="js/sb-admin.min.js"></script>

  <!-- Demo scripts for this page-->
  <script src="js/demo/datatables-demo.js"></script>
  <script src="js/demo/chart-area-demo.js"></script>

</body>

</html>
