<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);
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-->

        <?php
        $loan->requisitaLoan();
        $loan->returnLoan();
        $loan->reforcoLoan();
        $loan->creditosBack();
        ?>




  <?php 
  if($_SESSION['nivel']==2){
      ?>

	   <!-- Button to Open the Modal INICIO  -->
<button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#myModal">
  Solicitar credito
</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">Creditos</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">
	      
		 <div class="form-group">
		  <label for="cliente">Nome do cliente</label>
		  <select required name="nome_cliente" class="form-control">
		   <option></option>
		   <?php $clientes->buscaClienteId();?>
		  </select>
		 </div>
		 
		 <div class="form-group">
		  <label for="valor">Valor</label>
		  <input type="text" name="valor" class="form-control" 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 $loan->selectContaId();?>
		  </select>
		 </div>
		 
		<div class="form-group">
		  <label for="tipo">Nome do credito</label>
		  <select required name="nome_credito" class="form-control">
		   <option></option>		  	   
	    <?php $loan->GetTypeLoan();?>	   
		   </select>
		 </div>
		 
		 <div class="form-group">
		  <label for="prestacoes">Prestacoes</label>
		  <select required name="prestacoes" class="form-control">
		   <option></option>		  	   
		   <?php $loan->prestacoesLimite();?>		   
		   
		     </select>
		 </div>
	 
		 <div class="form-group">
		  <label for="previsao">Garantias</label>
		  <input type="text" name="garantias" class="form-control" required/>
		 </div>
		 
		 <div class="form-group">
		  <label for="previsao">Valor estimado</label>
		  <input type="number" name="estimado" class="form-control" required/>
		 </div>
		 	 
		 <div class="form-group">
		  <label for="previsao">Finalidade</label>
		  <input type="text" name="finalidade" class="form-control" required/>
		 </div>

     <div class="form-group">
		  <label for="conta_origem">Tipo de pagamento</label>
		  <select required name="tipo_pagamento" class="form-control">
		   <option></option>
       <option>Normal</option>
       <option>Parcelado</option>
       <option>Negociado</option>
        </select>
		 </div>
		 
		  		 
		 <div class="form-group">
		  <input type="submit" name="submitLoan" 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 to Open the Modal INICIO  -->
     <button type="button" class="btn btn-warning btn-sm" data-toggle="modal" data-target="#myModal_2">
  Reforço de crédito
</button>

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

      <!-- Modal Header -->
      <div class="modal-header">
        <h4 class="modal-title">Creditos</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">
		 <div class="form-group">
		  <label for="cliente">Nome do cliente</label>
		  <select required name="id_cliente" class="form-control">
		   <option></option>
		  <?php $clientes->buscaClientePendente();?>
		  </select>
		 </div>
		 
		 <div class="form-group">
		  <label for="valor">Valor</label>
		  <input type="text" name="valor" class="form-control" 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 $loan->selectContaId();?>
		  </select>
		 </div>
			 
		 
		  		 
		 <div class="form-group">
		  <input type="submit" name="submitReforco" 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 to Open the Modal INICIO  -->
<button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#myModal_back">
  Lançar crédito em curso
</button>

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

      <!-- Modal Header -->
      <div class="modal-header">
        <h4 class="modal-title"><font color='red'>Lançamentos por data de créditos em curso</font></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">
	     

		  <div class="form-group">
		  <label for="cliente">Nome do cliente</label>
		  <select required name="id_cliente" class="form-control">
		   <option></option>
		   <?php $clientes->buscaClienteId();?>
		  </select>
		 </div>
		 
		 <div class="form-group">
		  <label for="valor">Valor</label>
		  <input type="text" name="valor" class="form-control" autocomplete="off" required/>
		 </div>

     <div class="form-group">
		  <label for="conta_origem">Conta origem</label>
		  <select required name="conta_id" class="form-control">
		   <option></option>
		   <?php $loan->selectContaId();?>
		  </select>
		 </div>

		<div class="form-group">
		  <label for="tipo">Nome do crédito</label>
		  <select required name="nome_credito" class="form-control">
		   <option></option>		  	   
	    <?php $loan->GetTypeLoan();?>	   
		   </select>
		 </div>
		 	 <div class="form-group">
		  <label for="prestacoes">Prestacoes</label>
		  <select required name="prestacoes" class="form-control">
		   <option></option>		  	   
		   <?php $loan->prestacoesLimite();?>		   
		   
		     </select>
		 </div>

		 <div class="form-group">
		  <label for="previsao">Garantias</label>
		  <input type="text" name="garantias" class="form-control" required/>
		 </div>
		 
		 
     <div class="form-group">
		  <label for="conta_origem">Tipo de pagamento</label>
		  <select required name="tipo_pagamento" class="form-control">
		   <option></option>
       <option>Normal</option>
       <option>Parcelado</option>
          <option>Negociado</option>
        </select>
		 </div>
	
		  <div class="form-group">
		  <label for="data">Data do crédito</label>
		  <input type="date" name="data" class="form-control" required/>
		 </div>
		 
		 <div class="form-group">
		  <input type="submit" name="submitBack" 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>



	  
	  

	  
	  
  <?php }elseif($_SESSION['nivel']==1){
      ?>
<!-- Button to Open the Modal INICIO  -->
<button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#myModal">
  Solicitar credito
</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">Creditos</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">
	      
		 <div class="form-group">
		  <label for="cliente">Nome do cliente</label>
		  <select required name="nome_cliente" class="form-control">
		   <option></option>
		   <?php $clientes->buscaClienteId();?>
		  </select>
		 </div>
		 
		 <div class="form-group">
		  <label for="valor">Valor</label>
		  <input type="text" name="valor" class="form-control" 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 $loan->selectContaId();?>
		  </select>
		 </div>
		 
		<div class="form-group">
		  <label for="tipo">Nome do credito</label>
		  <select required name="nome_credito" class="form-control">
		   <option></option>		  	   
	    <?php $loan->GetTypeLoan();?>	   
		   </select>
		 </div>
		 
		 <div class="form-group">
		  <label for="prestacoes">Prestacoes</label>
		  <select required name="prestacoes" class="form-control">
		   <option></option>		  	   
		   <?php $loan->prestacoesLimite();?>		   
		   
		     </select>
		 </div>
	 
		 <div class="form-group">
		  <label for="previsao">Garantias</label>
		  <input type="text" name="garantias" class="form-control" required/>
		 </div>
		 
		 <div class="form-group">
		  <label for="previsao">Valor estimado</label>
		  <input type="number" name="estimado" class="form-control" required/>
		 </div>
		 	 
		 <div class="form-group">
		  <label for="previsao">Finalidade</label>
		  <input type="text" name="finalidade" class="form-control" required/>
		 </div>

     <div class="form-group">
		  <label for="conta_origem">Tipo de pagamento</label>
		  <select required name="tipo_pagamento" class="form-control">
		   <option></option>
       <option>Normal</option>
       <option>Parcelado</option>
       <option>Negociado</option>
        </select>
		 </div>
		 
		  		 
		 <div class="form-group">
		  <input type="submit" name="submitLoan" 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-primary btn-sm"><a href="pedidos_credito.php" style="text-decoration:none;color:white"><span>Validar creditos(
	<?php 
				$busca_solicitados = "SELECT * FROM creditos WHERE status='aguardar'";
				$yes_we_do = mysqli_query($con,$busca_solicitados);
				$contagem = mysqli_num_rows($yes_we_do);
				
				echo "<font color='red'>".$contagem."</font>";
				
				?>



)</span></a></button>


      
  <?php }else{
      echo "N/A";
      echo "N/A";
  }
  
  ?>




	 



        
  
  
  

<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>Lista de creditos</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>Nome</th>
            <th>Valor</th>
            <th>Prestações</th>
            <th>Tipo</th>
             <th>Ver</th>
			 <th>Contracto</th>
			 <th>Dec.</th>
			  <th>Editar</th>
             <th>Cancelar</th>
             <th>Alerta</th>
                 </tr>
        </thead>
       
        <tbody>
            
          <?php 
          $item =1;
          $find = $database->query("SELECT cr.*, cl.* FROM creditos cr INNER JOIN clientes cl ON 
          cr.id_cliente = cl.id_cliente WHERE cr.status='pendente' AND cl.tipo_cliente='FP'");
          while($row = $database->fetch_assoc($find)){
     
             $id = $row['id'];
             $tipo_pay = $row['tipo_pagamento'];
             $data_previsao = $row['data'];
              $Dias = "30";
                $date = new DateTime($data_previsao);
                 $date->modify('+' .$Dias. 'day');
                 $mudanca_frente_1 = $date->format('Y-m-d');
 
                 $nova_data = $mudanca_frente_1;
            ?>
           <tr>   
            <td><?php echo $item++;?></td>
            <td><?php echo $row['nome_cliente'];?></td>
            <td><?php echo number_format((float)$row['valor'],2,",",".");?></td>
            <td><?php echo $row['prestacoes'];?></td>
            <td><?php echo $row['tipo_pagamento']?></td>
                   <td>
                <!-- Button trigger modal -->
                 <?php 
                 if($tipo_pay == "Normal"){
                        ?>
<button type="button" class="btn btn-success btn-sm"><a href="processamentos.php?id=<?php echo $row['id'];?>&&cliente=<?php echo $row['nome_cliente'];?>" style="text-decoration:none;color:white;"><span><i class="fa fa-eye" aria-hidden="true"></i></span></a>
</button>
      <?php }elseif($tipo_pay == "Parcelado"){
        ?>
        <button type="button" class="btn btn-danger btn-sm"><a href="parcelado.php?id=<?php echo $row['id'];?>&&cliente=<?php echo $row['nome_cliente'];?>" style="text-decoration:none;color:white;"><span><i class="fa fa-eye" aria-hidden="true"></i></span></a>
        </button>
      <?php }elseif($tipo_pay=="Negociado"){
?>
<button type="button" class="btn btn-warning btn-sm"><a href="negociado.php?id=<?php echo $row['id'];?>&&cliente=<?php echo $row['nome_cliente'];?>" style="text-decoration:none;color:white;"><span><i class="fa fa-eye" aria-hidden="true"></i></span></a>
</button>
      <?php }else{
        echo "";
      }
     ?>






</td>

<td><button type="button" class="btn btn-info btn-sm"><a href="contracto.php?id=<?php echo $id;?>" target="_blank" style="text-decoration:none;color:white"><span><i class="fa fa-print" aria-hidden="true"></i></span></a></button></td>



<td><button type="button" class="btn btn-info btn-sm"><a href="dec.php?id=<?php echo $id;?>" target="_blank" style="text-decoration:none;color:white"><span><i class="fa fa-book" aria-hidden="true"></i></span></a></button></td>

<td>
<a href="contracto_editavel.php?id=<?= $id ?>" target="_blank" class="btn btn-primary btn-sm">
    <span><i class="fa fa-pen" aria-hidden="true"></i></span>
</a>
</td>
<?php 
if($_SESSION['nivel']==1){
    ?>

              
                        <!-- 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['cliente'];?>?</font></h3></p>
        <button type="button" class="btn btn-danger btn-sm"><a href="creditos.php?delete_Loan=<?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>N/A</td>";
  }
  

?>        
      
      <td>
<?php
    $dataAtual = new DateTime();
    $dataPrevista = new DateTime($nova_data);
    $intervalo = $dataAtual->diff($dataPrevista);
    $diasRestantes = (int)$intervalo->format('%r%a'); // %r mantém sinal negativo

    if ($diasRestantes < 0) {
        // Crédito vencido
        echo '<span class="alert-sinal vermelho piscar">🔴 Vencido</span>';
    } elseif ($diasRestantes <= 5) {
        // 5 dias ou menos para o vencimento
        echo '<span class="alert-sinal amarelo piscar">🟡 ' . $diasRestantes . ' dias restantes</span>';
    } else {
        // Dentro do prazo
        echo '<span class="alert-sinal verde">🟢 ' . $diasRestantes . ' dias restantes</span>';
    }
?>
</td>      
            
      
          </tr>
        
        <?php }
          
          ?>  

         
      
   
               </tbody>
      </table>
    </div>
  </div>
  <div class="card-footer small text-muted">Jonas Dinis Chambela</div>
</div>


</div>

        </div>
      </div>
      <!-- /.container-fluid -->
		  </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";?>
 <?php include "includes/sweatscripFooter.php";?>
