<?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">
       
	   
	   <table class="table">
	    <thead>
		 <tr>
		  <th>Item</th>
		  <th>Cliente</th>
		  <th>Valor</th>
		  <th>Tipo</th>
		  <th>Aprovar</th>
		  <th>Eliminar</th>
		 </tr>
		</thead>
		
		<tbody>
		<?php 
		$item=1;
		
		$busca_solicitados = "SELECT * FROM solicitacoes WHERE status='pendente'";
		$yes_we_do = mysqli_query($con,$busca_solicitados);
		while($row = mysqli_fetch_assoc($yes_we_do)){
			
			$valor = $row['valor'];
			$cliente = $row['nome_cliente'];
			
		?>
		
		 <tr>
		  <td><?php echo $item++;?></td>
		  <td><?php echo $row['nome_cliente'];?></td>
		  <td><?php echo number_format($valor,2,",",".");?></td>
		  <td><?php echo $row['tipo_credito'];?></td>
		  
		  
		  <!--MODAL PARA APROVAR CREDITO-->
	<?php 
	$busca_aprovacao = "SELECT * FROM aprovacao WHERE cliente='$cliente'";
	$query = mysqli_query($con,$busca_aprovacao);
	$count = mysqli_fetch_assoc($query);
	
	$contagem = mysqli_num_rows($query);
	
	$status = $count['status'];
	$id = $count['id'];
	
	if($contagem == "" AND $_SESSION['nivel']==2){
		?>
		
		<td><button type='submit' class='btn btn-danger btn-sm'>
		  <a href='permissao.php?id=<?php echo $row['nome_cliente'];?>&&valor=<?php echo $valor;?>' style='text-decoration:none; color:white;'>
		  <span>Pedir permissao</span></a></button></td>
	
	<?php }elseif($status == 'pendente' AND $_SESSION['nivel']==2){
		?>
		<td><button type='button' class='btn btn-info btn-sm'><span>Aguarde...</span></button></td>
		
		
	<?php }elseif($status == 'pendente' AND $_SESSION['nivel']==1){
		?>
		
		<td><button type='submit' class='btn btn-success btn-sm'>
		  <a href='permitido.php?id=<?php echo $row['nome_cliente'];?>&&valor=<?php echo $valor;?>' style='text-decoration:none; color:white;'>
		  <span>Aprovar</span></a></button></td>
		
		
		
	<?php }elseif($status == 'Aprovado' AND $_SESSION['nivel']==1 ){
		
		?>
		<td><button type='button' class='btn btn-info btn-sm'><span>Aprovado</span></button></td>
		
		
	<?php }else{
		
		
		
		?>
		
	
		  
		  <td>
		  <button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#myModal<?php echo $row['id'];?>">
  Validar credito
</button>

<!-- The Modal -->
<div class="modal" id="myModal<?php echo $row['id'];?>">
  <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="aprovar_2.php" method="post">
		 <?php ?>
		 <div class="form-group">
		  <label for="cliente">Nome do cliente</label>
		  <select required name="cliente" class="form-control">
		   <option><?php echo $row['nome_cliente'];?></option>
		   
		  </select>
		 </div>
		 
		 <div class="form-group">
		  <label for="valor">Valor</label>
		  <input type="text" name="valor" class="form-control" value='<?php echo $row['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><?php echo $row['tipo_credito'];?></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>		   
		     </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">
		  <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">
		  <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>
		<?php }
	
	
	?>  
		  <!--TERMINA AQUI O MODAL PARA APROVAR CREDITO-->
		  
		  
		  <?php 
		  if($_SESSION['nivel'] == 1 || $_SESSION['nivel'] ==2){
		  
		  ?>
		  
		  <td><button type='submit' class='btn btn-danger btn-sm'>
		  <a href='solicitados.php?apagar=<?php echo $row['id'];?>' style='text-decoration:none; color:white;'>
		  <span>Apagar</span></a></button></td>
		
		<?php }else{
			
			echo "<td><font color='red'>N/A</font></td>";
		}?>
		 
		 </tr>
		<?php } ?>
		</tbody>
	   
	   </table>
        
        
        </div>

 <?php 
 if(isset($_GET['apagar']) && !empty($_GET['apagar'])){
	 
	 $id_apagar = $_GET['apagar'];
	 
	 $apaga = "DELETE FROM solicitacoes WHERE id='$id_apagar'";
	 $vamos = mysqli_query($con,$apaga);
	 
	 if($vamos){
		 
		 echo "<script>alert('Solicitação cancelada')</script>";
		 echo "<script>window.open('solicitados.php','_self')</script>";
	 }else{
		 echo "<script>alert('Ocorreu um erro')</script>";
		 echo "<script>window.open('solicitados.php','_self')</script>";
		 
	 }
	 
 }
 
 
 ?>    
    

         <button type='submit' class='btn btn-success btn-sm'>
		  <a href='report_aprovados.php' style='text-decoration:none; color:white;'>
		  <span>Relatorio</span></a></button>    

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