<?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="#">Olá</a>
        </li>
        <li class="breadcrumb-item active"><?php echo $_SESSION['nome'];?></li>
      </ol>

      <div class="card mb-3">
        <div class="card-header">
          <i class="fas fa-table"></i> Créditos Prestes a Vencer (Próximos 7 dias)
        </div>
        <div class="card-body">
          <div class="table-responsive">
            
            <?php 
              // Query ajustada com INNER JOIN usando 'id_cliente' em ambas as tabelas
              $busca_sucesso = "SELECT 
                                  c.id AS id_credito,
                                  c.v_prest,
                                  c.status,
                                  c.data AS data_lancamento,
                                  DATE_ADD(c.data, INTERVAL 30 DAY) AS data_vencimento,
                                  cli.id_cliente,
                                  cli.nome_cliente,
                                  cli.cell1,
                                  cli.cell2
                                FROM creditos c
                                INNER JOIN clientes cli ON c.id_cliente = cli.id_cliente
                                WHERE c.status = 'pendente' 
                                  AND DATE_ADD(c.data, INTERVAL 30 DAY) BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 7 DAY)";

              $query_link = mysqli_query($con, $busca_sucesso);
            ?>
            
            <table class="table table-bordered" width="100%" cellspacing="0">
              <thead>
                <tr>
                  <th>Item</th>
                  <th>Nome do Cliente</th>
                  <th>Contato</th>
                  <th>Valor da Prestação</th>
                  <th>Data de Vencimento</th>
                  <th>Notificar</th>
                </tr>
              </thead>
              
              <tbody>
                <?php 
                $item = 1;
                while($meus = mysqli_fetch_assoc($query_link)){
                    $id_credito      = $meus['id_credito']; 
                    $nome_cliente    = $meus['nome_cliente'];
                    $v_prest         = $meus['v_prest'];
                    $cell1           = $meus['cell1'];
                    $data_vencimento = date('d/m/Y', strtotime($meus['data_vencimento']));
                ?>
                
                <tr>
                  <td><?php echo $item++; ?></td>
                  <td><?php echo htmlspecialchars($nome_cliente); ?></td>
                  <td><?php echo htmlspecialchars($cell1); ?></td>
                  <td><?php echo number_format($v_prest, 2, ",", "."); ?> MT</td>
                  <td><span class="badge badge-warning" style="font-size: 14px;"><?php echo $data_vencimento; ?></span></td>
                  
                  <!-- Botão aciona Modal com ID dinâmico por Crédito -->
                  <td>
                    <button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#modalEmail<?php echo $id_credito; ?>">
                      <i class="fas fa-envelope"></i> Enviar Mensagem
                    </button>

                    <!-- Modal Dinâmico -->
                    <div class="modal fade" id="modalEmail<?php echo $id_credito; ?>" tabindex="-1" role="dialog" aria-labelledby="modalLabel<?php echo $id_credito; ?>" aria-hidden="true">
                      <div class="modal-dialog" role="document">
                        <div class="modal-content">
                          <div class="modal-header">
                            <h5 class="modal-title" id="modalLabel<?php echo $id_credito; ?>">Notificar <?php echo htmlspecialchars($nome_cliente); ?></h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                              <span aria-hidden="true">&times;</span>
                            </button>
                          </div>
                          <div class="modal-body">
                            <form action="enviar_sms.php" method="POST">
                              <input type="hidden" name="id_credito" value="<?php echo $id_credito; ?>">
                              <input type="hidden" name="id_cliente" value="<?php echo $meus['id_cliente']; ?>">
                              
                              <div class="form-group">
                                <label>Cliente</label>
                                <input type="text" class="form-control" name="cliente" value="<?php echo htmlspecialchars($nome_cliente); ?>" readonly>
                              </div>

                              <div class="form-group">
                                <label>Mensagem de Lembrete</label>
                                <textarea name="mensagem" class="form-control" rows="5" required>Estimado(a) <?php echo htmlspecialchars($nome_cliente); ?>, lembramos que a sua prestação no valor de <?php echo number_format($v_prest, 2, ",", "."); ?> MT vence no dia <?php echo $data_vencimento; ?>. Por favor, efetue o pagamento para evitar penalidades.</textarea>
                              </div>

                              <button type="submit" name="submit" class="btn btn-info btn-block">Enviar Lembrete</button>
                            </form>
                          </div>
                        </div>
                      </div>
                    </div>
                  </td>

               
                </tr>
                
                <?php } ?>
              </tbody>
            </table>

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

    </div>
    <!-- /.container-fluid -->

    <?php include "includes/footer.php";?>

  </div>
  <!-- /.content-wrapper -->

</div>
<!-- /#wrapper -->

<a class="scroll-to-top rounded" href="#page-top">
  <i class="fas fa-angle-up"></i>
</a>

<?php include "includes/logout_modal.php";?>

<!-- Scripts do Bootstrap -->
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="vendor/jquery-easing/jquery.easing.min.js"></script>
<script src="js/sb-admin.min.js"></script>

</body>
</html>