<?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></div>
<hr>
<?php
if(isset($_GET['cliente']) && !empty($_GET['cliente'])){
       
    $cliente = $_GET['cliente'];
  
}
            
        ?>
            

       <h5><i class="fa fa-shopping-cart" aria-hidden="true"></i>Plano de amortização de <font color='red'><?php echo $cliente;?></font></h5>
        
      
        <table class="table">
             <thead>
                <tr>
                    <th>Prestação</th>
                    <th>Capital</th>
                    <th>Juros de capital</th>
                    <th>Total</th>
                    <th>Previsão</th>
                    <th>Dias em atraso</th>
                     <th>Juros de mora</th>
                      <th>Pagar</th>
                </tr>
            </thead>
            <tbody>   
            <?php 
     if(isset($_GET['id']) && !empty($_GET['id'])){
       $id = $_GET['id'];
        $customer = $database->query("SELECT * FROM creditos WHERE id='$id'");
        while($row = $database->fetch_assoc($customer)){

            $cliente = $row['cliente'];
            $prestacao = $row['prestacoes'];
            $capital = $row['valor'];
            $juros = $row['juros'];
            $taxas = $row['taxas'];

            $tipo_pay = $row['tipo_pagamento'];

            $data_previsao = $row['data'];
            $tipo_credito = $row['tipo'];

          $totalLoan = $row['total'];


            $buscarMora = $database->query("SELECT * FROM tipos_creditos WHERE tipo_credito = '$tipo_credito'");
            $findTax = $database->fetch_assoc($buscarMora);
             
            //$juros_de_mora = $findTax['juros_de_mora'];
            
            $novo_credito = $capital + $juros;
            $novo_juro = $novo_credito * $taxas;
            $total2 = $novo_credito + $novo_juro;
            

               if($tipo_credito =="Diario"){
                $numero_dias  = 1;
               }elseif($tipo_credito =="Semanal"){
                $numero_dias  = 7;
               }elseif($tipo_credito == "Mensal"){
                $numero_dias = 30;
               }elseif($tipo_credito == "Quinzenal"){
                $numero_dias = 15;
               }else{
                $numero_dias = 0;
               }

	  



          

                $Dias = "30";
               
                $date = new DateTime($data_previsao);
                 $date->modify('+' .$Dias. 'day');
                 $mudanca_frente_1 = $date->format('Y-m-d');
 
                 $nova_data = $mudanca_frente_1;

                 $now = date('Y-m-d');

                //ACTUALIZAR CREDITOS APOS VENCIMENTO DA DATA DA PRESTACAO.
                 if($now > $nova_data){
                  $url = "creditos.php";
                  $message = "Prestação vencida no dia $nova_data, a recapitalizar o crédito...";
                  echo "<script>window.open('alertas/alertas_bad.php?url={$url}&&message={$message}','_self')</script>";

                  if($updateCredito = $database->query("UPDATE creditos SET valor ='$novo_credito',v_prest='$total2',juros='$novo_juro',total='$total2',data='$now' WHERE id='$id'")){
                    $url2 = "creditos.php";
                    $message2 = "Credito recapitalizado.";
                    echo "<script>window.open('alertas/alertas_good.php?url={$url2}&&message={$message2}','_self')</script>";
                  }
                 }


                 $date1=date_create($nova_data);
                   $date2=date_create($now);
                  $diff=date_diff($date1,$date2);
                  
                   if($date2 > $date1){
                    $diasAtraso = $diff->format("%a");
                $moraTotal = $totalLoan / 30 * ($diasAtraso);


                        }else{
                             $diasAtraso =0;
                             $moraTotal=0;

                         }
                         $totalLoanPlusmoraTotal =  $totalLoan + $moraTotal;


     ?>
           <tr>
            <td>1</td>
            <td><?php echo number_format($capital,2,",",".");?></td>
            <td><?php echo number_format($juros,2,",",".");?></td>
            <td><?php echo number_format($totalLoan,2,",",".");?></td>
            <td><?php echo $nova_data;?></td>

            
            
           <?php 
           



           $recibo = $database->query("SELECT * FROM pagamentos WHERE id_pagamento='$id'");
           $count = mysqli_num_rows($recibo);
           if($count ==1){
            ?>
            
            <td><font color="red">N/A</font></td>
            <td><font color="red">N/A</font></td>
            <td><button type="button" class="btn btn-info btn-sm"><a href="recibo.php?id=<?php echo $id;?>" target="_blank" style="text-decoration:none;color:white;"><span>Recibo</span></a></button></td>
           <?php }else{
?>
  
             <td><?php echo $diasAtraso;?></td>
            <td><?php echo number_format($moraTotal,2,",",".");?></td>
  <td><button type="button" class="btn btn-success btn-sm" data-toggle="modal" data-target="#exampleModalLong1<?php echo $id;?>">
  <span><i class="fa fa-coins" aria-hidden="true"></i></span>
</button></td>
          <?php  }
           ?> 
          

<!-- 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">Total da prestação por pagar: <font color="red"><?php echo $totalLoanPlusmoraTotal;?></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-8">
                <form action="loanValidation.php" method="post">
                    <div class="form-group">
                        <label for="valor">Valor</label>
                        <input type="number" class="form-control" name="valor" value="<?php echo $totalLoanPlusmoraTotal;?>" required/>
                    </div>
                    <div class="form-group">
                        <label for="conta">Conta capital</label>
                        <select required name="conta" class="form-control">
                            <option></option>
                            <?php selectConta();?>
                        </select>
                    </div>
                    <input type="hidden" name="valorMulta" value="<?php echo $totalLoanPlusmoraTotal;?>"/>
                    <input type="hidden" name="id" value="<?php echo $id;?>"/>
                    <input type="hidden" name="juros_de_mora" value="<?php echo $moraTotal;?>"/>
                    <input type="hidden" name="v_prest" value="<?php echo $totalLoan;?>"/>
                    <input type="hidden" name="tipo_pagamento" value="<?php echo $tipo_pay;?>"/>


                    <div class="form-group">
                        <button type="submit" name="payLoan" class="btn btn-primary btn-sm"><span>Pagar</span></button>
                    </div>
                </form>
            </div>
        </div>
      </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>
        
        <hr>
        <h3><center>Lista de pagamentos</center></h3>
        <table class="table">
          <thead>
            <tr>
              <th>item</th>
              <th>Nome do cliente</th>
              <th>Valor pago</th>
              <th>Data</th>
              <th>Recibo</th>
            </tr>
          </thead>
          <tbody>
            <?php 
            $item=1;
            $sql = $database->query("SELECT * FROM pagamentos WHERE nome_cliente='$cliente'");
            while($payments = $database->fetch_assoc($sql)){
            ?>
            <tr>
              <td><?php echo $item++;?></td>
              <td><?php echo $payments['nome_cliente'];?> </td>
              <td><?php echo number_format($payments['valor_pago'],2,",",".");?> </td>
              <td><?php echo $payments['data'];?> </td>
              <td><button type="button" class="btn btn-info btn-sm"><a href="recibo.php?id=<?php echo $payments['id_pagamento'];?>" target="_blank" style="text-decoration:none;color:white;"><span>Recibo</span></a></button></td>
              </tr>
              <?php }?>
          </tbody>
        </table>
        <!-- <button type="button" class="btn btn-info btn-sm"><a href="processamentos.php?id=<?php echo $id;?>" style="text-decoration:none; color:white;"><span>Finalizar</span></a></button>
        <button type="button" class="btn btn-info btn-sm"><a href="plano.php?id=<?php echo $id;?>" style="text-decoration:none; color:white;"><span>Imprimir Plano</span></a></button> -->

        </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";?>
