<?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>
		
		<!-- COMECA AQUI O Reembolso E Desembolso-->
	
<?php 
if($_SESSION['nivel'] ==1 || $_SESSION['nivel']==2){
?>
	<!-- Button to Open the Modal -->
<button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#desembolso">
 Total Desembolso
</button>

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

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

      <!-- Modal body -->
      <div class="modal-body">
      
	 
	  
	  <table class="table">
	   <thead>
	    <tr>
		 <th>Cliente</th>
		 <th>Valor</th>
		 <th>Juros</th>
		 <th>Total</th>
		</tr>
	   </thead>
	   
	   <tbody>
	    <?php 
	  $tira_credito = "SELECT * FROM creditos WHERE status='pendente' AND tipo='Trimestral'";
	  $naguira = mysqli_query($con,$tira_credito);
	  while($row = mysqli_fetch_assoc($naguira)){
		  
		  $cliente = $row['cliente'];
		  $valor = $row['valor'];
		  $juros = $row['juros'];
		  $total = $row['total'];		  
	  	  ?>
	    <tr>
		 <td><?php echo $cliente;?></td>
		 <td><?php echo $valor;?></td>
		 <td><?php echo $juros;?></td>
		 <td><?php echo $total;?></td>
		</tr>
		
		<?php }?>
	   </tbody>
	   
	   <?php 
	 $stock = mysqli_query($con, "SELECT SUM(valor) as soma  FROM creditos WHERE status='pendente' AND tipo='Trimestral'");
 
  while($total = mysqli_fetch_array($stock)){
	  
	  $stock_total = $total['soma'];
  }
  
   $stock1 = mysqli_query($con, "SELECT SUM(juros) as soma  FROM creditos WHERE status='pendente' AND tipo='Trimestral'");
 
  while($total1 = mysqli_fetch_array($stock1)){
	  
	  $stock_total1 = $total1['soma'];
  }
  
   $stock2 = mysqli_query($con, "SELECT SUM(total) as soma  FROM creditos WHERE status='pendente' AND tipo='Trimestral'");
 
  while($total2 = mysqli_fetch_array($stock2)){
	  
	  $stock_total2 = $total2['soma'];
  }
	   
	   ?>
	   
	   
	   <td colspan="2"><b>Valor:</b><?php echo number_format($stock_total,2,",",".");?></td>
	   <td><b>Juros:</b><?php echo number_format($stock_total1,2,",",".");?></td>
	   <td><b>Total:</b><?php echo number_format($stock_total2,2,",",".");?></td>
	  </table>
	  
	  
      </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 btn-sm" data-toggle="modal" data-target="#reembolso">
  Prev. Reembolso
</button>

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

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

      <!-- Modal body -->
      <div class="modal-body">
       
	 <table class="table">
	   <thead>
	    <tr>
		 <th>Cliente</th>
		 <th>Valor previsto</th>
		</tr>
	   </thead>
	   
	   <tbody>
	    <?php 
	  $tira_credito = "SELECT * FROM creditos WHERE status='pendente' AND tipo='Trimestral'";
	  $naguira = mysqli_query($con,$tira_credito);
	  while($row = mysqli_fetch_assoc($naguira)){
		  
		  $cliente = $row['cliente'];
		   $prest = $row['v_prest'];		  
	  	  ?>
	    <tr>
		 <td><?php echo $cliente;?></td>
		 <td><?php echo number_format($prest,2,",",".");?></td>
		 </tr>
		
		<?php }?>
	   </tbody>
	   
	   <?php 
	 $stock = mysqli_query($con, "SELECT SUM(v_prest) as soma  FROM creditos WHERE status='pendente' AND tipo='Trimestral'");
 
  while($total = mysqli_fetch_array($stock)){
	  
	  $stock_total = $total['soma'];
  }
  
  
	   
	   ?>
	   
	   
	   <td colspan="2"><b>Total previsto mensal:</b><?php echo number_format($stock_total,2,",",".");?></td>
	   
	  </table>
	   
	   
      </div>

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

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

<?php }else{
	
	echo "";
}?>
	<hr>
<!-- TERMINA AQUI O Desembolso E Reembolso-->
		
		
		
		

        <table class="table">
	 <thead>
	  <tr>
	   <th>Nome do cliente</th>
	   <th>Tipo de credito</th>
	   <th>Valor</th>
	   <th>Juros</th>
	   <th>Total</th>
	   <th>Data</th>
	   <th>Contrato</th>
	   <th>Amortizar</th>
	   <th>Editar</th>
	   <th>Apagar</th>
	  </tr>
	 </thead>
	 
	 <tbody>
	 <?php 
	 if($_SESSION['nivel'] ==1 || $_SESSION['nivel']==2){
	 
	 
	 $busca_trato = "SELECT * FROM creditos WHERE tipo='Trimestral' AND status = 'pendente'";
	 $confirma = mysqli_query($con,$busca_trato);
	 while($row = mysqli_fetch_assoc($confirma)){
		 
		 $id = $row['id'];
		 $nome = $row['cliente'];
		 $valor = $row['valor'];
		 $tipo = $row['tipo'];
		 $juros = $row['juros'];
		 $total = $row['total'];
		 $siku = $row['data'];	
		 ?>
	 
	  <tr>
	   <td><?php echo $nome;?></td>
	   <td><?php echo $tipo;?></td>
	   <td><?php echo number_format($valor,2,",",".");?></td>
	   <td><?php echo number_format($juros,2,",",".");?></td>
	   <td><?php echo number_format($total,2,",",".");?></td>
	   <td><?php echo $siku;?></td>
	   <td><button type="button" class="btn btn-info btn-sm"><a href="contracto.php?id=<?php echo $id;?>"
	   style="text-decoration:none; color:white;"><span>Contrato</span></a></button></td>
	   
	  
      <?php 
	  if($_SESSION['nivel'] == 1 || $_SESSION['nivel'] == 2){
	  
	  ?>

	  <td><button type="button" class="btn btn-success btn-sm"><a href="pay_trimestral.php?id=<?php echo $id;?>"
	   style="text-decoration:none; color:white;"><span>Pagar</span></a></button></td>
	  
	  
	  	  
	  	   <!-- Button to Open the Modal INICIO  -->
<td><button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#myModal<?php echo $id;?>">
  Editar credito
</button>

<!-- The Modal -->
<div class="modal" id="myModal<?php echo $id;?>">
  <div class="modal-dialog">
    <div class="modal-content">

      <!-- Modal Header -->
      <div class="modal-header">
        <h4 class="modal-title">Credito de <?php echo $nome;?></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="actualizar_credito.php" method="post">
		
		 <div class="form-group">
		  <label for="cliente">Nome do cliente</label>
		  <select required name="cliente" class="form-control">
		   <option><?php echo $nome;?></option>
		   
		  </select>
		 </div>
		 
		 <div class="form-group">
		  <label for="valor">Valor</label>
		  <input type="text" name="valor" class="form-control" value="<?php echo $valor;?>" autocomplete="off" required/>
		 </div>
		 
		 <div class="form-group">
		  <label for="conta_origem">Conta origem</label>
		  <select required name="conta_origem" class="form-control">
		   <option></option>
		   <?php selectConta();?>
		  </select>
		 </div>
		 
		<div class="form-group">
		  <label for="tipo">Tipo de credito</label>
		  <select required name="tipo" class="form-control">
		   <option></option>		  	   
		   <option>Mensal</option>	
		   <option>Bimensal</option>	
		   <option>Trimestral</option>		   
		   <option>Semestral</option>
		   <option>Anual</option>			   
		   </select>
		 </div>
		 
		 <div class="form-group">
		  <label for="prestacoes">Prestacoes</label>
		  <select required name="prestacoes" class="form-control">
		   <option></option>		  	   
		   <option>1</option>		   
		   <option>2</option>		   
		   <option>3</option>		   
		   <option>4</option>		   
		   <option>5</option>		   
		   <option>6</option>		   
		   <option>7</option>		   
		   <option>8</option>		   
		   <option>9</option>		   
		   <option>10</option>		   
		   <option>11</option>		   
		   <option>12</option>		   
		     </select>
		 </div>
		 		 
		 <div class="form-group">
		  <label for="taxa">Taxa de juros</label>
		  <select required name="taxas" class="form-control">
		   <option></option>
		  	   
	     <option value='0.10'>10%</option>	
            <option value='0.12'>12%</option>
            <option value='0.15'>15%</option>
            <option value='0.18'>18%</option>
            <option value='0.20'>20%</option>
		   <option value='0.25'>25%</option>
            <option value='0.3'>30%</option>
            <option value='0.35'>35%</option>
            <option value='0.40'>40%</option>		   
		  	   
		  </select>
		 </div>	 
		 
		  <div class="form-group">
		  <label for="previsao">Data Prevista</label>
		  <input type="date" name="previsao" class="form-control" required/>
		 </div>
		 
		 <div class="form-group">
		  <input type="submit" name="submit" 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></td>
	  
	  
	      <!-- funcao apagar-->
<td><button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#myModal_apagar<?php echo $id;?>">
  Apagar
</button></td>


<!-- The Modal -->
<div class="modal" id="myModal_apagar<?php echo $id;?>">
  <div class="modal-dialog">
    <div class="modal-content">

      <!-- Modal Header -->
      <div class="modal-header">
        <h4 class="modal-title">Deseja Cancelar credito de <font color="red"><?php echo $nome;?></font></h4>
        <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>

      <!-- Modal body -->
      <div class="modal-body">
      <button type="button" class="btn btn-info btn-block"><a href="mensais.php?apagar=<?php echo $id;?>"
	   style="text-decoration:none; color:white;"><span>Confirmar</span></a></button>
      </div>

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

    </div>
  </div>
</div>
	  
	  
	  
	  
	  
	  
	  
	  <?php }else{
		  ?>
		  <td><button type="button" class="btn btn-info btn-sm"><a href="pagar_2.php?id=<?php echo $id;?>"
	   style="text-decoration:none; color:white;"><span>Lançar pagamento</span></a></button></td>
		  
		  
	  <?php } ?>
	  
	  
	  </tr>
	  
	 <?php }
	 
	 
	 
	 }else{
		 
		 $gestor = $_SESSION['nome'];
		 
	 $busca_trato = "SELECT * FROM creditos WHERE tipo='Trimestral' AND status = 'pendente' AND vendedor='$gestor'";
	 $confirma = mysqli_query($con,$busca_trato);
	 while($row = mysqli_fetch_assoc($confirma)){
		 
		 $id = $row['id'];
		 $nome = $row['cliente'];
		 $valor = $row['valor'];
		 $tipo = $row['tipo'];
		 $juros = $row['juros'];
		 $total = $row['total'];
		 $siku = $row['data'];	
		 ?>
	 
	  <tr>
	   <td><?php echo $nome;?></td>
	   <td><?php echo $tipo;?></td>
	   <td><?php echo $valor;?></td>
	   <td><?php echo $juros;?></td>
	   <td><?php echo $total;?></td>
	   <td><?php echo $siku;?></td>
	   <td><button type="button" class="btn btn-info btn-sm"><a href="contracto.php?id=<?php echo $id;?>"
	   style="text-decoration:none; color:white;"><span>Contrato</span></a></button></td>
	   
	   
	   
	   
	   <?php 
	  if($_SESSION['nivel'] == 1 || $_SESSION['nivel'] == 2){
	  
	  ?>

	  <td><button type="button" class="btn btn-success btn-sm"><a href="pay.php?id=<?php echo $id;?>"
	   style="text-decoration:none; color:white;"><span>Pagar</span></a></button></td>
	  
	  <?php }else{
		  ?>
		  <td><button type="button" class="btn btn-info btn-sm"><a href="pagar_2.php?id=<?php echo $id;?>"
	   style="text-decoration:none; color:white;"><span>Lançar pagamento</span></a></button></td>
		  
		  
	  <?php } ?>
	  </tr>
	  
	 <?php }
		 
		 
		 
	 }
	 
	 
	 
	 ?>
	 </tbody>
	
	</table>
      

 <?php 
if(isset($_GET['apagar']) && !empty($_GET['apagar'])){
	
	$apagar = $_GET['apagar'];
	
	$del = "DELETE FROM creditos WHERE id='$apagar'";
	$query = mysqli_query($con,$del);
	
	if($query){
		 echo "<script>alert('Credito anulado com sucesso')</script>";
		 echo "<script>window.open('trimestral.php','_self')</script>";
		
	}else{
		 echo "<script>alert('FALHA')</script>";
		 echo "<script>window.open('trimestral.php','_self')</script>";
	}
	
}

?>       

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