<?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">
       
	   <h2><font color='green'>Lista de contas</font></h2>
	      <table class="table">
		   <thead>
		    <th>Nome da conta</th>
		    <th>Saldo</th>
		    <th>Editar</th>
		   
		   </thead>
		  
		   <tbody>
		   
		   <?php 
		   $buscarContas = "SELECT * FROM contas";
		   $query = mysqli_query($con,$buscarContas);
		   while($row = mysqli_fetch_assoc($query)){
			   
			   $id_conta = $row['id'];
			   $nome_conta = $row['conta'];
			   $valor = $row['saldo'];
			   ?>
		   
		    <tr>
			 <td><?php echo $nome_conta;?></td>
			 <td><?php echo number_format($valor,2,",",".");?></td>
			 <td><button type="button" class="btn btn-info btn-sm"><a href="lista_contas.php?editar=<?php echo $id_conta;?>"
			 style="text-decoration:none; color:white;"><span>Editar</span></a></button></td>
			
			
			</tr>
		  
		  <?php }?>
		  
		  </tbody>
		  
		  </table>
        
        </div>

     <hr>
	 
	 <!-- Button to Open the Modal -->
<button type="button" class="btn btn-danger" data-toggle="modal" data-target="#myModal1">
  Saidas
</button>

<!-- The Modal -->
<div class="modal" id="myModal1">
  <div class="modal-dialog">
    <div class="modal-content">

      <!-- Modal Header -->
      <div class="modal-header">
        <h4 class="modal-title">Saidas</h4>
        <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>

      <!-- Modal body -->
      <div class="modal-body">
       
	    <div class="container">
	    <div class="col-sm-8">
		  	
		  
		  <form action="" method="post">
		 
		     <?php Saidas();?>
		   <div class="form-group">
		    <label for="descricao">Descricao</label>
			 <input type="text" name="descricao" class="form-control" autocomplete="off" required/>
		   </div>
		   
		   <div class="form-group">
		    <label for="valor_conta">Valor</label>
			 <input type="number" name="valor" class="form-control" autocomplete="off" required/>
		   </div>
		   
		    <div class="form-group">
		    <label for="conta_origem">Selecione a conta origem</label>
			 <select required name="conta_origem" class="form-control">
			  <option></option>
			  <?php selectConta();?>
			 
			 </select>
		   </div>
		   
		   <div class="form-group">
		    <label for="conta_destino">Selecione a conta destino(Opcional)</label>
			 <select name="conta_destino" class="form-control">
			  <option></option>
			  <?php selectConta();?>
			 
			 </select>
		   </div>
		   
		   
		   
		   <div class="form-group">		   
			 <input type="submit" name="submit" class="btn btn-primary btn-block" value="Enviar"/>
		   </div>
		    </form>
			</div>
	   	   </div>
	   
	   
	   
      </div>

      <!-- Modal footer -->
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
      </div>

    </div>
  </div>
</div>

<!-- Button to Open the Modal -->
<button type="button" class="btn btn-success" data-toggle="modal" data-target="#myModal">
  Entradas/Depositos
</button>

<!-- The Modal -->
<div class="modal" id="myModal">
  <div class="modal-dialog">
    <div class="modal-content">

      <!-- Modal Header -->
      <div class="modal-header">
        <h4 class="modal-title">Entradas</h4>
        <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>

      <!-- Modal body -->
      <div class="modal-body">
        <div class="container">
	    <div class="col-sm-8">
		  	
		  
		  <form action="entradas.php" method="post">
		 
		   <div class="form-group">
		    <label for="descricao">Descricao</label>
			 <input type="text" name="descricao" class="form-control" autocomplete="off" required/>
		   </div>
		   
		   <div class="form-group">
		    <label for="valor_conta">Valor</label>
			 <input type="number" name="valor" class="form-control" autocomplete="off" required/>
		   </div>
		   
		    <div class="form-group">
		    <label for="conta">Selecione a conta</label>
			 <select required name="conta" class="form-control">
			  <option></option>
			  <?php selectConta();?>
			 
			 </select>
		   </div>
		   
		   
		   
		   <div class="form-group">		   
			 <input type="submit" name="submit" class="btn btn-primary btn-block" value="Enviar"/>
		   </div>
		    </form>
			</div>
	   	   </div>
      </div>

      <!-- Modal footer -->
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
      </div>

    </div>
  </div>
</div>

<?php
if(isset($_GET['editar']) && !empty($_GET['editar'])){
	
	$id_editar = $_GET['editar'];
	
	$busca = "SELECT * FROM contas WHERE id='$id_editar'";
	$yes = mysqli_query($con,$busca);
	while($row = mysqli_fetch_assoc($yes)){
		
		$nome_conta = $row['conta'];
		$numero_conta = $row['numero_conta'];
		$moeda = $row['moeda'];
		$nib = $row['nib'];
		$iban = $row['iban'];
		$swift = $row['swift'];
		?>
		
		<div class="container">
	    <div class="col-sm-4">
		  
		  <form action="" method="post">
		 
		   <div class="form-group">
		    <label for="nome_conta">Nome da Conta</label>
			 <input type="text" name="nome_conta" class="form-control" value="<?php echo $nome_conta;?>" required/>
		   </div>
		   
		    <div class="form-group">
		    <label for="numero_conta">Numero da Conta</label>
			 <input type="text" name="numero_conta" class="form-control" value="<?php echo $numero_conta;?>" required/>
		   </div>
		   
		    <div class="form-group">
		    <label for="moeda">Moeda</label>
			 <input type="text" name="moeda" class="form-control" value="<?php echo $moeda;?>" required/>
		   </div>
		   
		    <div class="form-group">
		    <label for="nib">NIB</label>
			 <input type="text" name="nib" class="form-control" value="<?php echo $nib;?>" required/>
		   </div>
		   
		   <div class="form-group">
		    <label for="iban">Iban</label>
			 <input type="text" name="iban" class="form-control" value="<?php echo $iban;?>" required/>
		   </div>
		   
		   <div class="form-group">
		    <label for="nib">Swift</label>
			 <input type="text" name="swift" class="form-control" value="<?php echo $swift;?>" required/>
		   </div>
		   
		   
		   <div class="form-group">		   
			 <input type="submit" name="edit_conta" class="btn btn-primary btn-block" value="Actualizar"/>
		   </div>
		    </form>
			</div>
	   	   </div>
		
		
		
	<?php }
	
	  if(isset($_POST['edit_conta'])){
		  
		  
		  $conta = $_POST['nome_conta'];
		  
		  $Up = "UPDATE contas SET conta='$conta',numero_conta='$numero_conta',moeda='$moeda',nib='$nib',iban='$iban',swift='$swift' WHERE id='$id_editar'";
		  $func = mysqli_query($con,$Up);
		  
		  if($func){
			  
			    echo "<script>alert('Conta actualizada com sucesso')</script>";
			  echo "<script>window.open('lista_contas.php','_self')</script>";
			
			  
		  }else{
			   echo "<script>alert('Falhouoooooooouooooooo')</script>";
			  echo "<script>window.open('lista_contas.php','_self')</script>";
			 
		  }
	  }
	
}


 ?>

<hr>






	 
        

      </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>
