<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
?>
<?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-8">
<?php $account->deleteAccount();?>
        
	   
	     <!-- Button to Open the Modal INICIO  -->
         <button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#myModal2">
  <i class="fas fas fa-plus"> Contas</i>
</button>

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

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

      <!-- Modal body -->
      <div class="modal-body">
        <div class="col-sm-8">	   
	    <form action="" method="post">
	    <?php $account->registerAcount();?>
        <div class="form-group">
		  <label for="descricao">Nome da conta</label>
		  <input type="text" name="nome_conta" class="form-control" required/>
		 </div>
		 	 
		 <div class="form-group">
		  <label for="previsao">Saldo inicial</label>
		  <input type="text" name="saldo" class="form-control"/>
		 </div>     
		 
		  		 
		 <div class="form-group">
		  <input type="submit" name="submitAccount" class="btn btn-info btn-block" value="Submeter"/>
		 </div>
		
		</form>	   
	   </div>
      </div>

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

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


<!-- BUTTONS ENTRADAS-->
<button type="button" class="btn btn-success btn-sm" data-toggle="modal" data-target="#myModal4">
  <i class="fas fas fa-plus"> Entradas</i>
</button>

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

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

      <!-- Modal body -->
      <div class="modal-body">
        <div class="col-sm-8">	   
	    <form action="" method="post">
	      <?php $account->incomes();?>
        <div class="form-group">
		  <label for="descricao">Descricao</label>
		  <input type="text" name="descricao" class="form-control" required/>
		 </div>
		 	 
		 <div class="form-group">
		  <label for="previsao">Valor</label>
		  <input type="text" name="valor" class="form-control"/>
		 </div>     

         <div class="form-group">
            <label for="conta">Conta</label>
            <select required name="conta" class="form-control">
                <option></option>
                <?php $account->fetchAccount();?>
            </select>
         </div>	 
       
		 <div class="form-group">
		  <input type="submit" name="submitIncomes" class="btn btn-info btn-block" value="Submeter"/>
		 </div>
		
		</form>	   
	   </div>
      </div>

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

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

<!--fFIM DE BUTTONS ENTRADAS-->



<!--BUTTONS SAIDAS--> 
<button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#myModal3">
  <i class="fas fas fa-minus"> Saidas</i>
</button>

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

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

      <!-- Modal body -->
      <div class="modal-body">
        <div class="col-sm-8">	   
	    <form action="" method="post">
	     <?php $account->Expensives();?>
        <div class="form-group">
		  <label for="descricao">Descricao</label>
		  <input type="text" name="descricao" class="form-control" required/>
		 </div>
		 	 
		 <div class="form-group">
		  <label for="previsao">Valor</label>
		  <input type="text" name="valor" class="form-control"/>
		 </div> 
		
         <div class="form-group">
            <label for="conta">Conta</label>
            <select required name="conta" class="form-control">
                <option></option>
                <?php $account->fetchAccount();?>
            </select>
         </div> 
	  		 
		 <div class="form-group">
		  <input type="submit" name="submitExpensives" class="btn btn-info btn-block" value="Submeter"/>
		 </div>
		
		</form>	   
	   </div>
      </div>

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

    </div>
  </div>
</div>
<!--FIM DE BUTTONS SAIDAS-->


<!--INICIO DE BUTTONS REPORTS--> 
	     <!-- Button to Open the Modal INICIO  -->
         <button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#myModal5">
  <i class="fas fas fa-book"> Relatorio pdf</i>
</button>

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

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

      <!-- Modal body -->
      <div class="modal-body">
        <div class="col-sm-8">	   
	    <form action="relatorioPorConta.php" target="_blank" method="post">

        <div class="form-group">
		  <label for="previsao">Data inicial</label>
		  <input type="date" name="data_inicio" class="form-control"/>
		 </div> 

         <div class="form-group">
		  <label for="previsao">Data final</label>
		  <input type="date" name="data_fim" class="form-control"/>
		 </div> 
         <div class="form-group">
            <label for="conta">Conta</label>
            <select required name="conta_id" class="form-control">
                <option></option>
                <?php $account->fetchAccount();?>
            </select>
         </div> 
		  		 
		 <div class="form-group">
		  <input type="submit" name="submitReport" class="btn btn-info btn-block" value="Submeter"/>
		 </div>
		
		</form>	   
	   </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 type="button" class="btn btn-success btn-sm" data-toggle="modal" data-target="#myModal7">
  <i class="fas fas fa-table"> Relatorio excel</i>
</button>

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

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

      <!-- Modal body -->
      <div class="modal-body">
        <div class="col-sm-8">	   
	    <form action="excel-report.php" target="_blank" method="post">

        <div class="form-group">
		  <label for="previsao">Data inicial</label>
		  <input type="date" name="data_inicio" class="form-control"/>
		 </div> 

         <div class="form-group">
		  <label for="previsao">Data final</label>
		  <input type="date" name="data_fim" class="form-control"/>
		 </div> 
         <div class="form-group">
            <label for="conta">Conta</label>
            <select required name="conta" class="form-control">
                <option></option>
                <?php $account->fetchAccount();?>
            </select>
         </div> 
		  		 
		 <div class="form-group">
		  <input type="submit" name="submitReportExcel" class="btn btn-info btn-block" value="Submeter"/>
		 </div>
		
		</form>	   
	   </div>
      </div>

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

    </div>
  </div>
</div>
<!--FIM DE BUTTONS REPORT-->

        
</div>
       </div>    
        </div>
<br><br>
        <table class="table">
            <thead>
                <tr>
                    <th>Id</th>
                    <th>Conta</th>
                    <th>Total entradas</th>
                    <th>Total saidas</th>
                    <th>Saldo</th>
                    <th>Editar</th>
                    <th>Apagar</th>
                </tr>
                 </thead>
                 <tbody>
                    <?php 
                    $item=1;
                    $sql = $database->query("SELECT * FROM contas");
                    while($row = $database->fetch_assoc($sql)){
                         $id = $row['id'];
                         $conta = $row['conta'];

                        $somaEntradas = $database->query("SELECT SUM(entrada) as soma FROM transacoes WHERE caixa='$id'");
                        $confirma = $database->fetch_assoc($somaEntradas);
                        $totalEntradas = $confirma['soma'];

                        $somaSaidas = $database->query("SELECT SUM(saida) as soma FROM transacoes WHERE caixa='$id'");
                        $confirma = $database->fetch_assoc($somaSaidas);
                        $totalSaidas = $confirma['soma'];
                    ?>
                    <tr>
                    <td style="background-color:silver"><?php echo $item++;?></td>
                        <td style="background-color:silver"><b><?php echo $row['conta'];?></b></td>
                        <td style="background-color:silver"><button type="button" class="btn btn-success btn-sm"><span><?php echo number_format($totalEntradas,2,",",".");?></span> </button></td>
                        <td style="background-color:silver"><button type="button" class="btn btn-danger btn-sm"><span><?php echo number_format($totalSaidas,2,",",".");?></span> </button></td>
                        <td style="background-color:silver"><button type="button" class="btn btn-warning btn-sm"><span><?php echo number_format($row['saldo'],2,",",".");?></span> </button></td>



                        <td style="background-color:silver">
                <!-- 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['conta'];?></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="" method="post" enctype="multipart/form-data">
		      <?php $account->updateAccount();?>
			 <div class="form-group">
			  <label for="nome">Nome da conta</label>
			   <input type="text" name="nome_conta" class="form-control" value="<?php echo $row['conta'];?>" autocomplete="off" required/>
			 </div>

             <input type="hidden" name="id" value="<?php echo $id;?>"/>
			 			  		 			 
			  <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 style="background-color:silver">
                        <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 todos registros de <font color="red"><?php echo $row['conta'];?>?</font></h3></p>
        <button type="button" class="btn btn-danger btn-sm"><a href="contas.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>

                    </tr>
                    <?php }?>
                 </tbody>
        </table>
        <br><br>
<div class="container-fluid">

<!-- Breadcrumbs-->

<!-- DataTables Example -->
<div class="card mb-3">
  <div class="card-header">
    <i class="fas fa-table"></i>
    <b>Transacoes</b></div>
  <div class="card-body">
    <div class="table-responsive">
      <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
        <thead>
          <tr>
            <th>Item</th>
            <th>Descricao</th>
            <th>Entradas</th>
            <th>Saidas</th>
            <th>Conta</th>
             <th>Data</th>
            <th>Apagar</th>
                </tr>
        </thead>
       
        <tbody>
            
          <?php 
          
          $account->return();
          $item=1;
            $find = $database->query("SELECT tr.*,con.* FROM transacoes tr INNER JOIN contas con ON tr.caixa = con.id");
          while($row = $database->fetch_assoc($find)){
     
               ?>
           <tr>  
           <td><?php echo $item++;?></td> 
              <td><?php echo $row['descricao'];?></td>            
            <td><?php echo number_format((float)$row['entrada'],2,",",".");?></td>
            <td><?php echo number_format((float)$row['saida'],2,",",".");?></td>
            <td><?php echo $row['conta']?></td>
            <td><?php echo $row['data']?></td>
            
              <td><button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#exampleModalLong1<?php echo $row['id'];?>">
  <span><i class="fa fa-trash" aria-hidden="true"></i></span>
</button></td>

<!-- Modal -->
<div class="modal fade" id="exampleModalLong1<?php echo $row['id'];?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle1" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLongTitle1"></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 cancelar <font color="red"><?php echo $row['descricao'];?>?</font></h3></p>
        <button type="button" class="btn btn-danger btn-sm"><a href="contas.php?id=<?php echo $row['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>
          

              </tr>
        
        <?php }
          
          ?>  

         
      
   
               </tbody>
      </table>
    </div>
  </div>
  <div class="card-footer small text-muted">Jonas Dinis Chambela</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>
